简介
Mybatis 相信大家都用过,是一款优秀的持久层框架,支持支持定制化Sql,存储过程和高级映射,大大提高了开发过程中与数据库交互的效率,而且非常的灵活,而Mybatis Plus 是Mybatis更高层级的封装,我们经常使用的增删改查她已经帮我们集成好了,我们直接使用即可,比Mybaits 更智能,开发效率更高。本文章Mybatis 版本为3.0.6,话不多说,一个字:干
1.Maven 配置文件
com.baomidou
mybatis-plus-boot-starter
3.0.6
org.apache.tomcat
tomcat-jdbc
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-freemarker
io.springfox
springfox-swagger2
2.8.0
org.mapstruct
mapstruct
io.springfox
springfox-swagger-ui
2.8.0
2.Mybatis配置文件
resources目录下创建config文件夹,然后再config文件夹下创建json类型的配置文件
{
"author": "gaoleijie",
"url": "jdbc:mysql://127.0.0.1:3306/security_oauth?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8",
"userName": "root",
"password": "root",
"driverName": "com.mysql.cj.jdbc.Driver",
"rootPath": "com.glj.order",
"mapping": [
{
"moduleName": "base",
"table": [
"oauth_client_details",
"sys_permission",
"sys_role",
"sys_role_permission",
"sys_user"
]
}
]
}
3.生成代码类
package com.glj.order;
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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CodeGenerator {
private static final Logger logger = LoggerFactory.getLogger(CodeGenerator.class);
private final static String AUTHOR = "author";
private final static String URL = "url";
private final static String TABLE = "table";
private final static String PASSWORD = "password";
private final static String USER_NAME = "userName";
private final static String MODULE_NAME = "moduleName";
private final static String DRIVER_NAME = "driverName";
private final static String MODULE_ROOT_PATH = "rootPath";
private final static String TABLE_MODULE_MAPPING = "mapping";
public static void main(String[] args) {
//代码自动生成
Map configData;
try {
ObjectMapper mapper = new ObjectMapper();
configData = mapper.readValue(ResourceUtils.
getFile("classpath:config/generateCfg.json"), Map.class);
} catch (Exception e) {
logger.debug("Parse the config failure , reason : {}", e);
throw new RuntimeException("Parse config generate file failure");
}
List