前言
- MyBatis-Plus-Join (简称 MPJ)是一个 MyBatis-Plus的增强工具,在 MyBatis-Plus 的基础上只做增强不做改变,为简化开发、提高效率而生。
- 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 无感引入, 支持MP风格的查询, 您会MP就会MPJ, 无需额外的学习成本
- 兼容MP的别名、逻辑删除、枚举列、TypeHandle列等特性
- 支持注解形式一对一、一对多和连表查询形式的一对一和一对多
官网:mybatis-plus-join官网
一、新建SpringBoot项目
1.1、自选依赖
二、 mybatis-plus-join
2.1、导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.7</version>
</dependency>
<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.4.11</version>
</dependency>
2.2、修改配置文件
mybatis-plus-join配置文件如下:
# mybatis-plus配置
mybatis-plus:
# 启动检查MyBatis配置文件
check-config-location: false
# MyBatis配置文件位置
config-location:
# MyBaits别名包扫描路径
type-aliases-package: com.ui.springbootmybatisplusjoin.mybatis.entity
# Mapper所对应的XML文件位置 默认【classpath*:/mapper/**/*.xml】
mapper-locations: classpath:mappers/*xml
# TypeHandler扫描路径
type-handlers-package:
configuration:
# 日志打印
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 是否开启自动驼峰命名规则
map-underscore-to-camel-case: true
# 开启Mybatis二级缓存,默认为true
cache-enabled: true
global-config:
# 控制台mybatis-plus的logo
banner: true
db-config:
# 全局默认主键类型
id-type: auto
# 逻辑删除配置
logic-delete-field: deleted
logic-delete-value: 1
logic-not-delete-value: 0
2.3、实体类
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @ClassName User
* @Description 描述
* @Author 轸...
* @Date 2024/9/28 20:15
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
@TableId(value = "user_id",type = IdType.AUTO)
private Long userId;
@TableField("user_name")
private String userName;
@TableField("user_account")
private String userAccount;
@TableField("user_pwd")
private String userPwd;
@TableField("user_tel")
private String userTel;
@TableField("clazz_id")
private Integer clazzId;
@TableField(exist = false)
private String clazzName;
}
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ClassName Log
* @Description 描述
* @Author 轸...
* @Date 2024/9/28 20:15
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("clazz")
public class Clazz {
@TableId(value = "clazz_id",type = IdType.AUTO)
private Long clazzId;
@TableField("clazz_name")
private String clazzName;
}
2.4、mapper 配置 mapper 扫描路径
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.ui.springbootmybatisplusjoin.demos.mapper")
public class SpringBootMybatisPlusJoinApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisPlusJoinApplication.class, args);
}
}
2.5、service 以及 serviceImpl
三、mybatis-plus-join 分页联查
3.1、增加Vo实体类 分页参数
3.2、分页列表及联查业务层
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ui.springbootmybatisplusjoin.demos.mapper.UserMapper;
import com.ui.springbootmybatisplusjoin.demos.pojo.Clazz;
import com.ui.springbootmybatisplusjoin.demos.pojo.User;
import com.ui.springbootmybatisplusjoin.demos.pojo.Vo.UserVo;
import com.ui.springbootmybatisplusjoin.demos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @ClassName UserServiceImpl
* @Description 描述
* @Author 轸...
* @Date 2024/9/28 20:22
*/
@Service
public class UserServiceImpl extends MPJBaseServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
/**
* mybatis-plus-join 联查及分页
* @param userVo
* @return
*/
@Override
public Page<User> pageList(UserVo userVo) {
// SELECT t.*,t1.clazz_name FROM user t LEFT JOIN clazz t1 ON t.clazz_id = t1.clazz_id LIMIT ?,?
MPJLambdaWrapper<User> lambdaWrapper = new MPJLambdaWrapper<>();
lambdaWrapper.selectAll(User.class)
.select(Clazz::getClazzName)
.leftJoin(Clazz.class,Clazz::getClazzId,User::getClazzId);
Page<User> userPage = new Page<>(userVo.getPageNum(), userVo.getPageSize());
return userMapper.selectJoinPage(userPage, User.class,lambdaWrapper);
}
}
3.3、控制层
四、以上步骤 启动执行方法时会报一个com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: mapper not find by class <Clazz> , add mapper and extends BaseMapper<T> or MPJBaseMapper<T>这样的错误
这个错误是因为 找不到联查表的mapper
所以我们应该写一个 clazzMapper
import com.github.yulichang.base.MPJBaseMapper;
import com.ui.springbootmybatisplusjoin.demos.pojo.Clazz;
/**
* @ClassName ClazzMapper
* @Description 描述
* @Author 轸...
* @Date 2024/9/28 20:53
*/
public interface ClazzMapper extends MPJBaseMapper<Clazz> {
}
六、测试
本文转载自: https://blog.csdn.net/X_xxxx7/article/details/142620774
版权归原作者 七七七7 所有, 如有侵权,请联系我们删除。
版权归原作者 七七七7 所有, 如有侵权,请联系我们删除。