Mybatis Plus Join使用方法示例詳解
1、pom文件
不引入mybatis的任何內(nèi)容,防止包沖突
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-plus-join-boot-starter</artifactId> <version>1.5.3</version> </dependency>
2、yaml配置文件
mybatis-plus-join: #是否打印 mybatis plus join banner 默認(rèn)true banner: true #全局啟用副表邏輯刪除(默認(rèn)true) 關(guān)閉后關(guān)聯(lián)查詢不會(huì)加副表邏輯刪除 sub-table-logic: true #攔截器MappedStatement緩存(默認(rèn)true) ms-cache: true #表別名(默認(rèn) t) table-alias: t #副表邏輯刪除條件的位置,支持where、on #默認(rèn)ON (1.4.7.2及之前版本默認(rèn)為where) logic-del-type: on
3、分頁(yè)插件
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * mybatis-plus配置 * * @author Mark sunlightcs@gmail.com */ @Configuration public class MybatisPlusConfig { /** * 添加分頁(yè)插件 */ @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多個(gè)插件, 切記分頁(yè)最后添加 // 如果有多數(shù)據(jù)源可以不配具體類型, 否則都建議配上具體的 DbType return interceptor; } }
4、示例代碼:
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("address") public class Address { @TableId private Long id; private Long userId; private String city; private String address; } import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("user") public class User { @TableId private Long id; private String name; private Integer age; private String email; } import lombok.Data; import lombok.ToString; @Data @ToString public class UserDTO { private Long id; private String name; private Integer age; private String email; private String city; private String address; } import com.github.yulichang.base.MPJBaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface AddressMapper extends MPJBaseMapper<Address> { } import com.github.yulichang.base.MPJBaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends MPJBaseMapper<User> { }
5、測(cè)試代碼
import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.wrapper.MPJLambdaWrapper; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @SpringBootTest(classes = RenrenApplication.class) @RunWith(SpringRunner.class) @Slf4j public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查詢user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List<UserDTO> userList = userMapper.selectJoinList(UserDTO.class, wrapper); userList.forEach(System.out::println); //分頁(yè)查詢 (需要啟用 mybatis plus 分頁(yè)插件) Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 2), UserDTO.class, wrapper); log.info("分頁(yè)查詢結(jié)果:{}", JSONUtil.toJsonStr(listPage)); } }
class test { @Resource private UserMapper userMapper; void testJoin() { //和Mybatis plus一致,MPJLambdaWrapper的泛型必須是主表的泛型,并且要用主表的Mapper來(lái)調(diào)用 MPJLambdaWrapper<UserDO> wrapper = JoinWrappers.lambda(UserDO.class) .selectAll(UserDO.class)//查詢user表全部字段 .select(UserAddressDO::getTel)//查詢user_address tel 字段 .selectAs(UserAddressDO::getAddress, UserDTO::getUserAddress)//別名 .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); //連表查詢 返回自定義ResultType List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper); //分頁(yè)查詢 (需要啟用 mybatis plus 分頁(yè)插件) Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper); } }
6、和PageHelper結(jié)合
6.1引入pom文件
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>com.github.yulichang</groupId> <artifactId>mybatis-plus-join-boot-starter</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.6</version> </dependency>
6.2 PageHelper配置
pagehelper: helper-dialect: mysql # 數(shù)據(jù)庫(kù)方言 reasonable: true # 頁(yè)碼越界時(shí)自動(dòng)修正 support-methods-arguments: true # 支持接口參數(shù)分頁(yè) params: count=countSql
6.3 測(cè)試代碼:
import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @SpringBootTest(classes = Application.class) @RunWith(SpringRunner.class) @Slf4j public class SampleTest { @Autowired private UserMapper userMapper; @Test public void testSelect() { MPJLambdaWrapper<User> wrapper = new MPJLambdaWrapper<User>() .selectAll(User.class)//查詢user表全部字段 .select(Address::getCity, Address::getAddress) .leftJoin(Address.class, Address::getUserId, User::getId); List<UserDTO> userList = userMapper.selectJoinList(UserDTO.class, wrapper); log.info("查詢結(jié)果:{}", JSONUtil.toJsonStr(userList)); PageHelper.startPage(1, 2); List<UserDTO> userList1 = userMapper.selectJoinList(UserDTO.class, wrapper); PageInfo<UserDTO> pageInfo = new PageInfo<>(userList1); log.info("PageHelper 分頁(yè)查詢結(jié)果:{}", JSONUtil.toJsonStr(pageInfo)); PageHelper.clearPage(); //分頁(yè)查詢 (需要啟用 mybatis plus 分頁(yè)插件) Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 4), UserDTO.class, wrapper); log.info("分頁(yè)查詢結(jié)果:{}", JSONUtil.toJsonStr(listPage)); } }
到此這篇關(guān)于Mybatis Plus Join使用方法示例詳解的文章就介紹到這了,更多相關(guān)Mybatis Plus Join使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot之如何正確、安全的關(guān)閉服務(wù)
這篇文章主要介紹了SpringBoot之如何正確、安全的關(guān)閉服務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java基礎(chǔ)教程之理解Annotation詳細(xì)介紹
這篇文章主要介紹了Java基礎(chǔ)教程之理解Annotation詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-01-01JDBC中Statement和Preparement的使用講解
今天小編就為大家分享一篇關(guān)于JDBC中Statement和Preparement的使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01自定義starter引發(fā)的線上事故記錄復(fù)盤(pán)
這篇文章主要為大家介紹了自定義starter引發(fā)的線上事故記錄復(fù)盤(pán),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05java實(shí)現(xiàn)學(xué)生成績(jī)信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生成績(jī)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07java面向國(guó)際化項(xiàng)目開(kāi)發(fā)需遵循的命名規(guī)范
這篇文章主要為大家介紹了在參與開(kāi)發(fā)國(guó)際化項(xiàng)目時(shí)需遵循的java命名規(guī)范,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03mybatis中bind標(biāo)簽和concat的使用說(shuō)明
這篇文章主要介紹了mybatis中bind標(biāo)簽和concat的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12