码迷,mamicode.com
首页 > 移动开发 > 详细

通用mapper

时间:2020-05-27 10:31:16      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:amp   extend   username   tar   min   ble   result   column   ring   

<!--        通用mapper依赖-->
<!--        <dependency>-->
<!--            <groupId>tk.mybatis</groupId>-->
<!--            <artifactId>mapper-spring-boot-starter</artifactId>-->
<!--            <version>2.1.5</version>-->
<!--        </dependency>-->

实体user:

@Table(name="user") // 配置实体映射的表名
public class User implements Serializable {
    private static final long serialVersionUID = -37900582537861695L;

    @Id // 必须配置不配置,如果使用通过ID查询的时候会把所有字段当作条件进行查询
    private Integer id;

    private String userName;

    private String address;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

 

 

mapper:

@Component
@org.apache.ibatis.annotations.Mapper
public interface UserMapper extends Mapper<User> {

}

测试test:

public class MapperTest extends ApplicationTests {
    @Autowired
    private UserMapper userMapper;

    ///////////////////////// 查询 测试 /////////////////////
    // 测试根据ID查询,必须在注解字段标记@Id
    @Test
    public void testSelectById() throws Exception {
        List<User> users = userMapper.selectAll();
        convertToJson(users);
    }

    // 测试根据指定的列进行查询
    @Test
    public void testSelectByColumns() throws Exception {
        // 创建查询条件对象
        Example example = new Example(User.class);
        example.and().andEqualTo("userName","admin")
                .andEqualTo("address","广州");
       /* Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userName","牧*人")
                .andEqualTo("updateTime","2020-04-21 23:42:13.0");*/
        User user = userMapper.selectOneByExample(example);
        convertToJson(user);
    }

public void convertToJson(Object obj) {
System.out.println("result===>"+ JSON.toJSONString(obj, true));
}
 

 

通用mapper

标签:amp   extend   username   tar   min   ble   result   column   ring   

原文地址:https://www.cnblogs.com/alomsc/p/12970944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!