• 通用mapper


    导入依赖

    	<!-- 通用Mapper -->
    	<dependency>
    		<groupId>com.github.abel533</groupId>
    		<artifactId>mapper</artifactId>
    	</dependency>
    

    配置通用mapper插件

    <plugins>
      <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor">
        <property name="IDENTITY" value="MYSQL"/>
        <property name="mappers" value="com.github.abel533.mapper.Mapper"/>
      </plugin>
    </plugins>
    
    
    <mappers>
    	<!-- 引入映射资源文件 -->
    	<!-- <mapper resource="UserMapper.xml"/> -->
    
    	<package name="com.huawei.support" />
    </mappers>
    

    继承通用的Mapper<T>,必须指定泛型<T>

    public interface NewUserMapper extends Mapper<User> {
    
    }
    

    实体必须定义jpa注解

    @Table(name = "user")
    public class User implements Serializable {
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Long id;
    	@Column(name = "username")
    	private String username;
    	@Column(name = "password")
    	private String password;
    	@Column(name = "phone")
    	private String phone;
    	@Column(name = "email")
    	private String email;
    

    测试用例

    public class NewUserMapperTest {
    
        
        SqlSessionFactory sqlSessionFactory;
        SqlSession sqlSession;
        NewUserMapper  newUserMapper;
    
        @Before
        public void setUp() throws Exception {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession = sqlSessionFactory.openSession(true);
            newUserMapper=sqlSession.getMapper(NewUserMapper.class);
        }
    
        @Test
        public void testSelectOne() {
            User user = new User();
            user.setId(1L);
            User selectOne = this.newUserMapper.selectOne(user);
            System.out.println(selectOne);
        }
    
        @Test
        public void testSelect() {
          List<User> list = this.newUserMapper.select(null);
          for (User user : list) {
            System.out.println(user);
        }
        }
    
        @Test
        public void testSelectCount() {
            int selectCount = this.newUserMapper.selectCount(null);
            System.out.println(selectCount);
        }
    
        @Test
        public void testSelectByPrimaryKey() {
           User user = this.newUserMapper.selectByPrimaryKey(2L);
           System.out.println(user);
        }
    
        @Test
        public void testInsert() {
           User user = new User();
           user.setId(null);
           user.setUsername("user123");
           user.setEmail("user234@qq.com");
           user.setPassword("123456");
           user.setPhone("134121231");
           this.newUserMapper.insert(user);
        }
    
        @Test
        public void testInsertSelective() {
            User user = new User();
            user.setId(null);
            user.setUsername("user1234");
            user.setPassword("123456");
            this.newUserMapper.insertSelective(user);
        }
    
        @Test
        public void testDelete() {
            User user = new User();
           this.newUserMapper.delete(user);
        }
    
        @Test
        public void testDeleteByPrimaryKey() {
            this.newUserMapper.deleteByPrimaryKey(2L);
        }
    
        @Test
        public void testUpdateByPrimaryKey() {
            User user = new User();
            user.setId(3L);
            user.setUsername("zhangsan");
            user.setPassword("12312123");
            this.newUserMapper.updateByPrimaryKey(user);
        }
    
        @Test
        public void testUpdateByPrimaryKeySelective() {
            User user = new User();
            user.setId(3L);
            user.setUsername("zhangsan");
            user.setPassword("12312123");
            this.newUserMapper.updateByPrimaryKey(user);
        }
    
        @Test
        public void testSelectCountByExample() {
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("password", "123456");
            int selectCountByExample = this.newUserMapper.selectCountByExample(example);
            System.out.println(selectCountByExample);
        }
    
        @Test
        public void testDeleteByExample() {
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("password", "123456");
            this.newUserMapper.deleteByExample(example);
        }
    
        @Test
        public void testSelectByExample() {
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("password", "123456");
            List<User> list = this.newUserMapper.selectByExample(example);
            System.out.println(list.size());
        }
    
        @Test
        public void testUpdateByExampleSelective() {
            User user = new User();
            user.setId(3L);
            user.setPhone("123123123");
            user.setEmail("lkajsdf@laksdf.com");
            user.setPassword("123");
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("password", "12312123");
            this.newUserMapper.updateByExampleSelective(user, example);
        }
    
        @Test
        public void testUpdateByExample() {
            User user = new User();
            user.setId(3L);
            user.setPhone("123123123");
            user.setEmail("lkajsdf@laksdf.com");
            user.setPassword("123");
            Example example = new Example(User.class);
            example.createCriteria().andEqualTo("password", "12312123");
            this.newUserMapper.updateByExample(user, example);
        }
    
    }
  • 相关阅读:
    Postman使用教程
    CAD和ArcGIS转换 矢量配准
    SAP CRM Advanced search和Simple search里Max hit表现行为的差异
    SAP CRM Product simple search的启用步骤
    如何快速定位SAP CRM订单应用(Order Application)错误消息抛出的准确位置
    如何动态修改SAP CRM WebClient UI表格栏的宽度
    如何在SAP CRM WebClient UI里创建web service并使用ABAP消费
    如何处理SAP CRM Web Service错误
    如何使用SAP CRM WebClient UI实现一个类似新浪微博的字数统计器
    如何开启SAP CRM基于WORD模板创建附件的功能
  • 原文地址:https://www.cnblogs.com/sidekick/p/8353761.html
Copyright © 2020-2023  润新知