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.neu.soft</groupId>
<artifactId>plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>plus</name>
<description>plus</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.6.13</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!--mybatisplus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</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>${spring-boot.version}</version>
<configuration>
<mainClass>com.neu.soft.plus.PlusApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
代码生成器类
package com.neu.soft.plus;
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 代码生成器
*
* @author
* @since
*/
public class CodeGenerator {
/**
* 执行代码生成 main 方法
*
* @param args
*/
public static void main(String[] args) {
CodeGenerator.autoGenerator();
}
/**
* 代码生成设置
*/
public static void autoGenerator(){
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//代码生成后存放路径
gc.setOutputDir(projectPath + "/src/main/java");
//作者
gc.setAuthor("xxxx");
//生成后是否打开
gc.setOpen(false);
gc.setIdType(IdType.ID_WORKER);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
//去掉生成Service的I -- 默认生成的是XXXIService,这样设置后显示的是XXXService,%s表示类的名字
gc.setServiceName("%sService");
gc.setFileOverride(false);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(false);
mpg.setGlobalConfig(gc);
//数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=UTC");
//驱动
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
//用户名
dsc.setUsername("root");
//密码
dsc.setPassword("root");
mpg.setDataSource(dsc);
//包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.neu");
pc.setModuleName("base");
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setEntity("entity");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setMapper("/templates/mapper.java");
templateConfig.setXml("/templates/mapper.xml");
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true);
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
/**
* 定义切面
*/
static class MyInjectionConfig extends InjectionConfig {
/**
* 文件存储的url
*/
private String parentPath;
/**
* 构造器
*
* @param parentPath 文件存储的url 如:com.lingluo.app
*/
public MyInjectionConfig(String parentPath) {
this.parentPath = parentPath;
}
/**
* 设置在ftl文件中要用到的参数 如:templates/appService.java.ftl 文件中 ${cfg.appServiceName}
*/
@Override
public void initMap() {
List<TableInfo> list = this.getConfig().getTableInfoList();
Map<String, Object> map = new HashMap<>(10);
map.put("appService", parentPath);
for (TableInfo tableInfo : list) {
map.put("appServiceName", tableInfo.getEntityName() + "AppService");
}
this.setMap(map);
}
}
/**
* 自定义文件输出目录
*/
static class MyFileOutConfig extends FileOutConfig {
/**
* 完整的文件输出目录
*/
private String filePath;
/**
* 文件输出构造器
*
* @param templatePath 模板url 如:/templates/appService.java.ftl
* @param filePath 完成的文件输出目录 如:C:\Users\luohoujian01\myProject\springboot-project/src/main/java/com/lingluo/app/
*/
public MyFileOutConfig(String templatePath, String filePath) {
super(templatePath);
this.filePath = filePath;
}
// 自定义输出文件目录,完整的输出目录 如:C:\Users\luohoujian01\myProject\springboot-project/src/main/java/com/lingluo/app/AlipayOrdersAppService.java
@Override
public String outputFile(TableInfo tableInfo) {
return filePath + String.format("%sAppService", tableInfo.getEntityName()) + ".java";
}
}
}
mapper相关配置信息(ftl)
<#assign className = table.mapperName>
package ${package.Mapper};
import ${package.Entity}.${entity};
import org.apache.ibatis.annotations.*;
import java.math.BigInteger;
import java.util.List;
@Mapper
public interface ${className} {
// 根据ID查询操作
@Select("SELECT * FROM ${table.name} WHERE ${table.name}_id = <#noparse>#{</#noparse>${table.name}Id<#noparse>}</#noparse> AND is_deleted=0")
${entity} getById(BigInteger ${table.name}Id);
// 根据ID提取操作
@Select("SELECT * FROM ${table.name} WHERE ${table.name}_id = <#noparse>#{</#noparse>${table.name}Id<#noparse>}</#noparse>")
${entity} extractById(BigInteger ${table.name}Id);
// 插入操作
Integer insert(${entity} ${table.name});
// 更新操作
Integer update(${entity} ${table.name});
// 删除操作
@Update("UPDATE ${table.name} SET update_time = <#noparse>#{</#noparse>updateTime<#noparse>}</#noparse> , is_deleted = 1 WHERE ${table.name}_id = <#noparse>#{</#noparse>${table.name}Id<#noparse>}</#noparse>")
Integer delete(@Param("updateTime") Integer updateTime,BigInteger ${table.name}Id);
List<${entity}> findListByPage(@Param("pageSize") Integer pageSize,@Param("offset")Integer offset,@Param("name") String name);
Integer total(@Param("name")String name,@Param("title") String title);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
<insert id="insert"
useGeneratedKeys="true"
keyProperty="id"
parameterType="${package.Entity}.${entity}">
insert into ${table.name}(
<#list table.fields as field>
<#if field.keyFlag == true>
<#else>
<#if field.name == "is_deleted">
is_deleted <#if field_has_next>,</#if>
<#else>
<#if field.propertyType == "String" >
<if test=" ${field.propertyName} != null and ${field.propertyName} != ''"> `${field.name}` <#if field_has_next>,</#if></if>
<#else>
<#if field.propertyType == "Integer">
<if test=" ${field.propertyName} != null "> `${field.name}` <#if field_has_next>,</#if></if>
</#if>
</#if>
</#if>
</#if>
</#list>
)values(
<#list table.fields as field>
<#if field.keyFlag == true>
<#else>
<#if field.name == "is_deleted">
<#noparse>#{</#noparse>isDeleted<#noparse>}</#noparse> <#if field_has_next>,</#if>
<#else>
<#if field.propertyType == "String" >
<if test="${field.propertyName} != null and ${field.propertyName}!= ''"><#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse><#if field_has_next>,</#if></if>
<#else>
<#if field.propertyType == "Integer">
<if test="${field.propertyName} != null"><#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse><#if field_has_next>,</#if></if>
</#if>
</#if>
</#if>
</#if>
</#list>
)
</insert>
<!-- 更新操作 -->
<update id="update" parameterType="${package.Entity}.${entity}">
UPDATE ${table.name}
<set>
<#list table.fields as field>
<#if field.keyFlag == true>
id = <#noparse>#{</#noparse>Id<#noparse>}</#noparse>,
<#else>
<#if field.propertyType == "String" >
<if test="${field.name} != null and ${field.name} != ''">
${field.name} = <#noparse>#{</#noparse> ${field.propertyName} <#noparse>}</#noparse>,
</if>
<#else>
<#if field.propertyType == "Integer">
<if test="${field.name} != null">
${field.name} = <#noparse>#{</#noparse> ${field.propertyName} <#noparse>}</#noparse>,
</if>
</#if>
</#if>
</#if>
</#list>
</set>
WHERE id = <#noparse>#{</#noparse>Id<#noparse>}</#noparse>
</update>
</mapper>
application.properties
# 应用服务 WEB 访问端口
server.port=8080
#下面这些内容是为了让MyBatis映射
#指定Mybatis的Mapper文件
mybatis.mapper-locations=classpath:mappers/*xml
#指定Mybatis的实体目录
mybatis.type-aliases-package=com.post.app.mybatis.entity
#在Spring Boot中,可以通过设置map-underscore-to-camel-case属性为true来开启驼峰功能。
mybatis.configuration.map-underscore-to-camel-case=true
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/post?serverTimezone=UTC&useSSL=false&useAffectedRows=true
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root