本文主要详细介绍一下SpringBoot整合,使用SpringBoot进行开发更加迅速,利用SpringBoot对SSM+Junit+Redis进行整合。整合代码已经上传github,并且会持续更新其它整合点击此处下载代码
开发环境:
IntelliJ IDEA 2018.3
JDK1.9
MySql5.0.22
SpringBoot2.0.1
1.使用idea快速创建SpringBoot项目(Spring+SpringMVC)
项目的目录结构为
将spring-boot-starter-parent版本改为2.0.1.RELEASE1
2
3
4
5
6<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.SpringBoot整合Mybatis
2.1添加Mybatis的起步依赖
1 | <!--mybatis起步依赖--> |
2.2添加数据库驱动坐标
1 | <!-- MySQL连接驱动 --> |
2.3添加数据库连接信息
在application.properties中添加数据库的连接信息1
2
3
4
5#DB Configuration:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
2.4创建user表
在test数据库中创建user表1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');
2.5创建实体Bean
1 | public class User { |
2.6编写Mapper
1 |
|
注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中
2.7配置Mapper映射文件
在src\main\resources\mapper路径下加入UserMapper.xml配置文件1
2
3
4
5
6
7<?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.itheima.mapper.UserMapper">
<select id="queryUserList" resultType="user">
select * from user
</select>
</mapper>
2.8在application.properties中添加mybatis的信息
1 | #spring集成Mybatis环境 |
测试在Junit集成之后进行
3.SpringBoot整合Junit
3.1添加Mybatis的起步依赖
1 | <!--测试的起步依赖--> |
3.2编写Mybatis的测试类
1 | (SpringRunner.class) |
其中,SpringRunner继承自SpringJUnit4ClassRunner,使用哪一个Spring提供的测试测试引擎都可以1
public final class SpringRunner extends SpringJUnit4ClassRunner
@SpringBootTest的属性指定的是引导类的字节码对象
4.SpringBoot整合Redis
4.1添加redis的起步依赖
1 | <dependency> |
4.2配置redis的连接信息
1 | #Redis |
4.3注入RedisTemplate测试redis操作
1 | (SpringRunner.class) |
5.SpringBoot开发使用thymeleaf模板开发html页面
5.1添加thymeleaf模板开发html页面的起步依赖
1 | <!-- thymeleaf模板开发html页面 --> |
5.2在application.yml配置thymeleaf
1 | spring: |
5.3编写Controller
controller层,我写了三个方法,首页,跳转,和视图的三种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
public class MapperController {
/**
* 首页
*
* @return
*/
("/welcome")
public String page() {
return "system/index";
}
/**
* 跳转
*
* @return
*/
("/redirect")
public String page2() {
return "redirect/redirect";
}
/**
* 视图
*
* @param model
* @return
*/
("/model")
public String page3(Model model) {
model.addAttribute("name", "xiaoming");
return "hello";
}
}
5.4编写页面
页面结构如下图所示
redirect.html页面代码1
2
3
4
5
6
7
8
9
10<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>redirectPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>congratulation redirect success !</h1>
</body>
</html>
index.html页面代码1
2
3
4
5
6
7
8
9
10
11
12<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>thymeleaf</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Welcome page of thymeleaf.</h1>
<a href="/springboot/redirect">redirect</a>
<a href="/springboot/model">model</a>
</body>
</html>
hello.html页面代码1
2
3
4
5
6
7
8
9
10
11<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>model</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>welcome : [[${name}]].</p>
<h1>hi , [[${name}]] !</h1>
</body>
</html>