Java 之 Spring Boot 入门到精通【IDEA 版】SpringBoot 整合其他框架
![](https://static001.geekbang.org/infoq/76/7665ad92a113520f188b4e76f386e3c8.png)
![](https://static001.geekbang.org/infoq/de/de8b660eafaed5a8b81837cb2ae7e111.png)
![](https://static001.geekbang.org/infoq/43/433424fb73b6ffea04e1f21204ed44f9.png)
![](https://static001.geekbang.org/infoq/da/da4e529ef7be18073b9c788bb80833ce.png)
![](https://static001.geekbang.org/infoq/9d/9d42ec3c175ddd3383f5d65e044e305c.png)
[](()2、引入 redis 起步依赖
通过上述的自动创建的工程 redis 的依赖已经自动添加好
![](https://static001.geekbang.org/infoq/ce/ce60edae69fca485851fbb1ef6273d25.png)
[](()3、在测试方法当中默认连接本地的 redis 数据库
不需要配置如何信息
![](https://static001.geekbang.org/infoq/87/871c219106a1ab72dcef406adbbae5fd.png)
package cn.itbluebox.springbootredis;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testSet() {
//存入数据
redisTemplate.boundValueOps("name").set("张三");
}
@Test
void testGet() {
//获取数据
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
启动 redis
![](https://static001.geekbang.org/infoq/f6/f680525922326fd88b39ec54e892b40a.png)
![](https://static001.geekbang.org/infoq/c3/c3df57b84e42e67c77afa5ed8d1c98ed.png)
运行测试类(测试方法 testSet()存入数据)
![](https://static001.geekbang.org/infoq/78/780fa5389e235bea3e4cdf4b7e0f1de0.png)
![](https://static001.geekbang.org/infoq/c5/c5f828d76a26023fc6f84bef52ef317e.png)
运行测试类(测试方法 testGet()获取数据)
![](https://static001.geekbang.org/infoq/ac/ac7a5bf0b776aac82b8fc7d6a6ff88e8.png)
![](https://static001.geekbang.org/infoq/74/74d157e77d34179e8c9c2aff6d81b15e.png)
[](()4、配置 redis 相关属性
![](https://static001.geekbang.org/infoq/a3/a37a5d70fb2b53165db25b3be7798b1c.png)
![](https://static001.geekbang.org/infoq/89/8955a1edf453d9b46fb60628d685d576.png)
![](https://static001.geekbang.org/infoq/e6/e61bd9426e820fad826afa1947d10624.png)
spring:
redis:
host: 127.0.0.1 #redis 的主机地址
port: 6379
运行测试
![](https://static001.geekbang.org/infoq/f9/f96401692f8d5d28cc216afa71795a7c.png)
![](https://static001.geekbang.org/infoq/9f/9f66907bfc49a13322b73d028184596a.png)
![](https://static001.geekbang.org/infoq/e9/e9941870076ea22c2ecae1114b1ac1a3.png)
[](()三、SpringBoot 整合 MyBatis
[](()1、搭建 SpringBoot 工程
![](https://static001.geekbang.org/infoq/0f/0f77ffb88681a0143c45e700c2b90b5f.png)
![](https://static001.geekbang.org/infoq/2d/2dd5f2a58ce7929344f38ba51d759f9c.png)
![](https://static001.geekbang.org/infoq/b2/b27186fa00926372058105bb05561f67.png)
![](https://static001.geekbang.org/infoq/9b/9b4256dd2a44258d582e0d10880a1de4.png)
![](https://static001.geekbang.org/infoq/1a/1a75cbb5cd1c5b98ae2836c396801355.png)
[](()2、引入 mybatis 起步依赖,添加 mysq|驱动
通过上述的工程搭建自动添加好了依赖
![](https://static001.geekbang.org/infoq/74/74f544af4e77f061bb70500e1c333088.png)
[](()3、定义表和实体类
创建数据库
CREATE DATABASE /!32312 IF NOT EXISTS/springboot
/*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci */;
USE springboot
;
/*Table structure for table t_user
*/
DROP TABLE IF EXISTS t_user
;
CREATE TABLE t_user
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
password
varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*Data for the table t_user
*/
insert into t_user
(id
,username
,password
) values (1,'zhangsan','123'),(2,'lisi','234');
![](https://static001.geekbang.org/infoq/66/66d3685d8c8723866b561b9296253e8b.png)
创建实体类
![](https://static001.geekbang.org/infoq/54/54f11234b8d253bfc14d5cb323c9ef60.png)
![](https://static001.geekbang.org/infoq/26/260ef63cf17e6dc5b7900bfcae132a6c.png)
package cn.tbluebox.springbootmybatis.domain;
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + ''' +
", password='" + password + ''' +
'}';
}
}
[](()4、编写 DataSource 和 MyBatis 相关配置
![](https://static001.geekbang.org/infoq/4e/4efbb59b922440dbefbff5b3543f6c56.png)
![](https://static001.geekbang.org/infoq/85/85fdc96ce5d67834096642ed39f54ec7.png)
![](https://static001.geekbang.org/infoq/2a/2acc4c794e9df94e4532c54dad9740bf.png)
#datasource
spring:
datasource:
url: jdbc:mysql:///springboot
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
[](()5、编写 dao 和 mapper 文件/纯注解开发
![](https://static001.geekbang.org/infoq/04/044d47ffd6c0df121ded2c39e7241906.png)
![](https://static001.geekbang.org/infoq/fc/fcd759f5114b994abb370980f86757cc.png)
package cn.tbluebox.springbootmybatis.mapper;
import cn.tbluebox.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
[](()6、测试
![](https://static001.geekbang.org/infoq/2b/2b288cfddca0de9885690985760d1716.png)
package cn.tbluebox.springbootmybatis;
import cn.tbluebox.springbootmybatis.domain.User;
import cn.tbluebox.springbootmybatis.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void findAllUser() {
List<User> all = userMapper.findAll();
System.out.println(all);
}
}
运行测试类
![](https://static001.geekbang.org/infoq/ec/ec4707a51a2a95457c4c46feb7aeb128.png)
![](https://static001.geekbang.org/infoq/bf/bfb9e1326001856eb97bc26b60a3196b.png)
[](()7、编写 dao 和 mapper 文件/XML 开发
[](()(1)创建 UserXmlMapper 接口
![](https://static001.geekbang.org/infoq/91/91df5f3ece7051efafdbc0db2dfcb9bf.png)
![](https://static001.geekbang.org/infoq/9a/9a45c88c596f80a574215ababbd132ac.png)
package cn.tbluebox.springbootmybatis.mapper;
import cn.tbluebox.springbootmybatis.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserXmlMapper {
public List<User> findAll();
}
[](()(2) 编写配置文件
![](https://static001.geekbang.org/infoq/7f/7fe53f8a8c0a30f49df620d3ea26a34f.png)
![](https://static001.geekbang.org/infoq/62/62ee030596616a0ef7f07abe123e5e2f.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/32c070ca8b0b42629c3e71ca467b566a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6JOd55uS5a2QaXRib 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 HVlYm94,size_20,color_FFFFFF,t_70,g_se,x_16)
<?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="cn.tbluebox.springbootmybatis.mapper.UserXmlMapper">
<select id="findAll" resultType="user">
select * from t_user
</select>
</mapper>
[](()(3) 配置配置文件
![](https://static001.geekbang.org/infoq/98/98e67a0de0811ab8ba783c1ba5b900b3.png)
#datasource
spring:
datasource:
url: jdbc:mysql:///springboot
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml #mapper 映射文件的路径
type-aliases-package: cn.tbluebox.springbootmybatis.domain
config-location: #指定 mybatis 核心配置文件
[](()(4)完善测试类
![](https://static001.geekbang.org/infoq/42/4209423c80648b33cc0b509179cb674a.png)
package cn.tbluebox.springbootmybatis;
import cn.tbluebox.springbootmybatis.domain.User;
import cn.tbluebox.springbootmybatis.mapper.UserMapper;
import cn.tbluebox.springbootmybatis.mapper.UserXmlMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
评论