我是靠谱客的博主 虚幻白猫,这篇文章主要介绍mybatis-plus自动生成,现在分享给大家,希望可以做个参考。

依赖

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.4.1</version> </dependency> <!-- 模板引擎 代码生成 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <!-- 模板引擎 代码生成 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public static void main(String[] args) { AutoGenerator ag = new AutoGenerator(); //开启连接数据库 DataSourceConfig dsc = new DataSourceConfig(); // oracle // dsc.setUrl("jdbc:oracle:thin:@10.0.0.22:1521:orcl"); // dsc.setDriverName("oracle.jdbc.driver.OracleDriver"); // dsc.setUsername("C##gjh"); // dsc.setPassword("123456"); // msyql dsc.setUrl("jdbc:mysql://127.0.0.1:3306/bank?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root123"); ag.setDataSource(dsc); //设置全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("F:\ping"); gc.setAuthor("guanjihang"); gc.setFileOverride(true); gc.setOpen(false); gc.setMapperName("%sMapper");//OrderMapper gc.setXmlName("%sMapper");//OrderMapper.xml gc.setServiceName("%sService");//Service gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sCtrl"); ag.setGlobalConfig(gc); //设置包名 PackageConfig pc = new PackageConfig(); pc.setParent("com.qingge.springboot"); pc.setMapper("mapper"); pc.setEntity("pojo"); pc.setController("controller"); pc.setService("services"); pc.setServiceImpl("services.impl"); //设置每个类的主键方式 gc.setIdType(IdType.AUTO); //设置日期类型 gc.setDateType(DateType.ONLY_DATE); ag.setPackageInfo(pc); //设置策略 StrategyConfig sc = new StrategyConfig(); sc.setEntityLombokModel(true); sc.setRestControllerStyle(true); sc.setColumnNaming(NamingStrategy.underline_to_camel); sc.setNaming(NamingStrategy.underline_to_camel); ag.setStrategy(sc); ag.execute(); }

生成工具类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public static void main(String[] args) { AutoGenerator ag = new AutoGenerator(); // //开启连接数据库 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/qing?useUnicode=true&useSSL=false&characterEncoding=utf8"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); ag.setDataSource(dsc); //设置全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir("/Users/xiaobaihao/Desktop/pure-design-master/src/main/java"); gc.setAuthor("xiaobaihao、"); gc.setFileOverride(true); gc.setOpen(false); gc.setMapperName("%sMapper");//OrderMapper gc.setXmlName("%sMapper");//OrderMapper.xml gc.setServiceName("%sService");//Service gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sCtrl"); ag.setGlobalConfig(gc); //设置包名 PackageConfig pc = new PackageConfig(); pc.setParent("com.qingge.springboot"); pc.setMapper("mapper"); pc.setEntity("entity"); pc.setController("controller"); pc.setService("services"); pc.setServiceImpl("services.impl"); //设置每个类的主键方式 gc.setIdType(IdType.AUTO); //设置日期类型 gc.setDateType(DateType.ONLY_DATE); ag.setPackageInfo(pc); //设置策略 StrategyConfig sc = new StrategyConfig(); sc.setEntityLombokModel(true); sc.setRestControllerStyle(true); sc.setColumnNaming(NamingStrategy.underline_to_camel); sc.setNaming(NamingStrategy.underline_to_camel); ag.setStrategy(sc); ag.execute(); }

生成单张表的话可以用

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.stu; 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 java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * <p> * mysql 代码生成器 * </p> */ public class Generator { /** * RUN THIS */ public static void main(String[] args) { //获取控制台的数据 Scanner scanner = new Scanner(System.in); // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); System.out.println("请输入文件输出目录的模块或者项目的地址:"); String projectPath = scanner.nextLine(); gc.setOutputDir(projectPath + "/src/main/java"); //生成文件的输出目录 gc.setAuthor("jjh"); //作者 gc.setFileOverride(true); //是否覆蓋已有文件 默认值:false gc.setOpen(false); //是否打开输出目录 默认值:true gc.setBaseColumnList(true); //开启 baseColumnList 默认false gc.setBaseResultMap(true); //开启 BaseResultMap 默认false // gc.setEntityName("%sEntity"); //实体命名方式 默认值:null 例如:%sEntity 生成 UserEntity gc.setMapperName("%sMapper"); //mapper 命名方式 默认值:null 例如:%sDao 生成 UserDao gc.setXmlName("%sMapper"); //Mapper xml 命名方式 默认值:null 例如:%sDao 生成 UserDao.xml gc.setServiceName("%sService"); //service 命名方式 默认值:null 例如:%sBusiness 生成 UserBusiness gc.setServiceImplName("%sServiceImpl"); //service impl 命名方式 默认值:null 例如:%sBusinessImpl 生成 UserBusinessImpl gc.setControllerName("%sController"); //controller 命名方式 默认值:null 例如:%sAction 生成 UserAction mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("199000222"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); // pc.setModuleName(scanner("模块名")); // pc.setParent("com.stu"); System.out.println("请输入模块名:"); String name = scanner.nextLine(); //自定义包配置 pc.setParent(name); pc.setModuleName(null); pc.setMapper("mapper"); pc.setEntity("domain"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setController("controller"); mpg.setPackageInfo(pc); // 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> 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); //表名生成策略 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行 // strategy.setCapitalMode(true); // 全局大写命名 ORACLE 注意 // strategy.setTablePrefix("prefix"); //表前缀 // strategy.setSuperEntityClass("com.stu.domain"); //自定义继承的Entity类全称,带包名 // strategy.setSuperEntityColumns(new String[] { "test_id", "age" }); //自定义实体,公共字段 strategy.setEntityLombokModel(true); //【实体】是否为lombok模型(默认 false strategy.setRestControllerStyle(true); //生成 @RestController 控制器 // strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController"); //自定义继承的Controller类全称,带包名 // strategy.setInclude(scanner("表名")); //需要包含的表名,允许正则表达式(与exclude二选一配置) System.out.println("请输入映射的表名:"); String tables = scanner.nextLine(); String[] num = tables.split(","); strategy.setInclude(num); // 需要生成的表可以多张表 // strategy.setExclude(new String[]{"test"}); // 排除生成的表 //如果数据库有前缀,生成文件时是否要前缀acl_ // strategy.setTablePrefix("bus_"); // strategy.setTablePrefix("sys_"); strategy.setControllerMappingHyphenStyle(true); //驼峰转连字符 strategy.setTablePrefix(pc.getModuleName() + "_"); //是否生成实体时,生成字段注解 mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }

最后

以上就是虚幻白猫最近收集整理的关于mybatis-plus自动生成的全部内容,更多相关mybatis-plus自动生成内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部