Mybatis分页多表多条件查询

个人总结三种方式:

Xml、queryWrapper、PageHelper第三方组件这三种方式进行查询;

方式一:
xml中联表查询,在mapper中传参IPage<T>和条件Map(这里用map装参数)。

代码示例:
Mapper层
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

IPage<UserInfo>findUserInfoByConditions(IPage<UserInfo>page,@Param("map")Map<String,Object> map);

}

Xml文件
表关系:user表id和user_scope表中user_id是主外键关系,通过这两字段关联两张表查询

<select id="findUserInfoByConditions" resultMap="UserInfoMap">
   SELECT psi.* FROM user_info ui ,user_scope us
   where ui.id=us.user_id
   <if test="map.userName != null and map.userName !='' ">
      and ui.user_name like concat('%',#{map.userName},'%')
   </if>
   <if test="map.userCoding !=null and map.userCoding!='' ">
      and ui.user_coding like concat('%',#{map.userCoding},'%')
   </if>
   <if test="map.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>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.4.6</version>
</dependency>
代码示例:
Mapper
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

List<UserInfo> findUserInfoList(@Param("map") Map<String,Object> map);

}

Xml

<select id="findUserInfoList" resultMap="UserInfoMap">
   SELECT psi.* FROM user_info ui ,user_scope us
   where ui.id=us.user_id
   <if test="map.userName != null and map.userName !='' ">
      and ui.user_name like concat('%',#{map.userName},'%')
   </if>
   <if test="map.userCoding !=null and map.userCoding!='' ">
      and ui.user_coding like concat('%',#{map.userCoding},'%')
   </if>
   <if test="map.userType != null and map.userType!='' ">
      and us.user_type like concat('%',#{map.userType},'%')
   </if>
</select>

Service层
int pageNumber=1;

int pageSize=10;

Map map = new HashMap<>();

map.put(“userName”,”james”);

map.put(“userCoding”,”123456”);

map.put(“userType”,”普通用户”);

PageHelper.startPage(pageNumber, pageSize);//这里顺序不能颠倒,改行必须放在查询语句前面,AOP切面编程,即动态代理模式

List<UserInfo> userInfoList = userInfoMapper.findUserInfoList(map));

PageInfo pageInfo = new PageInfo(userInfoList );//该行返回分页对象

方式三:

QueryWrapper查询方式,这里得注意联合查询时,传参的语法问题,具体见代码标记的红色部分

代码示例:

Map层
@Mapper
public interface UserInfoMapper extends BaseMapper<UserInfo> {

IPage<UserInfo> findUserInfo(IPage<UserInfo> page,@Param(Constants.WRAPPER) QueryWrapper<UserInfo> wrappers);

}

Xml

<select id="findUserInfo" resultMap="userInfoMap">
   SELECT ui.* FROM `user_info` ui left join user_scope us
   on ui.id=us.user_id
   ${ew.customSqlSegment}//不能在此处前后加where
</select>

Service层

Int current=1;

Int size=10;

QueryWrapper<UserInfo> 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.id=us.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”,”普通用户”)
List<Object> idList = userScopeService.list(wrapper);

Wrapper.like(“user_name”,”james”);

Wrapper.like(“user_coding”,”123”);

IPage page = userInfoService.page(page,wrapper);

这样也可以达到效果,当然具体业务得具体调整拆分。如果有其他方式,你分享我整理。

  • 16
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用MyBatis提供的分页插件PageHelper进行分页查询,同时可以在Mapper.xml文件中使用动态SQL进行多条件查询和in查询。以下是一个例子: ``` <!-- Mapper.xml文件中的查询语句 --> <select id="findUsers" resultMap="userResultMap"> SELECT * FROM users <where> <if test="name != null and name != ''"> AND name = #{name} </if> <if test="age != null"> AND age = #{age} </if> <if test="ids != null and ids.size() > 0"> AND id IN <foreach collection="ids" item="id" open="(" separator="," close=")"> #{id} </foreach> </if> </where> </select> ``` 在Java代码中,使用PageHelper.startPage方法设置分页参数,并调用Mapper中的findUsers方法进行查询: ``` // 设置分页参数,第一页,每页2条数据 PageHelper.startPage(1, 2); // 构造查询条件 Map<String, Object> params = new HashMap<>(); params.put("name", "张三"); params.put("age", 20); List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); params.put("ids", ids); // 调用Mapper中的findUsers方法进行查询 List<User> users = userMapper.findUsers(params); // 获取分页信息 PageInfo<User> pageInfo = new PageInfo<>(users); System.out.println("总记录数:" + pageInfo.getTotal()); System.out.println("总页数:" + pageInfo.getPages()); System.out.println("当前页码:" + pageInfo.getPageNum()); System.out.println("每页记录数:" + pageInfo.getPageSize()); System.out.println("当前页的记录数:" + pageInfo.getSize()); System.out.println("当前页的第一条记录:" + pageInfo.getStartRow()); System.out.println("当前页的最后一条记录:" + pageInfo.getEndRow()); ``` 注意:在使用PageHelper进行分页查询时,需要在MyBatis的配置文件中配置分页插件: ``` <!-- MyBatis配置文件 --> <configuration> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"/> </plugins> </configuration> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焱墩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值