mybatis-plus使用
一、项目结构
二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ykq</groupId>
<artifactId>mybatis-plus-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-plus-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<!-- 添加 模板引擎 依赖 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.ykq.mybatisplusdemo.MybatisPlusDemoApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
三、application.properties
# 应用名称
spring.application.name=mybatis-plus-demo
server.port=8000
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath*:com/ykq/mybatisplusdemo/*/mapper/*Mapper.xml
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456
四、t_user
CREATE TABLE `t_user` (
`user_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '用户id',
`user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
`password` varchar(50) DEFAULT NULL COMMENT '密码',
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
`age` int(3) DEFAULT NULL COMMENT '年龄',
`sex` int(1) DEFAULT '0' COMMENT '性别:0-男,1-女',
`remark` varchar(100) DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
五、CodeGenerator
执行即可生成对应的代码。
package com.ykq.mybatisplusdemo;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
/**
* @Author ykq
* @Date 2022/5/11 18:57
* @Description mybatis plus代码生成
*/
public class CodeGenerator {
public static void main(String[] args) {
String parent = "com.ykq.mybatisplusdemo";
String parentPath = parent.replace(".", "/");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// (1)全局配置
GlobalConfig gc = new GlobalConfig();
// todo 在这里写好项目路径
String projectPath = "D:\\sts_workspacce\\mybatis-plus-demo";
gc.setOutputDir(projectPath + "/src/main/java");
String author = "ykq";
gc.setAuthor(author);
gc.setBaseResultMap(true);// 设置 ResultMap
gc.setBaseColumnList(true);// 设置 columList
gc.setOpen(false);
// mapper命名格式
gc.setMapperName("%sMapper");
// xml命名格式
gc.setXmlName("%sMapper");
// service命名格式
gc.setServiceName("%sService");
// serviceImpl命名格式
gc.setServiceImplName("%sServiceImpl");
// controller命名格式
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// (2)数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
// todo 在这里写好db配置
dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// (3)包配置
PackageConfig pc = new PackageConfig();
// todo 可以这里写好新增模块名
pc.setModuleName("tuser");
pc.setParent(parent);
// Entity包名
pc.setEntity("entity");
// Service包名
pc.setService("service");
// Service Impl包名
pc.setServiceImpl("service.impl");
// mapper包名
pc.setMapper("mapper");
// mapper xml包名
pc.setXml("mapper.xml");
// controller包名
pc.setController("controller");
mpg.setPackageInfo(pc);
// (4) 自定义配置
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
InjectionConfig cfg =new InjectionConfig(){
@Override
public void initMap(){
// to do nothing
}
};
List<FileOutConfig> focList =new ArrayList<>();
// ⾃定义配置会被优先输出
focList.add(new FileOutConfig(templatePath){
@Override
public String outputFile(TableInfo tableInfo){
return projectPath + "/src/main/resources/"+ parentPath + "/" + pc.getModuleName() + "/mapper/" + tableInfo.getEntityName()+"Mapper"+ StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(tc);
// (5)策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
// todo 设置对应表
strategy.setInclude("t_user");
// mapping中驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
// 带上@TableName以及@TableId
strategy.setEntityTableFieldAnnotationEnable(true);
// 表前缀
strategy.setTablePrefix(new String[] {});
mpg.setStrategy(strategy);
// (6)执行策略
mpg.execute();
}
}