目录
- 模糊查询,使用BaseMapper的selectList方法,传入参数QueryWrapper
- 模糊查询,使用BaseMapper接口中的selectPage方法,传入参数Page和Wrappers
- 使用自定义的mySelectMap方法
- 使用自定义的rowBoundList方法
代码案例
- 模糊查询,使用BaseMapper的selectList方法,传入参数QueryWrapper
@GetMapping("/test4")
@ResponseBody
public String test4() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// like 表示包含某个字符
// likeLeft 表示以某个字符结尾
// likeRight 表示以某个字符开头的
queryWrapper.likeRight("name","g");
List<User> users = userDao.selectList(queryWrapper);
users.forEach(user-> System.out.println("user = " + user));
return "success";
}
- 模糊查询,使用BaseMapper接口中的selectPage方法,传入参数Page和Wrappers
@RequestMapping("/test1")
@ResponseBody
public String test1(){
// 分页查询
Page<User> page = new Page<>(1, 5);
// 升序排序
page.addOrder(OrderItem.asc("age"));
// age为20,根据name模糊查询
Page<User> userIPage = mapper.selectPage(page, Wrappers.<User>lambdaQuery().eq(User::getAge, 20).like(User::getName, "Jack"));
// 打印出查询结果
userIPage.getRecords().forEach(System.out::println);
return "success";
}
# 控制台
SELECT id, name, age, email FROM user WHERE (age = ? AND name LIKE ?) ORDER BY age ASC LIMIT ?
List<User> mySelectMap(Map<String, Object> param);
<select id="mySelectMap" resultType="com.ychen.mybatis.entity.User">
select * from user
<where>
<if test="name!=null and name!=''">
name like #{name}
</if>
</where>
</select>
@RequestMapping("/test7")
@ResponseBody
public String test7(){
mapper.mySelectMap(Maps.newHashMap("name", "%a%")).forEach(System.out::println);
return "success";
}
# 控制台
select * from user WHERE name like ?
List<User> rowBoundList(RowBounds rowBounds, Map<String, Object> map);
<select id="rowBoundList" resultType="com.ychen.mybatis.entity.User">
select * from user
<where>
<if test="name!=null and name!=''">
name like #{name}
</if>
</where>
</select>
@RequestMapping("/test11")
@ResponseBody
public String test11(){
RowBounds rowBounds = new RowBounds(0, 5);
List<User> list = mapper.rowBoundList(rowBounds, Maps.newHashMap("name", "%"));
System.out.println("list.size=" + list.size());
return "success";
}
# 控制台
select * from user WHERE name like ?