信誉好的网站建设公司,微信网站,安徽城乡建设部网站首页,开发游戏需要什么条件个人总结三种方式#xff1a;
Xml、queryWrapper、PageHelper第三方组件这三种方式进行查询#xff1b;
方式一#xff1a; xml中联表查询#xff0c;在mapper中传参IPageT和条件Map#xff08;这里用map装参数#xff09;。
代码示例#xff1a; Mapper层 M…个人总结三种方式
Xml、queryWrapper、PageHelper第三方组件这三种方式进行查询
方式一 xml中联表查询在mapper中传参IPageT和条件Map这里用map装参数。
代码示例 Mapper层 Mapper public interface UserInfoMapper extends BaseMapperUserInfo {
IPageUserInfofindUserInfoByConditions(IPageUserInfopage,Param(map)MapString,Object map);
}
Xml文件 表关系user表id和user_scope表中user_id是主外键关系通过这两字段关联两张表查询
select idfindUserInfoByConditions resultMapUserInfoMap SELECT psi.* FROM user_info ui ,user_scope us where ui.idus.user_id if testmap.userName ! null and map.userName ! and ui.user_name like concat(%,#{map.userName},%) /if if testmap.userCoding !null and map.userCoding! and ui.user_coding like concat(%,#{map.userCoding},%) /if if testmap.userType ! null and map.userType! and us.user_type like concat(%,#{map.userType},%) /if /select
Service层
Map map new HashMap();
map.put(“userName”,”james”);
map.put(“userCoding”,”123456”);
map.put(“userType”,”普通用户”);
userInfoMapper.findUserInfoByConditions(page, map)); 方式二
PageHelper第三方组件分页查询最后new分页对象PageInfo返回需要注意设置分页参数和查询语句的顺序问题。 依赖引入 dependency groupIdcom.github.pagehelper/groupId artifactIdpagehelper-spring-boot-starter/artifactId version1.4.6/version /dependency 代码示例 Mapper Mapper public interface UserInfoMapper extends BaseMapperUserInfo {
ListUserInfo findUserInfoList(Param(map) MapString,Object map);
}
Xml
select idfindUserInfoList resultMapUserInfoMap SELECT psi.* FROM user_info ui ,user_scope us where ui.idus.user_id if testmap.userName ! null and map.userName ! and ui.user_name like concat(%,#{map.userName},%) /if if testmap.userCoding !null and map.userCoding! and ui.user_coding like concat(%,#{map.userCoding},%) /if if testmap.userType ! null and map.userType! and us.user_type like concat(%,#{map.userType},%) /if /select
Service层 int pageNumber1;
int pageSize10;
Map map new HashMap();
map.put(“userName”,”james”);
map.put(“userCoding”,”123456”);
map.put(“userType”,”普通用户”);
PageHelper.startPage(pageNumber, pageSize);//这里顺序不能颠倒改行必须放在查询语句前面AOP切面编程即动态代理模式
ListUserInfo userInfoList userInfoMapper.findUserInfoList(map));
PageInfo pageInfo new PageInfo(userInfoList );//该行返回分页对象 方式三
QueryWrapper查询方式这里得注意联合查询时传参的语法问题具体见代码标记的红色部分
代码示例
Map层 Mapper public interface UserInfoMapper extends BaseMapperUserInfo {
IPageUserInfo findUserInfo(IPageUserInfo page,Param(Constants.WRAPPER) QueryWrapperUserInfo wrappers);
}
Xml
select idfindUserInfo resultMapuserInfoMap SELECT ui.* FROM user_info ui left join user_scope us on ui.idus.user_id ${ew.customSqlSegment}//不能在此处前后加where /select
Service层
Int current1;
Int size10;
QueryWrapperUserInfo wrapper new QueryWrapper();
//这里注意区分是哪张表的字段根xml里面别名对应不然如果两张表里面有相同字段//且出现在查询条件里面时会分不清是具体哪张表的字段所以一定得注明别名。
wrapper.like(ui.user_coding, “123455”);
wrapper.like(ui.user_name, “james”);
wrapper.like(us.user_type, “普通用户”)
Page? page new Page(current, size);
userInfoMapper.findUserInfo(page, wrapper) 分析上述三种方式就开发效率而言方式三最快且好用当然如果遇到业务逻辑复杂的则可以通过xml中sql的方式来查。其实就方式三而言如果不用QueryWrapper联表方式查询只想用QueryWrapper的单表方式查询可以将逻辑分层分成两部分查询上述三种方式其sql都是一样的即 SELECT ui.* FROM user_info ui , user_scope us
where ui.idus.user_id and us.user_type like %钢% and ui.user_name like ‘%james%’ and ui.user_coding like ‘%123%’
将该sql分成两部分 select * from user_info where id in (select user_id from user_scope where user_type like ‘%钢%’ ) 对应QueryWrapper代码是 wrapper.like(“user_type”,”普通用户”) ListObject idList userScopeService.list(wrapper);
Wrapper.like(“user_name”,”james”);
Wrapper.like(“user_coding”,”123”);
IPage page userInfoService.page(page,wrapper);
这样也可以达到效果当然具体业务得具体调整拆分。如果有其他方式你分享我整理。