RuoYi-Cloud增加MyBatisPlus
📔 千寻简笔记介绍
千寻简笔记已开源,Gitee与GitHub搜索chihiro-notes
,包含笔记源文件.md
,以及PDF版本方便阅读,且是用了精美主题,阅读体验更佳,如果文章对你有帮助请帮我点一个Star
~
- 更新:
支持在线阅读所有发布文章,且根据发布日期分类。
@[toc]
简介
Mybatis-Plus
是在Mybatis
的基础上进行扩展,只做增强不做改变,可以兼容Mybatis
原生的特性。同时支持通用CRUD操作、多种主键策略、分页、性能分析、全局拦截等。极大帮助我们简化开发工作。
前后端分离看若依官方文章搜索:集成mybatisplus实现mybatis增强
实现代码
1.1 pom.xml
引入依赖
注释MyBatis依赖,增加 MyBatis-Plus依赖
<!-- 注释配置 -->
<!-- Mybatis 依赖配置 -->
<!-- <dependency>-->
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!-- <version>${spring-boot.mybatis}</version>-->
<!-- </dependency>-->
<!-- 新增配置 -->
<mybatis-plus.version>3.5.2</mybatis-plus.version>
<jasypt.version>2.1.2</jasypt.version>
<!-- mybatis-plus 增强CRUD -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-annotation</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
1.2 新增配置类文件
新建com.ruoyi.common.core.config.MybatisPlusConfig.java
package com.ruoyi.common.core.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@MapperScan({
"com.ruoyi.**.mapper","com.chihiro.**.mapper"})
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
}
在ruoyi-common-core
模块pom.xml
增加依赖引入
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
1.3 Nacos配置
在nacos每个模块application-system.yml
修改配置:
主要修改:mybatis:
>>mybatis-plus:
,每个使用到mybatis配置的模块都需要修改
原文
# mybatis配置
mybatis:
# 搜索指定包别名
typeAliasesPackage: com.ruoyi.system
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mapper/**/*.xml
修改为
# MyBatis Plus配置
mybatis-plus:
# 搜索指定包别名
typeAliasesPackage: com.ruoyi.system
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mapper/**/*.xml
# 加载全局的配置文件:下面都是可选
configLocation: classpath:mybatis/mybatis-config.xml
global-config:
db-config:
logic-delete-value: 2 # 逻辑已删除值(默认为 2)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
1.4 mybatis-config.xml
参考
在resources
下创建mybatis/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局参数 -->
<settings>
<!-- 使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 允许JDBC 支持自动生成主键 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 配置默认的执行器.SIMPLE就是普通执行器;REUSE执行器会重用预处理语句(prepared statements);BATCH执行器将重用语句并执行批量更新 -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 指定 MyBatis 所用日志的具体实现 -->
<setting name="logImpl" value="SLF4J" />
<!-- 使用驼峰命名法转换字段 -->
<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> -->
</settings>
</configuration>
1.5 修改代码生成模板
随带优化了生成代码模板,更新如下
- 适配MybatisPlus代码生成
- 生成swagger文档
1 controller.java.vm
项目路径:ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm
package ${
packageName}.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import {
mathJaxContainer[0]}{
ClassName};
import {
mathJaxContainer[1]}{
ClassName}Service;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
#if({
mathJaxContainer[2]}table.sub)
import com.ruoyi.common.core.web.page.TableDataInfo;
#elseif($table.tree)
#end
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
* ${functionName}Controller
*
* @author ${author}
* @date ${datetime}
*/
@Api("${functionName}")
@RestController
@RequestMapping("/${businessName}")
public class ${
ClassName}Controller extends BaseController
{
@Autowired
private I{
mathJaxContainer[3]}{
className}Service;
/**
* 查询${functionName}列表
*/
@ApiOperation("查询${functionName}列表")
@RequiresPermissions("${permissionPrefix}:list")
@GetMapping("/list")
#if({
mathJaxContainer[4]}table.sub)
public TableDataInfo list({
mathJaxContainer[5]}{
className})
{
startPage();
List<{
mathJaxContainer[6]}{
className}Service.select{
mathJaxContainer[7]}{
className});
return getDataTable(list);
}
#elseif($table.tree)
public AjaxResult list({
mathJaxContainer[8]}{
className})
{
List<{
mathJaxContainer[9]}{
className}Service.select{
mathJaxContainer[10]}{
className});
return AjaxResult.success(list);
}
#end
/**
* 导出${functionName}列表
*/
@ApiOperation("导出${functionName}列表")
@RequiresPermissions("${permissionPrefix}:export")
@Log(title = "${functionName}", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, {
mathJaxContainer[11]}{
className})
{
List<{
mathJaxContainer[12]}{
className}Service.select{
mathJaxContainer[13]}{
className});
ExcelUtil<{
mathJaxContainer[14]}{
ClassName}>(${
ClassName}.class);
util.exportExcel(response, list, "${functionName}数据");
}
/**
* 获取${functionName}详细信息
*/
@ApiOperation("获取${functionName}详细信息")
@RequiresPermissions("${permissionPrefix}:query")
@GetMapping(value = "/{${pkColumn.javaField}}")
public AjaxResult getInfo(@PathVariable("{
mathJaxContainer[15]}{
pkColumn.javaType} ${
pkColumn.javaField})
{
return AjaxResult.success({
mathJaxContainer[16]}{
ClassName}By{
mathJaxContainer[17]}{
pkColumn.javaField}));
}
/**
* 新增${functionName}
*/
@ApiOperation("新增${functionName}")
@RequiresPermissions("${permissionPrefix}:add")
@Log(title = "${functionName}", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody {
mathJaxContainer[18]}{
className})
{
return toAjax({
mathJaxContainer[19]}{
ClassName}(${
className}));
}
/**
* 修改${functionName}
*/
@ApiOperation("修改${functionName}")
@RequiresPermissions("${permissionPrefix}:edit")
@Log(title = "${functionName}", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody {
mathJaxContainer[20]}{
className})
{
return toAjax({
mathJaxContainer[21]}{
ClassName}(${
className}));
}
/**
* 删除${functionName}
*/
@ApiOperation("删除${functionName}")
@RequiresPermissions("${permissionPrefix}:remove")
@Log(title = "${functionName}", businessType = BusinessType.DELETE)
@DeleteMapping("/{${pkColumn.javaField}s}")
public AjaxResult remove(@PathVariable {
mathJaxContainer[22]}{
pkColumn.javaField}s)
{
return toAjax({
mathJaxContainer[23]}{
ClassName}By{
mathJaxContainer[24]}{
pkColumn.javaField}s));
}
}
2 service.java.vm
项目路径:ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/service.java.vm
package ${
packageName}.service;
import java.util.List;
import {
mathJaxContainer[25]}{
ClassName};
import com.baomidou.mybatisplus.extension.service.IService;
/**
* ${functionName}Service接口
*
* @author ${author}
* @date ${datetime}
*/
public interface I{
mathJaxContainer[26]}{
ClassName}>
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
public {
mathJaxContainer[28]}{
ClassName}By{
mathJaxContainer[29]}{
pkColumn.javaType} ${
pkColumn.javaField});
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}集合
*/
public List<{
mathJaxContainer[31]}{
ClassName}List({
mathJaxContainer[32]}{
className});
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int insert{
mathJaxContainer[34]}{
ClassName} ${
className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update{
mathJaxContainer[36]}{
ClassName} ${
className});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键集合
* @return 结果
*/
public int delete{
mathJaxContainer[38]}{
pkColumn.capJavaField}s({
mathJaxContainer[39]}{
pkColumn.javaField}s);
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete{
mathJaxContainer[41]}{
pkColumn.capJavaField}({
mathJaxContainer[42]}{
pkColumn.javaField});
}
3 serviceImpl.java.vm
项目路径:ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/serviceImpl.java.vm
package ${
packageName}.service.impl;
import java.util.List;
#foreach ({
mathJaxContainer[43]}columns)
#if({
mathJaxContainer[44]}column.javaField == 'updateTime')
import com.ruoyi.common.core.utils.DateUtils;
#break
#end
#end
import org.springframework.stereotype.Service;
#if($table.sub)
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import {
mathJaxContainer[45]}{
subClassName};
#end
import {
mathJaxContainer[46]}{
ClassName}Mapper;
import {
mathJaxContainer[47]}{
ClassName};
import {
mathJaxContainer[48]}{
ClassName}Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/**
* ${functionName}Service业务层处理
*
* @author ${author}
* @date ${datetime}
*/
@Service
public class {
mathJaxContainer[49]}{
ClassName}Mapper,{
mathJaxContainer[50]}{
ClassName}Service
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
@Override
public {
mathJaxContainer[52]}{
ClassName}By{
mathJaxContainer[53]}{
pkColumn.javaType} ${
pkColumn.javaField})
{
return baseMapper.select{
mathJaxContainer[54]}{
pkColumn.capJavaField}(${
pkColumn.javaField});
}
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}
*/
@Override
public List<{
mathJaxContainer[56]}{
ClassName}List({
mathJaxContainer[57]}{
className})
{
return baseMapper.select{
mathJaxContainer[58]}{
className});
}
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int insert{
mathJaxContainer[60]}{
ClassName} ${
className})
{
#foreach ({
mathJaxContainer[61]}columns)
#if($column.javaField == 'createTime')
${
className}.setCreateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
int rows = baseMapper.insert{
mathJaxContainer[62]}{
className});
insert{
mathJaxContainer[63]}{
className});
return rows;
#else
return baseMapper.insert{
mathJaxContainer[64]}{
className});
#end
}
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int update{
mathJaxContainer[66]}{
ClassName} ${
className})
{
#foreach ({
mathJaxContainer[67]}columns)
#if($column.javaField == 'updateTime')
${
className}.setUpdateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
baseMapper.delete{
mathJaxContainer[68]}{
subTableFkClassName}({
mathJaxContainer[69]}{
pkColumn.capJavaField}());
insert{
mathJaxContainer[70]}{
className});
#end
return baseMapper.update{
mathJaxContainer[71]}{
className});
}
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete{
mathJaxContainer[73]}{
pkColumn.capJavaField}s({
mathJaxContainer[74]}{
pkColumn.javaField}s)
{
#if($table.sub)
baseMapper.delete{
mathJaxContainer[75]}{
subTableFkClassName}s(${
pkColumn.javaField}s);
#end
return baseMapper.delete{
mathJaxContainer[76]}{
pkColumn.capJavaField}s(${
pkColumn.javaField}s);
}
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete{
mathJaxContainer[78]}{
pkColumn.capJavaField}({
mathJaxContainer[79]}{
pkColumn.javaField})
{
#if($table.sub)
baseMapper.delete{
mathJaxContainer[80]}{
subTableFkClassName}(${
pkColumn.javaField});
#end
return baseMapper.delete{
mathJaxContainer[81]}{
pkColumn.capJavaField}(${
pkColumn.javaField});
}
#if($table.sub)
/**
* 新增${subTable.functionName}信息
*
* @param ${className} ${functionName}对象
*/
public void insert{
mathJaxContainer[83]}{
ClassName} ${
className})
{
List<{
mathJaxContainer[84]}{
subclassName}List = {
mathJaxContainer[85]}{
subClassName}List();
{
mathJaxContainer[86]}{
pkColumn.javaField} = {
mathJaxContainer[87]}{
pkColumn.capJavaField}();
if (StringUtils.isNotNull(${
subclassName}List))
{
List<{
mathJaxContainer[88]}{
subClassName}>();
for ({
mathJaxContainer[89]}{
subclassName} : ${
subclassName}List)
{
{
mathJaxContainer[90]}{
subTableFkClassName}(${
pkColumn.javaField});
list.add(${
subclassName});
}
if (list.size() > 0)
{
baseMapper.batch${
subClassName}(list);
}
}
}
#end
}
4 mapper.java.vm
项目路径:ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/mapper.java.vm
package ${
packageName}.mapper;
import java.util.List;
import {
mathJaxContainer[91]}{
ClassName};
#if($table.sub)
import {
mathJaxContainer[92]}{
subClassName};
#end
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* ${functionName}Mapper接口
*
* @author ${author}
* @date ${datetime}
*/
public interface {
mathJaxContainer[93]}{
ClassName}>
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
public {
mathJaxContainer[95]}{
ClassName}By{
mathJaxContainer[96]}{
pkColumn.javaType} ${
pkColumn.javaField});
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}集合
*/
public List<{
mathJaxContainer[98]}{
ClassName}List({
mathJaxContainer[99]}{
className});
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int insert{
mathJaxContainer[101]}{
ClassName} ${
className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update{
mathJaxContainer[103]}{
ClassName} ${
className});
/**
* 删除${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete{
mathJaxContainer[105]}{
pkColumn.capJavaField}({
mathJaxContainer[106]}{
pkColumn.javaField});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete{
mathJaxContainer[107]}{
pkColumn.capJavaField}s({
mathJaxContainer[108]}{
pkColumn.javaField}s);
#if($table.sub)
/**
* 批量删除${subTable.functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete{
mathJaxContainer[109]}{
subTableFkClassName}s({
mathJaxContainer[110]}{
pkColumn.javaField}s);
/**
* 批量新增${subTable.functionName}
*
* @param ${subclassName}List ${subTable.functionName}列表
* @return 结果
*/
public int batch{
mathJaxContainer[112]}{
subClassName}> ${
subclassName}List);
/**
* 通过${functionName}主键删除${subTable.functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}ID
* @return 结果
*/
public int delete{
mathJaxContainer[115]}{
subTableFkClassName}({
mathJaxContainer[116]}{
pkColumn.javaField});
#end
}