SpringBoot项目技术整合

集成持久层框架Mybatis

集成mybatis框架,启动成功

父porm.xml集成mybatis

1
2
3
4
5
6
7
8
9
10
11
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>

子system/pom.xml的引用父的,所以不需要带版本号

1
2
3
4
5
6
7
8
9
<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

system/src/main/resources/application.properties

1
2
3
4
spring.datasource.url=jdbc:mysql://localhost:3306/courseimooc?characterEncoding=UTF8&autoReconnect=true
spring.datasource.username=courseimooc
spring.datasource.password=courseimooc
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis使用示例:http://127.0.0.1:9001/system/test

system/src/main/java/com/course/system/SystemApplication.java

1
2
@ComponentScan("com.course") //@ComponentScan 注解中指定了要扫描的包。
@MapperScan("com.course.system.mapper") //

添加@MapperScan("com.course.system.mapper")注解以后,com.course.system.mapper包下面的接口类,在编译之后都会生成相应的实现类。

添加@ComponentScan("com.course"),让spring扫描com.course这个包下面的类,将扫描到的类装配到spring容器中。带有下面几种常见注解的类,都会被扫描到。

1
2
3
4
@Component
@Controller
@RestController
@Service

system/src/main/resources/application.properties

1
mybatis.mapper-locations=classpath:/mapper/*.xml

增加三层结构

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class TestController {

@Resource
private TestService testService;

@RequestMapping("/test")
public List<Test> test() {
return testService.list();
}
}

system/src/main/java/com/course/system/domain/Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.course.system.domain;

public class Test {

private String id;

private String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

system/src/main/java/com/course/system/mapper/TestMapper.java

1
2
3
4
5
6
7
8
9
10
package com.course.system.mapper;

import com.course.system.domain.Test;

import java.util.List;

public interface TestMapper {

public List<Test> list();
}

system/src/main/java/com/course/system/service/TestService.java

1
2
3
4
5
6
7
8
9
10
@Service
public class TestService {

@Resource
private TestMapper testMapper;

public List<Test> list() {
return testMapper.list();
}
}

system/src/main/resources/mapper/TestMapper.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.course.system.mapper.TestMapper" >

<select id="list" resultType="com.course.system.domain.Test">
select `id`, `name` from `test`
</select>

</mapper>

项目优化

使用database关联数据库

打开idea,点击Database新增MySQL连接。

来到doc/db/all.sql下就可以执行sql脚本

新增数据库脚本

1
2
3
4
5
6
7
8
9
drop table if exists `test`;
create table `test` (
`id` char(8) not null default '' comment 'id',
`name` varchar(50) comment '名称',
primary key (`id`)
) engine=innodb default charset=utf8mb4 comment='测试';

insert into `test` (id, name) values (1, '测试');
insert into `test` (id, name) values (2, '测试2');

集成热部署DevTools

1. 引入依赖system/pom.xml

1
2
3
4
5
<!-- 热部署DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
  1. IDEA开启项目自动编译,进入设置,搜索Compiler,勾选Build Project automatically
  1. IDEA开启项目运行时自动编译,连按两次shift,搜索registry -> 勾选compiler.automake.allow.when.app.running

4.优化mybatis日志输出
system/src/main/resources/application.properties

1
logging.level.com.course.system.mapper=trace

搭建服务模块-server

新建公共模块server

父pom.xml新建server模块

1
2
3
4
5
6
<modules>
<module>eureka</module>
<module>system</module>
<module>gateway</module>
<module>server</module>
</modules>

server/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
<dependencies>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- 热部署DevTools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

server/src/main/java/com/course/server/domain/Test.java

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
package com.course.server.domain;

public class Test {

private String id;

private String name;
private String test;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

server/src/main/java/com/course/server/mapper/TestMapper.java

1
2
3
4
5
6
7
8
9
10
package com.course.server.mapper;

import com.course.server.domain.Test;

import java.util.List;

public interface TestMapper {

public List<Test> list();
}

server/src/main/java/com/course/server/service/TestService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.course.server.service;

import com.course.server.domain.Test;
import com.course.server.mapper.TestMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestService {

@Resource
private TestMapper testMapper;

public List<Test> list() {
return testMapper.list();
}
}

server/src/main/resources/mapper/TestMapper.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.course.TestMapper" >

<select id="list" resultType="com.course.server.domain.Test">
select `id`, `name` from `test`
</select>

</mapper>

system/src/main/java/com/course/system/domain/Test.java

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
package com.course.system.domain;

public class Test {

private String id;

private String name;
private String test;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

system/src/main/resources/mapper/TestMapper.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.course.TestMapper" >

<select id="list" resultType="com.course.Test">
select `id`, `name` from `test`
</select>

</mapper>

system集成server模块

父pom.xml集成server

1
2
3
4
5
<dependency>
<groupId>com.javami</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

server/src/main/resources/mapper/TestMapper.xml

1
2
3
4
5
6
7
<mapper namespace="com.javami.course.server.mapper.TestMapper" >

<select id="list" resultType="com.javami.course.server.domain.Test">
select `id`, `name` from `test`
</select>

</mapper>

system/pom.xml

1
2
3
4
<dependency>
<groupId>com.javami</groupId>
<artifactId>server</artifactId>
</dependency>

system/src/main/java/com/course/system/controller/TestController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.javami.course.system.controller.TestController;

import com.javami.course.server.domain.Test;
import com.javami.course.server.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController {

@Resource
private TestService testService;

@RequestMapping("/test")
public List<Test> test() {
return testService.list();
}
}

将通用的配置移动到server

注:server配置文件的路径要放在resources/config/application.properties,不能和上层的路径一样放在resoures根目录下

server/src/main/resources/config/application.properties

1
2
3
4
5
6
7
8
spring.datasource.url=jdbc:mysql://localhost:3306/courseimooc?characterEncoding=UTF8&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=meiyi8013
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapper-locations=classpath:/mapper/*.xml

logging.level.com.javami.course.server.mapper=trace

system/src/main/resources/application.properties

1
2
3
4
spring.application.name=system
server.servlet.context-path=/system
server.port=9001
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

集成mybatis generator

集成mybatis-generator

步骤1:添加idea插件
步骤2:添加server\src\main\resources\generator\generatorConfig.xml
步骤3:创建maven启动命令mybatis-generator:generate -e

父pom.xml,添加mybatis generator自动生成代码插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
</dependencies>
</plugin>

server/src/main/resources/generator/generatorConfig.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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">

<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>

<!-- 生成的实体类添加toString()方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />

<!-- 不生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>

<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/courseimooc"
userId="courseimooc"
password="courseimooc">
</jdbcConnection>

<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
targetPackage="com.course.server.domain"/>

<!-- mapper xml的位置 -->
<sqlMapGenerator targetProject="src\main\resources"
targetPackage="mapper"/>

<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
targetPackage="com.course.server.mapper"
type="XMLMAPPER" />

<table tableName="test" domainObjectName="Test"/>
</context>
</generatorConfiguration>

解决mapper.xml重复生成代码的问题

server/src/main/resources/generator/generatorConfig.xml

1
2
<!--覆盖生成XML文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

Example使用示例

使用Example可以快速完成单表的增删改查,熟练掌握后对代码开发效率将有一个质的飞跃。

server/src/main/java/com/course/server/service/TestService.java

1
2
3
4
5
6
public List<Test> list() {
TestExample testExample = new TestExample();
testExample.createCriteria().andIdEqualTo("1");
testExample.setOrderByClause("id desc");
return testMapper.selectByExample(testExample);
}