创建个springboot项目
勾选生所需的依赖:
我把application的后缀改为.yml了,方便些。
pom.xml:
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
|
<?xml version= "1.0" encoding= "utf-8" ?>
<project xmlns= "http://maven.apache.org/pom/4.0.0" xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
xsi:schemalocation= "http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelversion> 4.0 . 0 </modelversion>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version> 2.4 . 4 </version>
<relativepath/> <!-- lookup parent from repository -->
</parent>
<groupid>com.keafmd</groupid>
<artifactid>springboot-mybatisplus</artifactid>
<version> 0.0 . 1 -snapshot</version>
<name>springboot-mybatisplus</name>
<description>demo project for spring boot</description>
<properties>
<java.version> 1.8 </java.version>
</properties>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-jdbc</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-devtools</artifactid>
<scope>runtime</scope>
<optional> true </optional>
</dependency>
<dependency>
<groupid>mysql</groupid>
<artifactid>mysql-connector-java</artifactid>
<scope>runtime</scope>
</dependency>
<dependency>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
<optional> true </optional>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
<configuration>
<excludes>
<exclude>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
因为我们配置了数据源,所以需要在application.yml中配置下数据源,不然会起不来,我顺便也改了下端口。
application.yml:
1
2
3
4
5
6
7
8
|
server:
port: 80
spring:
datasource:
url: jdbc:mysql: //127.0.0.1:3306/ssm-java1?usessl=false&&characterencoding=utf-8
driver- class -name: com.mysql.cj.jdbc.driver
username: root
password: 18044229
|
写个hellocontroller测试下
hellocontroller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.keafmd.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* keafmd
*
* @classname: hellocontroller
* @description:
* @author: 牛哄哄的柯南
* @date: 2021-04-09 11:11
* @blog: https://keafmd.blog.csdn.net/
*/
@restcontroller
public class hellocontroller {
@requestmapping ( "/hello" )
public string hello(){
return "keafmd" ;
}
}
|
运行启动类,访问:http://127.0.0.1/hello
到此证明springboot没有问题。
使用代码生成器生成代码
添加所需的依赖
pom.xml中添加以下依赖:
1
2
3
4
5
6
7
8
9
10
11
12
|
<dependency>
<groupid>com.baomidou</groupid>
<artifactid>mybatis-plus-generator</artifactid>
<scope>test</scope>
<version> 3.4 . 0 </version>
</dependency>
<dependency>
<groupid>org.freemarker</groupid>
<artifactid>freemarker</artifactid>
<scope>test</scope>
<version> 2.3 . 31 </version>
</dependency>
|
由于代码生成器并不会在生产环境使用,只是在开发环境中使用了下。所以我们把代码生成器写在test包中即可,依赖的使用场景也定义成test即可。
codegenerator
codegenerator:
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
|
package com.keafmd.mp;
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;
/**
* keafmd
*
* @classname: codegenerator
* @description:
* @author: 牛哄哄的柯南
* @date: 2021-03-23 21:47
*/
// 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class codegenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
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.isnotblank(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" );
// system.out.println("projectpath = " + projectpath);
gc.setoutputdir(projectpath + "/src/main/java" );
// gc.setoutputdir("d:\\test");
gc.setauthor( "关注公众号:牛哄哄的柯南" );
gc.setopen( false );
// gc.setswagger2(true); 实体属性 swagger2 注解
gc.setservicename( "%sservice" );
mpg.setglobalconfig(gc);
// 数据源配置
datasourceconfig dsc = new datasourceconfig();
dsc.seturl( "jdbc:mysql://localhost:3306/ssm-java1?useunicode=true&usessl=false&characterencoding=utf8&servertimezone=utc" );
// dsc.setschemaname("public");
dsc.setdrivername( "com.mysql.cj.jdbc.driver" );
dsc.setusername( "root" );
dsc.setpassword( "18044229" );
mpg.setdatasource(dsc);
// 包配置
packageconfig pc = new packageconfig();
pc.setmodulename( null );
pc.setparent( "com.keafmd" );
mpg.setpackageinfo(pc);
// 自定义配置
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<>();
// 自定义配置会被优先输出
foclist.add( new fileoutconfig(templatepath) {
@override
public string outputfile(tableinfo tableinfo) {
// 自定义输出文件名 , 如果你 entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectpath + "/src/main/resources/mapper/"
+ "/" + tableinfo.getentityname() + "mapper" + stringpool.dot_xml;
}
});
cfg.setfileoutconfiglist(foclist);
mpg.setcfg(cfg);
// 配置模板
templateconfig templateconfig = new templateconfig();
templateconfig.setxml( null );
mpg.settemplate(templateconfig);
// 策略配置
strategyconfig strategy = new strategyconfig();
strategy.setnaming(namingstrategy.underline_to_camel);
strategy.setcolumnnaming(namingstrategy.underline_to_camel);
strategy.setentitylombokmodel( true );
strategy.setrestcontrollerstyle( true );
strategy.setinclude(scanner( "表名,多个英文逗号分割" ).split( "," ));
strategy.setcontrollermappinghyphenstyle( true );
strategy.settableprefix( "m_" );
mpg.setstrategy(strategy);
mpg.settemplateengine( new freemarkertemplateengine());
mpg.execute();
}
}
|
运行代码生成器,在控制台输入想要生成的表
这样就会生成一些包及相应的代码,注意codegenerator中的相关代码(如数据库的,包名的)需要该成你们需要的。
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注服务器之家的更多内容!
原文链接:https://blog.csdn.net/weixin_43883917/article/details/115542452