学习目标

  1. 能够理解SpringBoot设计理念
  2. 能够使用idea工具构建SpringBoot项目
  3. 能够熟的应用SpringBoot配置文件
  4. 能够熟练的整合mybatis、redis
  5. 能够运用代码测试工具
  6. 理解版本控制的原理

1 Springboot的介绍

1.1 目标

能够理解springboot的设计理念,知道springboot用来解决什么问题

1.2 学习路径

  • 目前项目开发的一些问题
  • springboot的解决的问题
  • springboot的特点和介绍

1.3 讲解

1.3.1 项目中开发的一些问题?

目前我们开发的过程当中,一般采用一个单体应用的开发采用SSM等框架进行开发,并在 开发的过程当中使用了大量的xml等配置文件,以及在开发过程中使用MAVEN的构建工具来进行构建项目,但是往往有时也会出现依赖的一些冲突,而且开发的时候测试还需要下载和使用tomcat等等这些servlet容器,所以开发的效率不高。

1.3.2 springboot解决的问题

那么在以上的问题中,我们就可以使用springboot来解决这些问题,当然他不仅仅是解决这些问题,来提高我们的开发人员的开发效率。使用springboot:可以不需要配置,可以不需要自己单独去获取tomcat,基本解决了包依赖冲突的问题,一键发布等等特性。

1.3.3 springboot的介绍

官方的说明:

  1. Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
  2. We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.

特性:

  1. 创建独立的Spring应用程序
  2. 直接嵌入TomcatJettyUndertow(无需部署WAR文件)
  3. 提供“入门”依赖项(起步依赖),以简化构建配置
  4. 尽可能自动配置Spring和第三方库
  5. 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
  6. 完全没有代码生成,也不需要XML配置

1.4 小结

  1. springboot 就是一个基于spring的一个框架。提供了一些自动配置的依赖包,自动嵌入servlet的容器,简化了我们开发的配置,提升开发人员的开发效率,并解决了包依赖的问题。

2 Springboot使用入门

2.1 目标

能使用idea实现springboot的入门程序

2.2 学习路径

  • 入门的需求
  • 使用环境的准备
  • 实现配置和开发
  • 测试和小结

2.3 讲解

2.3.1 入门需求

使用springboot在页面中展示一个hello world

2.3.2 环境准备

推荐:使用springboot版本 springboot 2.1.4.RELEASE

推荐:jdk1.8

2.3.3 开始开发和配置

(1)创建空工程

springboot-day01 - 图1

(2)创建maven工程

springboot-day01 - 图2

springboot-day01 - 图3

(3)配置pom.xml

  • 添加parent
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.4.RELEASE</version>
  5. </parent>
  • 添加起步依赖
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  • pom.xml的整体代码如下
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.itheima</groupId>
  7. <artifactId>itheima-springboot-demo</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.1.4.RELEASE</version>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. </dependencies>
  20. </project>

(4)创建启动类(或者 叫引导类)

  1. @SpringBootApplication
  2. public class SpringbootApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringbootApplication.class,args);
  5. }
  6. }

(5)创建controller 实现展示hello world

  1. @RestController
  2. public class TestController {
  3. @RequestMapping("/hello")
  4. public String showHello(){
  5. return "hello world";
  6. }
  7. }

(6)测试

启动启动类的main方法,在浏览器中输入localhost:8080/hello,则在页面中显示hello world

springboot-day01 - 图4

springboot-day01 - 图5

2.4 小结

上边的入门程序 很快开发完成,没有任何的配置,只需要添加依赖,设置springboot的parent,创建引导类即可。springboot的设置parent用于管理springboot的依赖的版本,起步依赖快速的依赖了在web开发中的所需要的依赖项。

springboot-day01 - 图6

2.5 Spring Initializr的方式创建springboot工程

2.5.1 目标

了解相关的操作步骤实现创建springboot项目。

2.5.2 讲解

可以使用idea提供的方式来快速的创建springboot的项目,更加的方便,但是要求需要 联网而且网络需要比较稳定才行。如下步骤:

(1)选中spring initializr

springboot-day01 - 图7

(2)设置包名和坐标名

springboot-day01 - 图8

(3)设置添加依赖

springboot-day01 - 图9

(4)生成成功,自动生成相关引导类,添加依赖了。不需要手动的进行添加。如下图:

springboot-day01 - 图10

解释:

  1. --ItheimaDemoInitalApplication类为引导类(启动类)
  2. --static 目录用于存储静态资源
  3. --templates 目录用于存储模板文件
  4. --application.properties 用于配置相关的使用到的属性,所有的配置基本上都需要在这里配置,需要注意的是名字不能修改。

3 Springboot的配置文件

SpringBoot是约定大于配置的,所以很多配置都有默认值。如果想修改默认配置,可以用application.properties或application.yml(application.yaml)自定义配置。SpringBoot默认从Resource目录加载自定义配置文件。

3.1 目标

  • 掌握常用的properties的属性配置
  • 掌握常用的yaml的属性配置
  • 掌握获取配置文件中的属性值的常用的方式
  • 掌握多配置文件切换

3.2 学习路径

  • 常见的默认的配置
  • 创建自定义配置格式
  • 获取属性值
  • 多文件配置和切换

3.3 讲解

3.3.1 properties文件

properties文件的配置多以key.key.key:value的形式组成,那么springboot本身有默认的一些配置,如果要修改这些默认的配置,可以在application.properties中进行配置修改。

比如:修改端口配置

  1. server.port=8081

3.3.2 yaml或者yml文件

  1. yaml文件等价于properties文件,在使用过程中都是一样的效果。但是yml文件书写的方式和properties文件不一样。更加简洁,那么我们可以根据需要选择性的使用propertiesyml文件。如果同时存在两个文件,那么优先级properties要高于yml

语法特点如下:

  • 大小写敏感
  • 数据值前必须有空格,作为分隔符
  • 缩进的空格数目不重要,只需要对齐即可
  • # 表示注释

书写格式如下要求如下:key和key之间需要换行以及空格两次。 简单key value之间需要冒号加空格。

  1. key1:
  2. key2:
  3. key3: value
  4. key4: value4

比如:

  1. server:
  2. port: 8081

注意:yml语法中,相同缩进代表同一个级别

  1. # 基本格式 key: value
  2. name: zhangsan
  3. # 数组 - 用于区分
  4. city:
  5. - beijing
  6. - tianjin
  7. - shanghai
  8. - chongqing
  9. #集合中的元素是对象形式
  10. students:
  11. - name: zhangsan
  12. age: 18
  13. score: 100
  14. - name: lisi
  15. age: 28
  16. score: 88
  17. - name: wangwu
  18. age: 38
  19. score: 90
  20. #map集合形式
  21. maps: {"name":"zhangsan", "age": "15"}
  22. #参数引用
  23. person:
  24. name: ${name} # 该值可以获取到上边的name定义的值

3.3.3 获取配置文件中值

获取配置文件中的值我们一般有几种方式:

  • @value注解的方式 只能获取简单值
  • Environment的方式
  • @ConfigurationProperties

演示如下:

yml中配置:

  1. # 基本格式 key: value
  2. name: zhangsan
  3. # 数组 - 用于区分
  4. city:
  5. - beijing
  6. - tianjin
  7. - shanghai
  8. - chongqing
  9. #集合中的元素是对象形式
  10. students:
  11. - name: zhangsan
  12. age: 18
  13. score: 100
  14. - name: lisi
  15. age: 28
  16. score: 88
  17. - name: wangwu
  18. age: 38
  19. score: 90
  20. #map集合形式
  21. maps: {"name":"zhangsan", "age": "15"}
  22. #参数引用
  23. person:
  24. name: ${name} # 该值可以获取到上边的name定义的值
  25. age: 12

java代码:

controller

  1. @RestController
  2. public class Test2Controller {
  3. @Value("${name}")
  4. private String name;
  5. @Value("${city[0]}")
  6. private String city0;
  7. @Value("${students[0].name}")
  8. private String studentname;
  9. @Value("${person.name}")
  10. private String personName;
  11. @Value("${maps.name}")//value注解只能获简单的值对象
  12. private String name1;
  13. @Autowired
  14. private Student student;
  15. @RequestMapping("/show")
  16. public String showHello() {
  17. System.out.println(name);
  18. System.out.println(city0);
  19. System.out.println(studentname);
  20. System.out.println(personName);
  21. System.out.println(">>>>"+student.getAge());
  22. return "hello world";
  23. }
  24. }

pojo:

  1. @Component
  2. @ConfigurationProperties(prefix = "person")
  3. public class Student {
  4. private String name;
  5. private Integer age;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public Integer getAge() {
  13. return age;
  14. }
  15. public void setAge(Integer age) {
  16. this.age = age;
  17. }
  18. }

3.3.4 profile

在开发的过程中,需要配置不同的环境,所以即使我们在application.yml中配置了相关的配置项,当时在测试是,需要修改数据源等端口路径的配置,测试完成之后,又上生产环境,这时配置又需要修改,修改起来很麻烦。

  • properties配置方式
    springboot-day01 - 图11

application.properties:

  1. #通过active指定选用配置环境
  2. spring.profiles.active=test

application-dev.properties:

  1. #开发环境
  2. server.port=8081

application-test.properties

  1. server.port=8082

application-pro.properties

  1. server.port=8083
  • yml配置方式

springboot-day01 - 图12

application.yml:

  1. #通过active指定选用配置环境
  2. spring:
  3. profiles:
  4. active: pro

application-dev.yml:

  1. #开发环境
  2. server:
  3. port: 8081

application-test.yml:

  1. #测试环境
  2. server:
  3. port: 8082

applicatioin-pro.yml

  1. #生产环境
  2. server:
  3. port: 8083

还有一种是分隔符的方式(了解)

  1. spring:
  2. profiles:
  3. active: dev
  4. ---
  5. #开发环境
  6. server:
  7. port: 8081
  8. spring:
  9. profiles: dev
  10. ---
  11. #测试环境
  12. server:
  13. port: 8082
  14. spring:
  15. profiles: test
  16. ---
  17. #生产环境
  18. server:
  19. port: 8083
  20. spring:
  21. profiles: pro
  • 激活profile的方式(了解)

    • 配置文件的方式(上边已经说过)
    • 运行是指定参数 java -jar xxx.jar --spring.profiles.active=test
    • jvm虚拟机参数配置 -Dspring.profiles.active=dev

4 Springboot集成第三方框架

在springboot使用过程中,基本是是和第三方的框架整合使用的比较多,就是利用了springboot的简化配置,起步依赖的有效性。我们整合一些常见的第三方框架进行整合使用,后面会需要用到。

4.1目标

  • 掌握springboot整合mybatis
  • 掌握springboot整合redis
  • 掌握springboot整合junit

4.2 学习路径

  • 学习springboot整合junit
  • 学习springboot整合mybatis
  • 学习springboot整合redis

4.3 讲解

4.3.1 springboot整合junit

(1)添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

(2)创建在/main/java/下一个类UserService用于测试:

  1. package com.itheima.service;
  2. /***
  3. * 描述
  4. * @author ljh
  5. * @packagename com.itheima.service
  6. * @version 1.0
  7. * @date 2020/2/26
  8. */
  9. @Service
  10. public class UserService {
  11. public String getUser() {
  12. System.out.println("获取用户的信息");
  13. return "zhangsan";
  14. }
  15. }

(3)在test/java/下创建测试类,类的包名和启动类的报名一致即可

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class SpringBootApplicationTests {
  4. @Autowired
  5. private UserService userService;
  6. @Test
  7. public void getUser() {
  8. String userinfo = userService.getUser();
  9. System.out.println(userinfo);
  10. }
  11. }

解释:

  1. @RunWith(SpringRunner.class) 使用springrunner运行器
  2. @SpringBootTest 启用springboot测试
  3. 使用的方式和之前的spring的使用方式差不多。

4.3.2 springboot整合mybatis

(1)实现步骤说明

  1. 1.准备数据库创建表
  2. 2.添加起步依赖
  3. 3.创建POJO
  4. 4.创建mapper接口
  5. 5.创建映射文件
  6. 6.配置yml 指定映射文件位置
  7. 7.创建启动类,加入注解扫描
  8. 8.创建service controller 进行测试

(2)创建数据库表

  1. -- ----------------------------
  2. -- Table structure for `user`
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `user`;
  5. CREATE TABLE `user` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `username` varchar(50) DEFAULT NULL,
  8. `password` varchar(50) DEFAULT NULL,
  9. `name` varchar(50) DEFAULT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  12. -- ----------------------------
  13. -- Records of user
  14. -- ----------------------------
  15. INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
  16. INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

(3)创建工程并添加依赖pom.xml如下参考

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.itheima</groupId>
  7. <artifactId>itheima-springboot-mybatis-demo04</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>2.1.4.RELEASE</version>
  13. </parent>
  14. <dependencies>
  15. <!--驱动-->
  16. <dependency>
  17. <groupId>mysql</groupId>
  18. <artifactId>mysql-connector-java</artifactId>
  19. <scope>runtime</scope>
  20. </dependency>
  21. <!--mybatis的 起步依赖-->
  22. <dependency>
  23. <groupId>org.mybatis.spring.boot</groupId>
  24. <artifactId>mybatis-spring-boot-starter</artifactId>
  25. <version>2.0.1</version>
  26. </dependency>
  27. <!--spring web起步依赖-->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-maven-plugin</artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

(4)创建pojo

  1. public class User implements Serializable{
  2. private Integer id;
  3. private String username;//用户名
  4. private String password;//密码
  5. private String name;//姓名
  6. //getter setter...
  7. //toString
  8. }

(5)创建mapper接口

  1. package com.itheima.dao;
  2. import com.itheima.pojo.User;
  3. import java.util.List;
  4. /***
  5. * mapper接口
  6. * @author ljh
  7. * @packagename com.itheima.dao
  8. * @version 1.0
  9. * @date 2020/2/23
  10. */
  11. public interface UserMapper {
  12. public List<User> findAllUser();
  13. }

(6)创建UserMapper映射文件,如下图所示

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.itheima.dao.UserMapper">
  4. <select id="findAllUser" resultType="com.itheima.pojo.User">
  5. SELECT * from user
  6. </select>
  7. </mapper>

springboot-day01 - 图13

(7)创建配置application.yml文件:

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  5. username: root
  6. password: 123456
  7. #配置mapper的映射文件的位置
  8. mybatis:
  9. mapper-locations: classpath:mappers/*Mapper.xml

(8)创建启动类,加入mapper接口注解扫描

  1. @SpringBootApplication
  2. @MapperScan(basePackages = "com.itheima.dao")
  3. //MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spriing容器
  4. public class MybatisApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MybatisApplication.class,args);
  7. }
  8. }

(9)创建service 实现类和接口

  1. package com.itheima.service.impl;
  2. import com.itheima.dao.UserMapper;
  3. import com.itheima.pojo.User;
  4. import com.itheima.service.UserService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. /***
  9. * 描述
  10. * @author ljh
  11. * @packagename com.itheima.service.impl
  12. * @version 1.0
  13. * @date 2020/2/23
  14. */
  15. @Service
  16. public class UserServiceImpl implements UserService {
  17. @Autowired
  18. private UserMapper userMapper;
  19. @Override
  20. public List<User> findAllUser() {
  21. return userMapper.findAllUser();
  22. }
  23. }

接口如下:

  1. package com.itheima.service;
  2. import com.itheima.pojo.User;
  3. import java.util.List;
  4. /***
  5. * 描述
  6. * @author ljh
  7. * @packagename com.itheima.service
  8. * @version 1.0
  9. * @date 2020/2/23
  10. */
  11. public interface UserService {
  12. public List<User> findAllUser();
  13. }

(10)创建controller

  1. package com.itheima.controller;
  2. import com.itheima.pojo.User;
  3. import com.itheima.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.List;
  8. /***
  9. * 描述
  10. * @author ljh
  11. * @packagename com.itheima.controller
  12. * @version 1.0
  13. * @date 2020/2/23
  14. */
  15. @RestController
  16. @RequestMapping("/user")
  17. public class UserController {
  18. @Autowired
  19. private UserService userService;
  20. @RequestMapping("/findAll")
  21. public List<User> findAll(){
  22. return userService.findAllUser();
  23. }
  24. }

(11)测试

浏览器中发送请求:http://localhost:8080/user/findAll

4.3.3 springboot整合redis

为了方便 ,我们就在之前的mybatis中的基础上进行测试。

4.3.3.1 整合redis实现数据添加

(1)springboot整合redis的步骤

  1. 1.添加起步依赖
  2. 2.准备好redis服务器 并启动
  3. 3.SerService中的方法中使用
  4. 3.1 注入redisTemplate
  5. 3.2 在方法中进行调用
  6. 4.配置yml 配置redis的服务器的地址

(2)添加起步依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

(3)配置redis链接信息

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  5. username: root
  6. password: 123456
  7. redis:
  8. host: localhost
  9. port: 6379
  10. #配置mapper的映射文件的位置
  11. mybatis:
  12. mapper-locations: classpath:mappers/*Mapper.xml

(4)service中进行调用

  1. @Service
  2. public class UserServiceImpl implements UserService {
  3. @Autowired
  4. private UserMapper userMapper;
  5. @Autowired
  6. private RedisTemplate redisTemplate;
  7. @Override
  8. public List<User> findAllUser() {
  9. //1.获取redis中的数据
  10. List<User> list = (List<User>) redisTemplate.boundValueOps("key_all").get();
  11. //2.判断 是否有,如果有则返回,如果没有则从mysql中获取设置到redis中再返回
  12. if (list != null && list.size() > 0) {
  13. return list;
  14. }
  15. List<User> allUser = userMapper.findAllUser();
  16. //3 从mysql中获取设置到redis中再返回
  17. redisTemplate.boundValueOps("key_all").set(allUser);
  18. return allUser;
  19. }
  20. }

(5)启动redis

如图双击exe启动

springboot-day01 - 图14

(6)测试,如下图所示

springboot-day01 - 图15

再次访问,则从redis中获取数据了。

4.3.3.2 redis的序列化机制

如上图所示,出现了乱码,这个是由于redis的默认的序列化机制导致的。这里需要注意下:并不是错误,由于序列化机制,导致我们数据无法正常显示。如果有代码的方式获取则是可以获取到数据的。

  1. 1,默认的情况下redisTemplate操作key vlaue的时候 必须要求 key一定实现序列化 value 也需要实现序列化
  2. 2,默认的情况下redisTemplate使用JDK自带的序列化机制:JdkSerializationRedisSerializer
  3. 3JDK自带的序列化机制中要求需要key value 都需要实现Serializable接口
  4. 4. RedisTemplate支持默认以下几种序列化机制:机制都实现了RedisSerializer接口
  5. + OxmSerializer
  6. + GenericJackson2JsonRedisSerializer
  7. + GenericToStringSerializer
  8. + StringRedisSerializer
  9. + JdkSerializationRedisSerializer
  10. + Jackson2JsonRedisSerializer

springboot-day01 - 图16

我们可以进行自定义序列化机制:例如:我们定义key 为字符串序列化机制,value:为JDK自带的方式则,应当处理如下:

  1. @Bean
  2. public RedisTemplate<Object, Object> redisTemplate(
  3. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  4. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  5. template.setConnectionFactory(redisConnectionFactory);
  6. //设置key的值为字符串序列化方式 那么在使用过程中key 一定只能是字符串
  7. template.setKeySerializer(new StringRedisSerializer());
  8. //设置value的序列化机制为JDK自带的方式
  9. template.setValueSerializer(new JdkSerializationRedisSerializer());
  10. return template;
  11. }

整体配置如下:

  1. @SpringBootApplication
  2. @MapperScan(basePackages = "com.itheima.dao")
  3. //MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spriing容器
  4. public class MybatisApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(MybatisApplication.class,args);
  7. }
  8. @Bean
  9. public RedisTemplate<Object, Object> redisTemplate(
  10. RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
  11. RedisTemplate<Object, Object> template = new RedisTemplate<>();
  12. template.setConnectionFactory(redisConnectionFactory);
  13. //设置key的值为字符串序列化方式 那么在使用过程中key 一定只能是字符串
  14. template.setKeySerializer(new StringRedisSerializer());
  15. //设置value的序列化机制为JDK自带的方式
  16. template.setValueSerializer(new JdkSerializationRedisSerializer());
  17. return template;
  18. }
  19. }

重启并重新测试如下效果:

springboot-day01 - 图17

总结:

  1. 在工作中,根据我们业务的需要进行设置和选择,如果没有合适的还可以自己定义。只要实现RedisSerializer接口即可。

5. Springboot版本控制的原理分析

我们知道springboot不需要添加版本。版本统一进行管理。那么原理是什么呢?看下边图:

springboot-day01 - 图18

如图所示:我们自己的springboot项目继承于spring-boot-starter-parent,而他又继承与spring-boot-dependencies ,dependencies中定义了各个版本。通过maven的依赖传递特性从而实现了版本的统一管理。