使用SpringBoot2.3.0同tk.mapper整合及自动生成service,mapper层的代码;
工作中使用之前使用了SpringBoot1.5.12,近期尝试了下2.3.0练练手;直接代码如下:
Jar包依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
...
<spring.cloud.version>Hoxton.SR3</spring.cloud.version>
<spring.boot.version>2.3.0.RELEASE</spring.boot.version>
<redis.version>3.7.0</redis.version>
<config.version>2.2.8.RELEASE</config.version>
<cloud.version>2.2.2.RELEASE</cloud.version>
<spring-boot-starter-validation.version>2.4.3</spring-boot-starter-validation.version>
<mysql-connector-java.version>5.1.46</mysql-connector-java.version>
<mybatis-spring-boot-starter.version>1.3.0</mybatis-spring-boot-starter.version>
...
</properties>
<dependencyManagement>
<dependencies>
<!--spring-cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>${cloud.version}</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.version}</version>
</dependency>
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>${config.version}</version>
</dependency>
<!--start MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java.version}</version>
</dependency>
<!--end MySQL -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring-boot-starter-validation.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<!--<version>1.2.3</version>-->
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.4</version>
</dependency>
<!-- Code auto generator-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>test</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
其中本次涉及重点讲下这个插件的整合
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.4</version>
</dependency>
Bean的 MybatisConfig.java
package com.xxxx.search.service.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.xxxx.search.service.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ObjectUtils;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
/**
* Mybatis & Mapper & PageHelper 配置
*@author liaozhanggen
*/
@Slf4j
@Configuration
public class MybatisConfig {
private static final String[] MAPPER_LOCATIONS = new String[]{"classpath:mybatis/mapper/*Mapper.xml"};
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage(Constants.MODEL_PACKAGE);
// 配置数据库表字段与对象属性字段的映射方式(下划线=》驼峰)
tk.mybatis.mapper.session.Configuration configuration = new tk.mybatis.mapper.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
configuration.setUseGeneratedKeys(true);
factory.setConfiguration(configuration);
// 配置分页插件,详情请查阅官方文档
PaginationInterceptor pageHelper = new PaginationInterceptor();
Properties properties = new Properties();
// 分页尺寸为0 时查询所有纪录不再执行分页
properties.setProperty("pageSizeZero", "true");
// 页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("reasonable", "true");
// 支持通过 Mapper 接口参数来传递分页参数
properties.setProperty("supportMethodsArguments", "true");
pageHelper.setProperties(properties);
// 添加插件
factory.setPlugins(new Interceptor[]{pageHelper});
// // 添加XML目录
// ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// 如果没有此目录的话(如果使用注解的方式的话,不需要创建mapper目录,不需要写xml文件)
// 后续初始化SqlSessionFactory会报错,这里参考MybatisProperties的代码
Resource[] resources = null;
if (!ObjectUtils.isEmpty(resources = resolveMapperLocations(MAPPER_LOCATIONS))) {
factory.setMapperLocations(resources);
}
log.info("--mybatis,{},mybatis resources->{},{}",dataSource.toString(),resources,resources.length);
return factory.getObject();
}
private Resource[] resolveMapperLocations(String... mapperLocations) {
PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
List<Resource> resourceList = new ArrayList<Resource>();
if(mapperLocations != null) {
int total = mapperLocations.length;
for(int i = 0; i < total; ++i) {
String mapperLocation = mapperLocations[i];
try {
Resource[] mappers = resourceResolver.getResources(mapperLocation);
resourceList.addAll(Arrays.asList(mappers));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
return resourceList.toArray(new Resource[resourceList.size()]);
}
}
yml?
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://xxx:3306/xxxx?useAffectedRows=true&useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
username: xxx
password: xxx@com
driver-class-name: com.mysql.jdbc.Driver
druid:
initialSize: 15
maxActive: 100
minIdle: 50
maxWait: 60000
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 50
maxOpenPreparedStatements: 50
removeAbandonedTimeout: 1800
logAbandoned: true
removeAbandoned: true
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
# ,wall,slf4j 加入这个就不能创建flywary使用
filters: stat
useUnfairLock: true
jpa:
database-platform: org.hibernate.dialect.MySQL5Dialect
show-sql: true
application.java启动?
@EnableSwagger2Doc
// 启用注册中心发现服务
@EnableDiscoveryClient
// 启用注解事务管理
@EnableTransactionManagement
@MapperScan("com.xx.xxx.service.biz.repo")
@EnableEurekaClient
@SpringBootApplication(scanBasePackages = {"com.xxx.framework", "com.xx.xxx"})
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class);
}
}
自动生成代码工具(controller,service,dal,*Mapper.xml)?
package com.xxx.search.service;
import com.google.common.base.CaseFormat;
import com.google.common.collect.Lists;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.xxxx.search.service.constants.ProjectConstant.*;
/**
* 代码生成器,根据数据表名称生成对应的Model、Mapper、Service、Controller简化开发。
*/
public class CodeGenerator {
//JDBC配置,请修改为你项目的实际配置
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/xxx";
private static final String JDBC_USERNAME = "dev_user";
private static final String JDBC_PASSWORD = "xxx@com";
private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径
private static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/devops-schedule-service/src/test/resources/generator/template";//模板位置
private static final String JAVA_PATH = "/src/main/java"; //java文件路径
private static final String RESOURCES_PATH = "/src/main/resources";//资源文件路径
private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径
private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径
private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径
/** 可在Run/Debug Configurations CodeGenerator启动类配置 VM Options -Duser.name=XXX ,即可在注释中生成author,默认是使用计算机使用者名称如在windows下是Administrator*/
// private static final String AUTHOR = System.getProperty("user.name");//@author
private static final String AUTHOR = "liaozhanggen@mistong.com";//@author
private static final String DATE = new SimpleDateFormat("yyyy/MM/dd").format(new Date());//@date
public static void main(String[] args) throws IOException {
InputStream ins = null;
try {
ins = System.in;
Scanner scanner = new Scanner(ins);
while (true) {
System.out.println("输入表名:Model类名【单个用t_user:User,多个用t_user:User,t_city:City(英文逗号隔开)】 输入end结束");
String tableNameAndModelNameStr = null;
tableNameAndModelNameStr = scanner.next();
if ("end".equals(tableNameAndModelNameStr)) {
return;
}
StringTokenizer stringTokenizer = new StringTokenizer(tableNameAndModelNameStr, ",",
false);
List<String> tableNameAndModelNameList = Lists.newArrayList();
while (stringTokenizer.hasMoreElements()) {
tableNameAndModelNameList.add(stringTokenizer.nextToken());
}
for (String tableNameAndModelName : tableNameAndModelNameList) {
String[] arr = tableNameAndModelName.split(":");
genCode(arr[0], arr[1]);
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
ins.close();
}
}
/**
* 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。
* 如输入表名称 "t_user_detail" 将生成 TUserDetail、TUserDetailMapper、TUserDetailService ...
* @param tableNames 数据表名称...
*/
public static void genCode(String... tableNames) {
for (String tableName : tableNames) {
genCode(tableName, null);
}
}
/**
* 通过数据表名称,和自定义的 Model 名称生成代码
* 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User" 将生成 User、UserMapper、UserService ...
* @param tableName 数据表名称
* @param modelName 自定义的 Model 名称
*/
public static void genCode(String tableName, String modelName) {
genModelAndMapper(tableName, modelName);
genService(tableName, modelName);
// genController(tableName, modelName);
}
public static void genModelAndMapper(String tableName, String modelName) {
Context context = new Context(ModelType.FLAT);
context.setId("Potato");
context.setTargetRuntime("MyBatis3Simple");
context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
pluginConfiguration.addProperty("mappers", MAPPER_INTERFACE_REFERENCE);
context.addPluginConfiguration(pluginConfiguration);
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(PROJECT_PATH + "/devops-schedule-service/"+ JAVA_PATH);
javaModelGeneratorConfiguration.setTargetPackage(MODEL_PACKAGE);
context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(PROJECT_PATH + "/devops-schedule-service/"+ RESOURCES_PATH);
sqlMapGeneratorConfiguration.setTargetPackage("mybatis/mapper");
context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setTargetProject(PROJECT_PATH + "/devops-schedule-service/"+ JAVA_PATH);
javaClientGeneratorConfiguration.setTargetPackage(MAPPER_PACKAGE);
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
TableConfiguration tableConfiguration = new TableConfiguration(context);
tableConfiguration.setTableName(tableName);
tableConfiguration.setDomainObjectName(modelName);
tableConfiguration.setGeneratedKey(new GeneratedKey("id", "Mysql", true, null));
context.addTableConfiguration(tableConfiguration);
List<String> warnings;
MyBatisGenerator generator;
try {
Configuration config = new Configuration();
config.addContext(context);
config.validate();
boolean overwrite = true;
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
warnings = new ArrayList<String>();
generator = new MyBatisGenerator(config, callback, warnings);
generator.generate(null);
} catch (Exception e) {
throw new RuntimeException("生成Model和Mapper失败", e);
}
if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
throw new RuntimeException("生成Model和Mapper失败:" + warnings);
}
if (StringUtils.isEmpty(modelName)) modelName = tableNameConvertUpperCamel(tableName);
System.out.println(modelName + ".java 生成成功");
System.out.println(modelName + "mapper.java 生成成功");
System.out.println(modelName + "mapper.xml 生成成功");
}
public static void genService(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();
Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", tableNameConvertLowerCamel(tableName));
data.put("basePackage", BASE_PACKAGE);
File file = new File(PROJECT_PATH + "/devops-schedule-service/"+ JAVA_PATH + PACKAGE_PATH_SERVICE + modelNameUpperCamel + "Service.java");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
cfg.getTemplate("service.ftl").process(data,
new FileWriter(file));
System.out.println(modelNameUpperCamel + "Service.java 生成成功");
File file1 = new File(PROJECT_PATH + "/devops-schedule-service/"+ JAVA_PATH + PACKAGE_PATH_SERVICE_IMPL + modelNameUpperCamel + "ServiceImpl.java");
if (!file1.getParentFile().exists()) {
file1.getParentFile().mkdirs();
}
cfg.getTemplate("service-impl.ftl").process(data, new FileWriter(file1));
System.out.println(modelNameUpperCamel + "ServiceImpl.java 生成成功");
} catch (Exception e) {
throw new RuntimeException("生成Service失败", e);
}
}
public static void genController(String tableName, String modelName) {
try {
freemarker.template.Configuration cfg = getConfiguration();
Map<String, Object> data = new HashMap<>();
data.put("date", DATE);
data.put("author", AUTHOR);
String modelNameUpperCamel = StringUtils.isEmpty(modelName) ? tableNameConvertUpperCamel(tableName) : modelName;
data.put("baseRequestMapping", modelNameConvertMappingPath(modelNameUpperCamel));
data.put("modelNameUpperCamel", modelNameUpperCamel);
data.put("modelNameLowerCamel", CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel));
data.put("basePackage", BASE_PACKAGE);
File file = new File(PROJECT_PATH + "/devops-schedule-service/"+ JAVA_PATH + PACKAGE_PATH_CONTROLLER + modelNameUpperCamel + "Controller.java");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
cfg.getTemplate("controller-restful.ftl").process(data, new FileWriter(file));
//cfg.getTemplate("controller.ftl").process(data, new FileWriter(file));
System.out.println(modelNameUpperCamel + "Controller.java 生成成功");
} catch (Exception e) {
throw new RuntimeException("生成Controller失败", e);
}
}
private static freemarker.template.Configuration getConfiguration() throws IOException {
freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File(TEMPLATE_FILE_PATH));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
return cfg;
}
private static String tableNameConvertLowerCamel(String tableName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, tableName.toLowerCase());
}
private static String tableNameConvertUpperCamel(String tableName) {
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
}
private static String tableNameConvertMappingPath(String tableName) {
tableName = tableName.toLowerCase();//兼容使用大写的表名
return (tableName.contains("_") ? tableName.replaceAll("_", "/") : tableName);
}
private static String modelNameConvertMappingPath(String modelName) {
return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelName);
}
private static String packageConvertPath(String packageName) {
return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
}
}
具体的代码git地址工程
GitHub - liaonanfeng/springboot-tkmapperContribute to liaonanfeng/springboot-tkmapper development by creating an account on GitHub.https://github.com/liaonanfeng/springboot-tkmapper
|