通用mapper
<!-- 通用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));
}