群晖建设网站,厦门网站开发比较大的公司,wordpress取消邮箱注册,湛江市企业网站建设哪家好众所周知#xff0c;Mybatis Plus 封装的 mapper 不支持 join#xff0c;如果需要支持就必须自己去实现。但是对于大部分的业务场景来说#xff0c;都需要多表 join#xff0c;要不然就没必要采用关系型数据库了。
直到前几天#xff0c;偶然碰到了这么一款叫做mybatis-p…众所周知Mybatis Plus 封装的 mapper 不支持 join如果需要支持就必须自己去实现。但是对于大部分的业务场景来说都需要多表 join要不然就没必要采用关系型数据库了。
直到前几天偶然碰到了这么一款叫做mybatis-plus-join的工具后面就简称mpj了使用了一下不得不说真香彻底将我从xml地狱中解放了出来终于可以以类似mybatis-plus中QueryWrapper的方式来进行联表查询了话不多说我们下面开始体验。
引入依赖
首先在项目中引入引入依赖坐标因为mpj中依赖较高版本mybatis-plus中的一些api所以项目建议直接使用高版本。
maven
dependencygroupIdcom.github.yulichang/groupIdartifactIdmybatis-plus-join/artifactIdversion1.2.4/version
/dependency
dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.1/version
/dependency引入相关依赖后在springboot项目中像往常一样正常配置数据源连接信息就可以了。
Gradle
implementation com.github.yulichang:mybatis-plus-join-boot-starter:1.4.4使用
mapper继承MPJBaseMapper (必选)service继承MPJBaseService (可选)serviceImpl继承MPJBaseServiceImpl (可选)
核心类MPJLambdaWrapper和MPJQueryWrapper
MPJLambdaWrapper用法
简单的三表查询
class test {Resourceprivate UserMapper userMapper;void testJoin() {//和Mybatis plus一致MPJLambdaWrapper的泛型必须是主表的泛型并且要用主表的Mapper来调用MPJLambdaWrapperUserDO wrapper new MPJLambdaWrapperUserDO().selectAll(UserDO.class)//查询user表全部字段.select(UserAddressDO::getTel)//查询user_address tel 字段.selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)//别名 t.address AS userAddress.select(AreaDO::getProvince, AreaDO::getCity).leftJoin(UserAddressDO.class, UserAddressDO::getUserId, UserDO::getId).leftJoin(AreaDO.class, AreaDO::getId, UserAddressDO::getAreaId).eq(UserDO::getId, 1).like(UserAddressDO::getTel, 1).gt(UserDO::getId, 5);//连表查询 返回自定义ResultTypeListUserDTO list userMapper.selectJoinList(UserDTO.class, wrapper);//分页查询 需要启用 mybatis plus 分页插件PageUserDTO listPage userMapper.selectJoinPage(new Page(2, 10), UserDTO.class, wrapper);}
}对应sql
SELECT t.id, t.name, t.sex, t.head_img, t1.tel, t1.address AS userAddress,t2.province, t2.city
FROM user t LEFT JOIN user_address t1 ON t1.user_id t.id LEFT JOIN area t2 ON t2.id t1.area_id
WHERE (t.id ? AND t1.tel LIKE ? AND t.id ?)说明:
UserDTO.class 查询结果返回类(resultType)selectAll() 查询指定实体类的全部字段select() 查询指定的字段,支持可变参数,同一个select只能查询相同表的字段selectAs() 字段别名查询,用于数据库字段与业务实体类属性名不一致时使用leftJoin() 参数说明第一个参数: 参与连表的实体类class 第二个参数: 连表的ON字段,这个属性必须是第一个参数实体类的属性 第三个参数: 参与连表的ON的另一个实体类属性默认主表别名是t,其他的表别名以先后调用的顺序使用t1,t2,t3…条件查询,可以查询主表以及参与连接的所有表的字段,全部调用mp原生的方法,正常使用没有sql注入风险
MPJLambdaWrapper 还有很多其他的功能
简单的SQL函数使用https://gitee.com/best_handsome/mybatis-plus-join/wikis/selectFunc()?sort_id4082479ON语句多条件支持https://gitee.com/best_handsome/mybatis-plus-join/wikis/leftJoin?sort_id3496671
等效于ResultMap
resultMap idxxxxxxxx typecom.github.yulichang.join.dto.UserDTOresult propertyid columnid/result propertyname columnname/!--其他属性省略--collection propertyaddressList javaTypejava.util.ListofTypecom.github.yulichang.join.entity.UserAddressDOid propertyid columnmpj_id/result propertyaddress columnaddress/result propertyuserId columnuser_id/!--其他属性省略--/collection
/resultMapMPJLambdaWrapper其他功能
一对一一对多使用https://ylctmh.com/pages/core/lambda/select/selectCollection.html简单的SQL函数使用https://ylctmh.com/pages/core/lambda/select/selectFunc.htmlON语句多条件支持https://ylctmh.com/pages/core/lambda/join/leftJoin.html
分页查询
mpj中也能很好的支持列表查询中的分页功能首先我们要在项目中加入分页拦截器
Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));return interceptor;
}接下来改造上面的代码调用selectJoinPage()方法
public void page() {IPageOrderDto orderPage orderMapper.selectJoinPage(new PageOrderDto(2,10),OrderDto.class,new MPJLambdaWrapperOrder().selectAll(Order.class).select(Product::getUnitPrice).selectAs(User::getName, OrderDto::getUserName).selectAs(Product::getName, OrderDto::getProductName).leftJoin(User.class, User::getId, Order::getUserId).leftJoin(Product.class, Product::getId, Order::getProductId).orderByAsc(Order::getId));orderPage.getRecords().forEach(System.out::println);
}注意在这里需要添加一个分页参数的Page对象我们再执行上面的代码并对日志进行解析查看sql语句 可以看到底层通过添加limit进行了分页同理MPJQueryWrapper也可以这样进行分页。
最后
经过简单的测试个人感觉mpj这款工具在联表查询方面还是比较实用的能更应对项目中不是非常复杂的场景下的sql查询大大提高我们的生产效率。当然在项目的issues中也能看到当前版本中也仍然存在一些问题希望在后续版本迭代中能继续完善。
关注我后续更新更多好用的工具效率天花板 文章转载自: http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.qlhkx.cn.gov.cn.qlhkx.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.supera.com.cn.gov.cn.supera.com.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.rqjxc.cn.gov.cn.rqjxc.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.dpflt.cn.gov.cn.dpflt.cn http://www.morning.rqfzp.cn.gov.cn.rqfzp.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.scjtr.cn.gov.cn.scjtr.cn http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn http://www.morning.webpapua.com.gov.cn.webpapua.com http://www.morning.gwhjy.cn.gov.cn.gwhjy.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.qfqld.cn.gov.cn.qfqld.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.ncrk.cn.gov.cn.ncrk.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.trnl.cn.gov.cn.trnl.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.gwqq.cn.gov.cn.gwqq.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.rbgqn.cn.gov.cn.rbgqn.cn http://www.morning.rfkyb.cn.gov.cn.rfkyb.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.rdmn.cn.gov.cn.rdmn.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.hpprx.cn.gov.cn.hpprx.cn http://www.morning.zqfjn.cn.gov.cn.zqfjn.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.smmby.cn.gov.cn.smmby.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.rzcbk.cn.gov.cn.rzcbk.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.ptlwt.cn.gov.cn.ptlwt.cn http://www.morning.dfkmz.cn.gov.cn.dfkmz.cn http://www.morning.pxwzk.cn.gov.cn.pxwzk.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.mpyry.cn.gov.cn.mpyry.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.rhmpk.cn.gov.cn.rhmpk.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.ycgrl.cn.gov.cn.ycgrl.cn http://www.morning.zzaxr.cn.gov.cn.zzaxr.cn http://www.morning.wknjy.cn.gov.cn.wknjy.cn