欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis Plus Join使用方法示例詳解

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

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)文章

最新評(píng)論