最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis Plus Join使用方法示例詳解

 更新時間:2025年06月11日 14:30:31   作者:weixin_42502300  
這篇文章主要介紹了Mybatis Plus Join使用方法示例詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友一起看看吧

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 默認true
    banner: true
    #全局啟用副表邏輯刪除(默認true) 關(guān)閉后關(guān)聯(lián)查詢不會加副表邏輯刪除
    sub-table-logic: true
    #攔截器MappedStatement緩存(默認true)
    ms-cache: true
    #表別名(默認 t)
    table-alias: t
    #副表邏輯刪除條件的位置,支持where、on
    #默認ON (1.4.7.2及之前版本默認為where)
    logic-del-type: on

3、分頁插件

 
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 {
    /**
     * 添加分頁插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 如果配置多個插件, 切記分頁最后添加
        // 如果有多數(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、測試代碼

 
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);
        //分頁查詢 (需要啟用 mybatis plus 分頁插件)
        Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 2), UserDTO.class, wrapper);
        log.info("分頁查詢結(jié)果:{}", JSONUtil.toJsonStr(listPage));
    }
}
class test {
    @Resource
    private UserMapper userMapper;
    void testJoin() {
        //和Mybatis plus一致,MPJLambdaWrapper的泛型必須是主表的泛型,并且要用主表的Mapper來調(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);
        //分頁查詢 (需要啟用 mybatis plus 分頁插件)
        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ù)庫方言
    reasonable: true         # 頁碼越界時自動修正
    support-methods-arguments: true  # 支持接口參數(shù)分頁
    params: count=countSql

6.3 測試代碼:

 
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 分頁查詢結(jié)果:{}", JSONUtil.toJsonStr(pageInfo));
        PageHelper.clearPage();
        //分頁查詢 (需要啟用 mybatis plus 分頁插件)
        Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 4), UserDTO.class, wrapper);
        log.info("分頁查詢結(jié)果:{}", JSONUtil.toJsonStr(listPage));
    }
}

到此這篇關(guān)于Mybatis Plus Join使用方法示例詳解的文章就介紹到這了,更多相關(guān)Mybatis Plus Join使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java使用apache commons連接ftp修改ftp文件名失敗原因

    java使用apache commons連接ftp修改ftp文件名失敗原因

    這篇文章主要介紹了java使用apache commons連接ftp修改ftp文件名失敗原因解析,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • 在Java中int和byte[]的相互轉(zhuǎn)換

    在Java中int和byte[]的相互轉(zhuǎn)換

    這篇文章主要介紹了在Java中int和byte[]的相互轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量

    linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量

    在Linux系統(tǒng)中,配置JDK環(huán)境變量是非常重要的,它可以讓你在終端中直接使用Java命令,這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • maven多模塊項目單獨打包指定模塊jar包方式

    maven多模塊項目單獨打包指定模塊jar包方式

    這篇文章主要介紹了maven多模塊項目單獨打包指定模塊jar包方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Java中Session的詳解

    Java中Session的詳解

    這篇文章主要介紹了了解java中的session的相關(guān)問題,什么是session,session怎么用等,具有一定參考價值,需要的朋友可以了解下。
    2021-10-10
  • Mybatis中Like的三種使用解讀

    Mybatis中Like的三種使用解讀

    這篇文章主要介紹了Mybatis中Like的三種使用解讀,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java打印九九乘法表代碼詳情

    Java打印九九乘法表代碼詳情

    這篇文章主要介紹了Java打印九九乘法表,使用了雙重for循環(huán),使用do{}while()實現(xiàn)打印九九乘法表這些好玩的語法實現(xiàn),感興趣的小伙伴可參考下面文章內(nèi)容
    2021-09-09
  • ES模糊查詢失效的坑以及解決方案

    ES模糊查詢失效的坑以及解決方案

    ES的查詢原理是按分詞建立索引,根據(jù)要保存的內(nèi)容先分詞,然后按照分詞的結(jié)果建立索引,這篇文章主要給大家介紹了關(guān)于ES模糊查詢失效的坑及解決方案的相關(guān)資料,需要的朋友可以參考下
    2023-09-09
  • 關(guān)于idea剛打開時瘋狂報錯的問題

    關(guān)于idea剛打開時瘋狂報錯的問題

    這篇文章主要介紹了關(guān)于idea剛打開時瘋狂報錯的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 通過Java修改游戲存檔的實現(xiàn)思路

    通過Java修改游戲存檔的實現(xiàn)思路

    這篇文章主要介紹了通過Java修改游戲存檔的實現(xiàn)思路,實現(xiàn)方法也很簡單,因為植物大戰(zhàn)僵尸游戲的數(shù)據(jù)文件存儲在本地的存儲位置是已知的,因此我們可以將實現(xiàn)過程拆分為三個步驟,需要的朋友可以參考下
    2021-10-10

最新評論

乡宁县| 双桥区| 台东市| 商丘市| 湄潭县| 张家界市| 吴桥县| 阿鲁科尔沁旗| 华容县| 乌兰察布市| 威远县| 南皮县| 财经| 凌源市| 纳雍县| 九江市| 民权县| 凉城县| 芮城县| 谷城县| 福建省| 通州市| 江源县| 江陵县| 崇阳县| 闵行区| 武安市| 垦利县| 延川县| 饶河县| 宁南县| 军事| 星子县| 罗平县| 保德县| 怀仁县| 祁东县| 清徐县| 武邑县| 中牟县| 株洲县|