mybatis-plus模糊查询

模糊查询方法一

control

	//模糊查询方法一
    @RequestMapping(value = "/testTask", method = RequestMethod.GET)
    public ResponseObj> testTask(@RequestParam("name") String name) {


        List userInfos = userInfoMapper.selectList(new EntityWrapper<>(userInfo).like("firstname", name)
                .or().like("lastname", name));

        return new ResponseObj<>(userInfos, null);


    }

new EntityWrapper<>(userInfo).like(“firstname”, name).or().like(“lastname”, name)
userInfo是需要模糊查询的表对应的model,firstname和lastname则是需要模糊查询的字段,or()是指两个条件或者的关系,在sql中执行的语句是:

SELECT * FROM users WHERE (firstname LIKE '%海%' OR lastname LIKE '%海%')

这里selectList方法是mybatis-plus中的

模糊查询方法二

这个是通过传入对象进行模糊查询

	//模糊查询方法二
    @RequestMapping(value = "/testTask1", method = RequestMethod.GET)
    public ResponseObj> testTask1(@ModelAttribute UserInfo userInfo) {

        List list = userInfoMapper.list(new EntityWrapper<>(userInfo));

        List listAll = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (UserInfo info : list) {

                List list1 = timeEntriesMapper.list(info.getId());
                listAll.addAll(list1);

            }
        }
        System.out.println(listAll.size());

        // 按userId升序、username降序、birthDate升序排序
        String[] sortNameArr = {"spentOn", "fullName", "hoursSum"};
        boolean[] isAscArr = {false, true, true};
        ListUtils.sort(listAll, sortNameArr, isAscArr);
        System.out.println("\n--------按按userId升序、username降序、birthDate升序排序(如果userId相同,则按照username降序," +
                "如果username相同,则按照birthDate升序)------------------");

        return new ResponseObj<>(listAll, null);

UserInfoMapper

@Mapper
public interface UserInfoMapper extends BaseMapper {

    List list(@Param("ew") Wrapper wrapper);
}

UserInfoMapper.xml


你可能感兴趣的:(mybatis-plus,模糊查询,like)