jdk | 1.8 |
---|---|
数据库 | Mysql5.5+ |
IDE | IDEA2018.2 |
Maven | 3.3.9 |
Spring boot | 2.1.0.RELEASE |
Mybatis plus | 3.0.5 |
1.建立Spring Initializr项目
2.添加进pom.xml所需要的依赖
4.0.0
com.lj
demo
0.0.1-SNAPSHOT
jar
demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
com.baomidou
mybatisplus-spring-boot-starter
1.0.4
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.apache.velocity
velocity-engine-core
2.0
com.baomidou
mybatis-plus-boot-starter
3.0.5
org.projectlombok
lombok
1.16.20
provided
com.baomidou
mybatis-plus
3.0.5
org.freemarker
freemarker
2.3.23
mysql
mysql-connector-java
6.0.6
org.junit.jupiter
junit-jupiter-api
RELEASE
compile
org.springframework.boot
spring-boot-maven-plugin
3.Mybatis plus代码生成器
创建MysqlGenerator类
package com.lj.generator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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 java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class MysqlGenerator {
/**
*
* 读取控制台内容
*
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//outputDir:生成文件的输出目录,默认值:D 盘根目录
gc.setOutputDir(projectPath + "/src/main/java");
//IdType设置主键生成策略,默认值null
gc.setIdType(IdType.UUID);
//Author设置作者
gc.setAuthor("LuJ");
//Open:是否打开输出目录
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/student?serverTimezone=UTC&&characterEncoding=utf-8&useSSL=false");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("1234");
//设置数据库类型,默认MYSQL
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(scanner("模块名"));
//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
pc.setParent("com.lj");
mpg.setPackageInfo(pc);
//模板配置
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//设置命名格式
strategy.setNaming(NamingStrategy.underline_to_camel);
//【实体】是否为lombok模型(默认 false)
strategy.setEntityLombokModel(false);
//生成 @RestController 控制器
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名"));
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//表名前缀
strategy.setTablePrefix(pc.getModuleName() + "_");
//是否生成实体时,生成字段注解
strategy.entityTableFieldAnnotationEnable(true);
//设置自定义继承的Entity类全称,带包名
// strategy.setSuperEntityClass("com.lj.common.BaseEntit");
//设置自定义继承的Controller类全称,带包名
// strategy.setSuperControllerClass("com.lj.common.BaseController");
//设置自定义基础的Entity类,公共字段
// strategy.setSuperEntityColumns("id");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
4.application.yml配置信息
# DataSource Config
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC&&characterEncoding=utf-8&useSSL=false
username: root
password: 1234
#mybatis-plus:
# mapper-locations: classpath:mapper/*.xml
# typeAliasesPackage: com.lj.demo.entity
# configuration:
# map-underscore-to-camel-case: true #使用驼峰法映射属性
# global-config:
# #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
# id-type: 0
# #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
# field-strategy: 2
# #驼峰下划线转换
# db-column-underline: true
# #刷新mapper 调试神器
# refresh-mapper: true
# #数据库大写下划线转换
# #capital-mode: true
# #序列接口实现类配置
# #key-generator: com.baomidou.springboot.xxx
# #逻辑删除配置
# #logic-delete-value: 0
# #logic-not-delete-value: 1
# #自定义填充策略接口实现
# #meta-object-handler: com.baomidou.springboot.xxx
# #自定义SQL注入器
# #sql-injector: com.baomidou.springboot.xxx
5.执行MysqlGenerator
执行后自动生成对应Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,以下是路径图
7.测试类DemoApplicationTest
package com.lj.demo;
import com.lj.demo.entity.Stu;
import com.lj.demo.mapper.StuMapper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan("com.lj.demo.mapper")
public class DemoApplicationTests {
@Autowired
StuMapper stuMapper;
@Test
public void contextLoads() {
List stuList = new ArrayList<>();
Stu stu = new Stu("join","2",40,"18855544444");
stuMapper.insert(stu);
// stuList.forEach(System.out::println);
// Assert.assertEquals(0, stuList.size());
}
}