通用Mapper——tk.mybatis(简单使用,不包含Example)
example用于添加条件,相当where后面的部分
方法说明
example方法 | 说明 | 示例 |
---|---|---|
setOrderByClause(String orderByClause) | 添加排列条件 | setOrderByClause(“id ASC,index DESC”) |
setDistinct(boolean distinct) | 去除重复,true为选择不重复的记录。 | |
setForUpdate(boolean forUpdate) | true为该数据加锁,事务提交后解锁 | |
selectProperties(String… properties) | 设置要查询的列 | |
excludeProperties(String… properties) | 排除查询字段,优先级低于 selectProperties | |
setCountProperty(String property) | 查询xxx列的条数 | 配合selectCountByExample使用 |
example.createCriteria()方法 | 说明 |
---|---|
andIsNull(String property) | 添加字段xxx为null的条件 |
andIsNotNull(String property) | 添加字段xxx不为null的条件 |
andEqualTo(String property, Object value) | 添加xxx字段等于value条件 |
andNotEqualTo(String property, Object value) | 添加xxx字段不等于value条件 |
andGreaterThan(String property, Object value) | 添加xxx字段大于value条件 |
andGreaterThanOrEqualTo(String property, Object value) | 添加xxx字段大于等于value条件 |
andLessThan(String property, Object value) | 添加xxx字段小于value条件 |
andLessThanOrEqualTo(String property, Object value) | 添加xxx字段小于等于value条件 |
andIn(String property, Iterable values) | 添加xxx字段值在List<?>条件 |
andNotIn(String property, Iterable values) | 添加xxx字段值不在List<?>条件 |
andBetween(String property, Object value1, Object value2) | 添加xxx字段值在value1和value2之间条件 |
andNotBetween(String property, Object value1, Object value2) | 添加xxx字段值不在value1和value2之间条件 |
andLike(String property, String value) | 添加xxx字段值为value的模糊查询条件 |
andNotLike(String property, String value) | 添加xxx字段值不为value的模糊查询条件 |
Mapper接口方法:
-
updateByExampleSelective
该方法有两个参数
参数1:要修改的那条数据所对应的对象
参数2:传入xxxExample,相当于 where 条件// 要修改的数据 User user = new User(); user.setUsername("username"); user.setUserSex("male"); // 修改条件 Example userExample = new Example(User.class); userExample.createCriteria() .andEqualTo("id", 1); // 修改用户id为1的 username 和 usersex 字段的值 userMapper.updateByExampleSelective(user, userExample);