通用Mapper——tk.mybatis(Example)

通用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接口方法:
  1. 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);
    
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`tk.mybatis.mapper.common.example` 包中提供了一些通用Mapper 方法,其中包括了 `deleteByExample` 方法,用于根据条件删除数据库中的数据。使用该方法时,需要在 Mapper 接口中继承 `tk.mybatis.mapper.common.example.DeleteByExampleMapper` 接口,并传入一个 `Example` 对象作为参数,该对象中包含了删除数据的条件。具体的示例代码如下: ```java public interface UserMapper extends Mapper<User>, DeleteByExampleMapper<User> { } public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public int deleteUserByExample(String name, int age) { // 创建 Example 对象 Example example = new Example(User.class); Example.Criteria criteria = example.createCriteria(); // 设置查询条件 criteria.andEqualTo("name", name); criteria.andEqualTo("age", age); // 调用 deleteByExample 方法删除用户 return userMapper.deleteByExample(example); } } ``` 在上述代码中,我们定义了一个 `UserMapper` 接口,该接口继承了 `Mapper<User>` 和 `DeleteByExampleMapper<User>` 接口,其中 `Mapper<User>` 是 `tk.mybatis.mapper.common.Mapper` 接口的泛型实现,用于提供基本的 CRUD 操作;`DeleteByExampleMapper<User>` 接口则提供了 `deleteByExample` 方法,用于根据条件删除数据库中的数据。在 `UserServiceImpl` 中,我们通过创建一个 `Example` 对象,并设置查询条件,最后调用 `deleteByExample` 方法删除满足条件的用户。其中,`Example` 对象也是 MyBatis 自动生成的,它根据实体类中的属性生成了一些查询条件的方法,我们可以通过 `example.createCriteria()` 方法获取到一个 `Criteria` 对象,并在该对象中设置查询条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值