Mybatis Plus Join使用方法示例详解
作者:weixin_42502300
这篇文章主要介绍了Mybatis Plus Join使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友一起看看吧
1、pom文件
不引入mybatis的任何内容,防止包冲突
<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) 关闭后关联查询不会加副表逻辑删除
sub-table-logic: true
#拦截器MappedStatement缓存(默认true)
ms-cache: true
#表别名(默认 t)
table-alias: t
#副表逻辑删除条件的位置,支持where、on
#默认ON (1.4.7.2及之前版本默认为where)
logic-del-type: on3、分页插件
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)); // 如果配置多个插件, 切记分页最后添加
// 如果有多数据源可以不配具体类型, 否则都建议配上具体的 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("分页查询结果:{}", JSONUtil.toJsonStr(listPage));
}
}class test {
@Resource
private UserMapper userMapper;
void testJoin() {
//和Mybatis plus一致,MPJLambdaWrapper的泛型必须是主表的泛型,并且要用主表的Mapper来调用
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结合
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 # 数据库方言
reasonable: true # 页码越界时自动修正
support-methods-arguments: true # 支持接口参数分页
params: count=countSql6.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("查询结果:{}", JSONUtil.toJsonStr(userList));
PageHelper.startPage(1, 2);
List<UserDTO> userList1 = userMapper.selectJoinList(UserDTO.class, wrapper);
PageInfo<UserDTO> pageInfo = new PageInfo<>(userList1);
log.info("PageHelper 分页查询结果:{}", JSONUtil.toJsonStr(pageInfo));
PageHelper.clearPage();
//分页查询 (需要启用 mybatis plus 分页插件)
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 4), UserDTO.class, wrapper);
log.info("分页查询结果:{}", JSONUtil.toJsonStr(listPage));
}
}到此这篇关于Mybatis Plus Join使用方法示例详解的文章就介绍到这了,更多相关Mybatis Plus Join使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
