1.背景
可以使用mybatis-plus-generator
逆向生成dao层、service层、controller层等代码
2.引入jar包
mybatis-plus-generator
在3.5.0以及以后的版本使用新的方式逆向生成代码。
这里介绍使用旧版本的方式生成代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!-- mybatis-plus begin --> < dependency > < groupId >com.baomidou</ groupId > < artifactId >mybatis-plus-boot-starter</ artifactId > < version >3.4.1</ version > </ dependency > < dependency > < groupId >com.baomidou</ groupId > < artifactId >mybatis-plus-generator</ artifactId > < version >3.4.1</ version > </ dependency > < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.44</ version > </ dependency > <!-- mybatis-plus 默认是vm引擎 --> < dependency > < groupId >org.apache.velocity</ groupId > < artifactId >velocity-engine-core</ artifactId > < version >2.0</ version > </ dependency > <!-- mybatis-plus end --> |
3.自动生成代码
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
|
public static void main(String[] args) { // 构建一个代码生成器对象 AutoGenerator mpg = new AutoGenerator(); // 配置执行策略 // 1.全局配置 GlobalConfig gc = new GlobalConfig(); // 当前项目路径 String proPath = System.getProperty( "user.dir" ); // 设置代码生成路径 gc.setOutputDir(proPath + "/src/main/java" ); // 生成的类的注释中作者信息 gc.setAuthor( "curry" ); // 生成后是否打开文件夹 gc.setOpen( false ); // 是否覆盖 gc.setFileOverride( true ); // 生成service类的后缀 gc.setServiceName( "%sService" ); // 主键生成策略 和数据库id生成策略一致 gc.setIdType(IdType.AUTO); // 设置日期类型 gc.setDateType(DateType.ONLY_DATE); // 是否生成Swagger gc.setSwagger2( false ); // 生成entity类的后缀 gc.setEntityName( "%sEntity" ); mpg.setGlobalConfig(gc); // 2.设置数据源 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl( "jdbc:mysql://127.0.0.1:3306/test_db" ); dsc.setDriverName( "com.mysql.jdbc.Driver" ); dsc.setUsername( "root" ); dsc.setPassword( "123456" ); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); // 3.配置生成包的路径 PackageConfig pc = new PackageConfig(); // 设置模块存放位置 pc.setParent( "com." ); // 设置该模块包的路径 pc.setModuleName( "dao" ); pc.setEntity( "entity" ); pc.setMapper( "mapper" ); pc.setService( "service" ); pc.setController( "controller" ); mpg.setPackageInfo(pc); // 4.策略配置 StrategyConfig strategy= new StrategyConfig(); // 设置要映射的表名 不配置默认处理全部表 // strategy.setInclude("user"); // 表名中下划线转驼峰命名 strategy.setNaming(NamingStrategy.underline_to_camel); // 表中字段如果有下划线,转驼峰命名 strategy.setColumnNaming(NamingStrategy.underline_to_camel); // strategy.setEntityLombokModel(true);//自动生成Lombok // strategy.setRestControllerStyle(true);//开启 RestFul 风格 // strategy.setControllerMappingHyphenStyle(true); // 对表中的字段 设置逻辑删除 生成的dao层代码会添加@TableLogic strategy.setLogicDeleteFieldName( "delete_flag" ); // 5.自动填充 (表中如果有创建时间、修改时间话,可以使用自动填充) TableFill createTime = new TableFill( "created_date" , FieldFill.INSERT); TableFill updateTime = new TableFill( "modified_date" , FieldFill.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(createTime); tableFills.add(updateTime); strategy.setTableFillList(tableFills); // 乐观锁配置 // strategy.setVersionFieldName("version"); mpg.setStrategy(strategy); // 6.配置实体类模板 TemplateConfig templateConfig = new TemplateConfig(); // 如果setXxxxx(null) 不会生成Xxxx实体类相关代码 // 因此如果只生成dao层代码 // 可以在这里控制 templateConfig.setController( null ); templateConfig.setMapper( null ); templateConfig.setService( null ); templateConfig.setServiceImpl( null ); templateConfig.setXml( null ); mpg.setTemplate(templateConfig); // 7.执行代码生成操作 mpg.execute(); } |
4.修改*Mapper.xml文件的生成位置
4.1 默认*Mapper.xml文件生成位置
1
2
3
4
5
6
7
8
9
10
11
|
// 3.配置生成包的路径 PackageConfig pc = new PackageConfig(); // 设置模块存放位置 pc.setParent( "com." ); // 设置该模块包的路径 pc.setModuleName( "dao" ); pc.setEntity( "entity" ); pc.setMapper( "mapper" ); pc.setService( "service" ); pc.setController( "controller" ); mpg.setPackageInfo(pc); |
以上面代码为例,*Mapper.xml
文件位置是mapper/xml/*Mapper.xml
4.2 修改*Mapper.xml文件生成位置
step1:在模板中控制不生成xml文件 防止重复生成
1
|
templateConfig.setXml( null ); |
step2:在第三步中mpg.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
|
// 自定义配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker // String templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity String templatePath = "/templates/mapper.xml.vm" ; // 自定义输出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定义配置会被优先输出 // 这里自定义配置的是*Mapper.xml文件 // 所以templatePath = "/templates/mapper.xml.vm"; // 如果你想自定义配置其它 修改templatePath即可 focList.add( new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { // 自定义输出文件名 如果你 Entity 设置了前后缀 String entityName = tableInfo.getEntityName(); int length = entityName.length(); entityName = entityName.substring( 0 , length - 6 ); return proPath + "/src/main/resources/mapper/" + entityName + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); |
到此这篇关于mybatis-plus使用generator实现逆向工程的文章就介绍到这了,更多相关mybatis-plus generator 逆向工程内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.cn/post/7095284828781674509