mybatis逆向工程中example用法

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加查询条件,相当where后面的部分

方法 说明
example.setOrderByClause(“字段名 ASC”) 按“字段名”升序排列,DESC为降序
example.setDistinct(false) 去除重复,true为选择不重复的记录
criteria.andXxxIsNull 字段xxx为null
criteria.andXxxIsNotNull 字段xxx不为null
criteria.andXxxEqualTo(value) xxx字段等于value
criteria.andXxxNotEqualTo(value) xxx字段不等于value
criteria.andXxxGreaterThan(value) xxx字段大于value
criteria.andXxxGreaterThanOrEqualTo(value) xxx字段大于等于value
criteria.andXxxLessThan(value) xxx字段小于value
criteria.andXxxLessThanOrEqualTo(value) xxx字段小于等于value
criteria.andXxxIn(List<?>) xxx字段值在List<?>
criteria.andXxxNotIn(List<?>) xxx字段值不在List<?>
criteria.andXxxLike(“%”+value+”%”) xxx字段值为value的模糊查询
criteria.andXxxNotLike(“%”+value+”%”) xxx字段值不为value的模糊查询
criteria.andXxxBetween(value1,value2) xxx字段值在value1和value2之间
criteria.andXxxNotBetween(value1,value2) xxx字段值不在value1和value2之间

selectByExample()

/**
 * select * from user where id = ? and pwd = ?
 */
UserExample example = new UserExample(); 
example.createCriteria().andIdEqualTo(id).andPwdEqualTo(pwd);
User user = userMapper.selectByExample(example);

updateByExample()

/**
 * 更新实体类中所有的字段,没有设置的属性为null
 * update user set id = null, set nickname = '张三', set age = null where id = ?
 * 当然这个sql执行会报错,因为id不能为空
 */
User user = new User();
user.setNickname("张三");

UserExample userExample = new UserExample();
userExample.createCriteria().andIdEqualTo(id);

int res = userService.updateByExampleSelective(user, userExample);

updateByExampleSelective()

/**
 * 实体类没有设置的属性为null,mybatis会使用动态sql过滤掉,不更新该字段
 * update user set nickname = '张三' where id = ?
 */
User user = new User();
user.setNickname("张三");

UserExample userExample = new UserExample();
userExample.createCriteria().andIdEqualTo(id);

int res = userService.updateByExampleSelective(user, userExample);

deleteByExample()

/**
 * delete from user where id = ?
 */
UserExample userExample = new UserExample();
userExample.createCriteria().andIdEqualTo(id);

int res = userService.deleteByExample(userExample);

countByExample()

/**
 * select count(*) from user where nickname = '张三'
 */
UserExample userExample = new UserExample();
userExample.createCriteria().andNicknameEqualTo("张三");

int res = userService.countByExample(userExample);

你可能感兴趣的:(随笔,Java,mybatis,example,自定义查询)