Skip to content

Commit 3f35698

Browse files
committed
第六篇 : SpringBoot 整合 mybatis-plus
1 parent 2caadf7 commit 3f35698

15 files changed

Lines changed: 488 additions & 0 deletions

File tree

springboot-mybatis/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
47.2 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

springboot-mybatis/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.gf</groupId>
7+
<artifactId>springboot-mybatis</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-mybatis</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.1.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>mysql</groupId>
35+
<artifactId>mysql-connector-java</artifactId>
36+
<scope>runtime</scope>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<optional>true</optional>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.baomidou</groupId>
47+
<artifactId>mybatis-plus-boot-starter</artifactId>
48+
<version>3.0.1</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.apache.velocity</groupId>
53+
<artifactId>velocity</artifactId>
54+
<version>1.7</version>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-test</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
</dependencies>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
74+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gf;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootMybatisApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootMybatisApplication.class, args);
11+
}
12+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.gf.config;
2+
3+
4+
import com.baomidou.mybatisplus.annotation.DbType;
5+
import com.baomidou.mybatisplus.core.toolkit.StringPool;
6+
import com.baomidou.mybatisplus.generator.AutoGenerator;
7+
import com.baomidou.mybatisplus.generator.InjectionConfig;
8+
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
9+
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
10+
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
11+
import com.baomidou.mybatisplus.generator.config.PackageConfig;
12+
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
13+
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
14+
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
15+
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
21+
public class CodeGenerator {
22+
23+
public static void main(String[] args) throws InterruptedException {
24+
AutoGenerator mpg = new AutoGenerator();
25+
26+
// 全局配置
27+
GlobalConfig gc = new GlobalConfig();
28+
String projectPath = System.getProperty("user.dir");
29+
gc.setOutputDir(projectPath + "/src/main/java");
30+
gc.setFileOverride(true);
31+
gc.setActiveRecord(true);
32+
gc.setEnableCache(false);// XML 二级缓存
33+
gc.setBaseResultMap(true);// XML ResultMap
34+
gc.setBaseColumnList(true);// XML columList
35+
gc.setOpen(false);
36+
gc.setAuthor("gf");
37+
38+
39+
// 自定义文件命名,注意 %s 会自动填充表实体属性!
40+
gc.setMapperName("%sMapper");
41+
gc.setXmlName("%sMapper");
42+
gc.setServiceName("%sService");
43+
gc.setServiceImplName("%sServiceImpl");
44+
gc.setControllerName("%sController");
45+
46+
mpg.setGlobalConfig(gc);
47+
48+
// 数据源配置
49+
DataSourceConfig dsc = new DataSourceConfig();
50+
dsc.setDbType( DbType.MYSQL);
51+
dsc.setDriverName("com.mysql.jdbc.Driver");
52+
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useSSL=false");
53+
dsc.setUsername("root");
54+
dsc.setPassword("xxxxxx");
55+
mpg.setDataSource(dsc);
56+
57+
// 包配置
58+
PackageConfig pc = new PackageConfig();
59+
pc.setParent("com.gf");
60+
pc.setController( "controller");
61+
pc.setEntity( "entity" );
62+
//pc.setModuleName("test");
63+
mpg.setPackageInfo(pc);
64+
65+
// 自定义配置
66+
InjectionConfig cfg = new InjectionConfig() {
67+
@Override
68+
public void initMap() {
69+
// to do nothing
70+
}
71+
};
72+
List<FileOutConfig> focList = new ArrayList<>();
73+
focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
74+
@Override
75+
public String outputFile(TableInfo tableInfo) {
76+
// 自定义输入文件名称
77+
return projectPath + "/src/main/resources/mapper/"
78+
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
79+
}
80+
});
81+
cfg.setFileOutConfigList(focList);
82+
mpg.setCfg(cfg);
83+
mpg.setTemplate(new TemplateConfig().setXml(null));
84+
85+
// 策略配置
86+
StrategyConfig strategy = new StrategyConfig();
87+
//此处可以修改为您的表前缀
88+
strategy.setTablePrefix(new String[] { "tb_"});
89+
// 表名生成策略
90+
strategy.setNaming(NamingStrategy.underline_to_camel);
91+
// 需要生成的表
92+
strategy.setInclude(new String[] { "tb_employee" });
93+
// 排除生成的表
94+
//strategy.setExclude(new String[]{"test"});
95+
strategy.setEntityLombokModel( true );
96+
97+
mpg.setStrategy(strategy);
98+
99+
// 执行生成
100+
mpg.execute();
101+
}
102+
103+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.gf.controller;
2+
3+
4+
import com.gf.entity.Employee;
5+
import com.gf.service.EmployeeService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.util.List;
15+
16+
/**
17+
* <p>
18+
* 前端控制器
19+
* </p>
20+
*
21+
* @author gf
22+
* @since 2018-11-25
23+
*/
24+
@RestController
25+
@RequestMapping("/employee")
26+
public class EmployeeController {
27+
28+
29+
@Autowired
30+
EmployeeService employeeService;
31+
32+
@RequestMapping(value = "/list", method = RequestMethod.GET)
33+
public List<Employee> getEmployees() {
34+
return employeeService.list( null );
35+
}
36+
37+
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
38+
public Employee getEmployeeById(@PathVariable("id") int id) {
39+
return employeeService.getById( id );
40+
}
41+
42+
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
43+
public String updateEmployee(@PathVariable("id") int id, @RequestParam(value = "lastName", required = true) String lastName,
44+
@RequestParam(value = "email", required = true) String email , @RequestParam(value = "gender", required = true) int gender , @RequestParam(value = "dId", required = true) int dId) {
45+
Employee employee = new Employee();
46+
employee.setId( id );
47+
employee.setLastName( "张" );
48+
employee.setEmail( "[email protected]" );
49+
employee.setGender( 1 );
50+
employee.setDId( 1 );
51+
52+
boolean b = employeeService.updateById( employee );
53+
54+
if (b) {
55+
return "update success";
56+
} else {
57+
return "update fail";
58+
}
59+
60+
}
61+
62+
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
63+
public String delete(@PathVariable(value = "id")int id) {
64+
boolean b = employeeService.removeById( id );
65+
66+
if(b) {
67+
return "delete success";
68+
}else {
69+
return "delete fail";
70+
}
71+
72+
}
73+
74+
@RequestMapping(value = "", method = RequestMethod.POST)
75+
public String postEmployee(@RequestParam(value = "lastName", required = true) String lastName,
76+
@RequestParam(value = "email", required = true) String email , @RequestParam(value = "gender", required = true) int gender , @RequestParam(value = "dId", required = true) int dId) {
77+
78+
Employee employee = new Employee();
79+
employee.setLastName( "王" );
80+
employee.setEmail( "[email protected]" );
81+
employee.setGender( 2 );
82+
employee.setDId( 2 );
83+
boolean b = employeeService.save( employee );
84+
85+
if(b) {
86+
return "sava success";
87+
}else {
88+
return "sava fail";
89+
}
90+
91+
}
92+
93+
94+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.gf.entity;
2+
3+
import com.baomidou.mybatisplus.annotation.TableName;
4+
import com.baomidou.mybatisplus.annotation.IdType;
5+
import com.baomidou.mybatisplus.extension.activerecord.Model;
6+
import com.baomidou.mybatisplus.annotation.TableId;
7+
import com.baomidou.mybatisplus.annotation.TableField;
8+
import java.io.Serializable;
9+
import lombok.Data;
10+
import lombok.EqualsAndHashCode;
11+
import lombok.experimental.Accessors;
12+
13+
/**
14+
* <p>
15+
*
16+
* </p>
17+
*
18+
* @author gf
19+
* @since 2018-11-25
20+
*/
21+
@Data
22+
@EqualsAndHashCode(callSuper = false)
23+
@Accessors(chain = true)
24+
@TableName("tb_employee")
25+
public class Employee extends Model<Employee> {
26+
27+
private static final long serialVersionUID = 1L;
28+
29+
@TableId(value = "id", type = IdType.AUTO)
30+
private Integer id;
31+
32+
@TableField("lastName")
33+
private String lastName;
34+
35+
private String email;
36+
37+
private Integer gender;
38+
39+
private Integer dId;
40+
41+
42+
@Override
43+
protected Serializable pkVal() {
44+
return this.id;
45+
}
46+
47+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.gf.mapper;
2+
3+
import com.gf.entity.Employee;
4+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5+
import org.apache.ibatis.annotations.Mapper;
6+
7+
/**
8+
* <p>
9+
* Mapper 接口
10+
* </p>
11+
*
12+
* @author gf
13+
* @since 2018-11-25
14+
*/
15+
@Mapper
16+
public interface EmployeeMapper extends BaseMapper<Employee> {
17+
18+
}

0 commit comments

Comments
 (0)