automybatis mysql_mybatis-plus:使用Mybatis-AutoGenerator代码生成器(1)
AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成包括 Entity、Mapper、Mapper XML、Service、Controller 數個模塊的代碼,可以提升開發效率.
首先,進入 https://start.spring.io 生成一個springboot簡單項目
下步,數據表SQL
CREATE TABLE `soldier` (
`soldier_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '士兵編號',
`soldier_name` varchar(30) NOT NULL COMMENT '士兵名字',
`join_army_time` timestamp NOT NULL COMMENT '參軍時間',
PRIMARY KEY (`soldier_id`),
KEY `sid` (`soldier_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
依賴
以下有些依賴不1定是必須的,但積累甚多,就一并貼出,
版本號是經過多次甄選的.
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-web-services
org.springframework.boot
spring-boot-devtools
runtime
false
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-freemarker
com.baomidou
mybatis-plus-generator
3.0.7.1
com.baomidou
mybatis-plus-boot-starter
3.1.1
org.mybatis.generator
mybatis-generator-core
1.3.7
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
org.springframework.boot
spring-boot-starter-data-jpa
com.github.vindell
spring-boot-starter-log4j2-plus
1.0.5.RELEASE
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
com.github.pagehelper
pagehelper-spring-boot-starter
1.2.5
org.springframework.session
spring-session-core
org.projectlombok
lombok
com.zaxxer
HikariCP
3.3.1
mysql
mysql-connector-java
8.0.11
org.apache.tomcat.embed
tomcat-embed-jasper
org.apache.activemq
activemq-all
5.15.9
cn.hutool
hutool-all
4.3.1
javax.servlet
javax.servlet-api
javax.servlet
jstl
配置mybatis-plus.properties文件
#此處為本項目src所在路徑(代碼生成器輸出路徑)
OutputDir=/home/gzh/eclipse-workspace/Boot-Demo/src/main/java
#mapper.xml的生成位置
OutputDirXml=/home/gzh/eclipse-workspace/Boot-Demo/src/main/resources
#數據庫表名(此處切不可為空,如果為空,則默認讀取數據庫的所有表名)
tableName=soldier
#存放所生成代碼文件的上一級包名
#className=自填
#設置作者
author=gene
#正常情況下,下面的代碼無需修改
#自定義包路徑
parent=cn.example.demo
#數據庫地址
url=jdbc:mysql://localhost:3306/test00?serverTimezone=CTT&characterEncoding=UTF-8&useSSL=false
#mysql:username & password
userName=plh
password=1234
Java代碼
注意導入的package,不要導錯
package cn.example.demo.util;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
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.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
*
* 代碼生成器
*
*
* @author gzh
*
*/
public class MybatisPlusGenerator {
public static void main(String[] args) throws InterruptedException {
// 獲取Mybatis-Plus.properties文件的配置信息
final ResourceBundle rb = ResourceBundle.getBundle("mybatis-plus");
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(rb.getString("OutputDir"));
gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setAuthor(rb.getString("author"));
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// dataSource配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setUrl(rb.getString("url"));
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername(rb.getString("userName"));
dsc.setPassword(rb.getString("password"));
mpg.setDataSource(dsc);
// package配置
PackageConfig pc = new PackageConfig();
pc.setParent(rb.getString("parent"));
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setEntity("bean");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
/* ... */
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定義輸出配置
List focList = new ArrayList<>();
// 自定義配置會被優先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(com.baomidou.mybatisplus.generator.config.po.TableInfo tableInfo) {
// 自定義輸入文件名稱
return rb.getString("OutputDirXml") + "/mapper/" + tableInfo.getEntityName() + 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);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setInclude(new String[]{rb.getString("tableName")});
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
System.out.println("done,fresh engineering");
}
}
運行上面的main方法,便可生成 xml/bean/mapper/service/controller 計5個模塊的代碼.
注:lombok注解@Data要想起作用的話,還要為IDE另外安裝lombok插件,具體步驟可單獨搜索 ?
總結
以上是生活随笔為你收集整理的automybatis mysql_mybatis-plus:使用Mybatis-AutoGenerator代码生成器(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java logic_java logi
- 下一篇: 麦当劳java排班_学习肯德基排班管理系