# 学习目标

今日内容介绍

63425891183

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

1 Springboot的介绍

【目标】

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

【路径】

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

【讲解】

1.1. 项目中开发的一些问题

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

1.2. springboot解决的问题

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

1.3. springboot的介绍

官方的介绍

60965914818

特性:

创建独立的Spring应用程序

直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)

提供“入门”依赖项(起步依赖),以简化构建配置

尽可能自动配置Spring和第三方库

提供可用于生产的功能,例如指标,运行状况检查和外部化配置

完全没有代码生成,也不需要XML配置

【小结】

​ 1.ssm存在问题:配置文件繁琐、依赖非常容易冲突、需要手动部署tomcat

​ 2.为什么?解决spring整合其它框架存在的问题,简化开发。

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

2 Springboot使用入门

【目标】

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

【路径】

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

【讲解】

2.1. 入门需求

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

2.2. 环境准备

推荐:springboot 2.1.0.RELEASE

推荐:jdk1.8

2.3. 开始开发和配置

(1)创建工程

60966124600

60966129531

(2)创建maven工程

1580453861218

(3)配置pom.xml

  • pom.xml的整体代码如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>itheima-springboot-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--添加parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <dependencies>
        <!--添加起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

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

@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class,args);
    }
}

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

@RestController
public class TestController {
    @RequestMapping("/hello")
    public String showHello(){
        return "hello world";
    }
}

(6)测试

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

1580454590539

1580454613474

2.4. Spring Initializr的方式

使用idea提供的方式来快速的创建springboot的项目,

但是要求联网而且网络需要比较稳定才行。如下步骤:

(1)选中spring initializr

1580455080392

如果以上步骤失败,可以尝试Custom中输入:http://start.aliyun.com

(2)设置包名和坐标名

1580455224207

(3)设置添加依赖

1580457236960

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

1580457538227

解释:

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

【小结】

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

1580454878440

3 Springboot的配置文件

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

【目标】

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

【路径】

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

【讲解】

3.1. properties文件

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

比如:修改端口配置

server.port=8081

3.2. yaml或者yml文件

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

properties > yml > yaml

语法特点如下:

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

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

key1:
  key2:
    key3: value
key4: value4    

比如:

server:
  port: 8081

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

# 基本格式 key: value
name: zhangsan
# 数组   - 用于区分
city:
  - beijing
  - tianjin
  - shanghai
  - chongqing
#集合中的元素是对象形式
students:
  - name: zhangsan
    age: 18
    score: 100
  - name: lisi
    age: 28
    score: 88
  - name: wangwu
    age: 38
    score: 90
#map集合形式
maps: {"name":"zhangsan", "age": "15"}
#参数引用
person:
  name: ${name} # 该值可以获取到上边的name定义的值	

3.3. 获取配置文件中值

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

  • @value注解的方式 只能获取简单值--重点掌握
  • Environment的方式
  • @ConfigurationProperties--重点掌握

演示如下:

yml中配置:

# 基本格式 key: value
name: zhangsan
# 数组   - 用于区分
city:
  - beijing
  - tianjin
  - shanghai
  - chongqing
#集合中的元素是对象形式
students:
  - name: zhangsan
    age: 18
    score: 100
  - name: lisi
    age: 28
    score: 88
  - name: wangwu
    age: 38
    score: 90
#map集合形式
maps: {"name":"zhangsan", "age": "15"}
#参数引用
person:
  name: ${name} # 该值可以获取到上边的name定义的值
  age: 12

java代码:

controller

@RestController
public class Test2Controller {
    @Value("${name}")
    private String name;

    @Value("${city[0]}")
    private String city0;

    @Value("${students[0].name}")
    private String studentname;

    @Value("${person.name}")
    private String personName;


    @Value("${maps.name}")//value注解只能获简单的值对象
    private String name1;

    @Autowired
    private Environment environment;
    
    @Autowired
    private Student student;

    @RequestMapping("/show")
    public String showHello() {
        System.out.println(name);
        System.out.println(city0);
        System.out.println(studentname);
        System.out.println(personName);
        
        System.out.println("environment name>>>>"+environment.getProperty("name"));
        System.out.println(">>>>"+student.getAge());

        return "hello world";
    }
}

pojo:

@Component
@ConfigurationProperties(prefix = "person")
public class Student {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

自定义配置提示:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>

如果没有提示,需要点击构建

61063075718

3.4. profile切换环境

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

3.4.1. properties配置文件方式

  • properties配置方式

    1582711701050

application.properties:

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

application-dev.properties:

#开发环境
server.port=8081

application-test.properties

server.port=8082

application-pro.properties

server.port=8083

3.4.2. yml配置文件方式

1582718508134

application.yml:

#通过active指定选用配置环境
spring:
  profiles:
    active: pro

application-dev.yml:

#开发环境
server:
  port: 8081

application-test.yml:

#测试环境
server:
  port: 8082

application-pro.yml

#生产环境
server:
  port: 8083

3.4.3. 启动时指定参数(了解)

a.加入依赖:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

b.工程执行:clean package

c.jar包所有在目录执行 java -jar xxx.jar --spring.profiles.active=test

3.4.4. jvm参数配置(了解)

jvm虚拟机参数配置 -Dspring.profiles.active=dev

61063541416

3.4.5. 分隔符的方式(了解)

spring:
  profiles:
    active: dev

---
#开发环境
server:
  port: 8081
spring:
  profiles: dev

---
#测试环境
server:
  port: 8082
spring:
  profiles: test

---
#生产环境
server:
  port: 8083
spring:
  profiles: pro

【小结】

1:掌握properties yaml yml常见配置

2:掌握获取配置文件中的值

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

3:profile切换环境

  • 掌握properties yml配置文件方式
  • 了解启动时指定参数
  • 了解jvm参数配置
  • 了解分隔符的方式

4 Springboot集成第三方框架

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

【目标】

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

【路径】

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

【讲解】

4.1. springboot整合junit

(1)创建工程,添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>springboot-junit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--添加parent-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--junit依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

(2)创建启动类

package com.itheima;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 启动类 junit测试
 */
@SpringBootApplication
public class MySpringBootJunit {

    public static void main(String[] args) {
        SpringApplication.run(MySpringBootJunit.class,args);
    }
}

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

package com.itheima.service;

import org.springframework.stereotype.Service;

/**
 * 业务类
 */
@Service
public class UserService {

    public String sayHello(){
        System.out.println("****************测试成功了****************");
        return "junit test success";
    }
}

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

package com.itheima.test;

import com.itheima.MySpringBootJunit;
import com.itheima.service.UserService;
import org.junit.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;

/**
 * junit测试
 * 之前:
 * //@RunWith(SpringJUnit4ClassRunner.class)
 * //@ContextConfiguration(locations = "application-xx.xml")
 * 现在:
 *  //@RunWith(SpringRunner.class)
 *  //@SpringBootTest
 *  //如果启动类跟测试类所在包不一样:@SpringBootTest(classes = MySpringBootJunit.class)
 */
@RunWith(SpringRunner.class)
@SpringBootTest//(classes = MySpringBootJunit.class)
public class JunitTest {
    @Autowired
    private UserService userService;
    /**
     * 测试service
     */
    @Test
    public void testService(){
        System.out.println(userService.sayHello());
    }

}

解释:

@RunWith(SpringRunner.class) 使用springrunner运行器
@SpringBootTest 启用springboot测试

使用的方式和之前的spring的使用方式差不多。

错误:

63427028433

4.2. springboot整合mybatis

(1)实现步骤说明

需求:http://localhost:8080/user/findAll

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

4.2.1. 创建数据库表

-- ----------------------------
-- 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=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

4.2.2. 添加依赖

创建工程,引入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>itheima-springboot-mybatis-demo04</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>
        <!--驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--mybatis的 起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>
        <!--spring web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

4.2.3. 创建pojo

public class User implements Serializable{
    private Integer id;
    private String username;//用户名
    private String password;//密码
    private String name;//姓名
    //getter setter...
    //toString
}

4.2.4. 创建mapper接口

package com.itheima.dao;

import com.itheima.pojo.User;

import java.util.List;

/***
 * mapper接口
 * @author wx
 * @packagename com.itheima.dao
 * @version 1.0
 * @date 2020/2/23
 */
public interface UserMapper {
    public List<User> findAllUser();
}

4.2.5. 创建UserMapper

<?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.dao.UserMapper">
    <select id="findAllUser" resultType="com.itheima.pojo.User">
        SELECT * from user
    </select>
</mapper>

1582791131694

4.2.6. 创建application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/itcast_springboot?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
#配置mapper的映射文件的位置
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml

4.2.7. 创建启动类,加入mapper接口注解扫描

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

4.2.8. 创建service 实现类和接口

package com.itheima.service.impl;

import com.itheima.dao.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * 描述
 * @author wx
 * @packagename com.itheima.service.impl
 * @version 1.0
 * @date 2020/2/23
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }
}

接口如下:

package com.itheima.service;

import com.itheima.pojo.User;

import java.util.List;

/***
 * 描述
 * @author wx
 * @packagename com.itheima.service
 * @version 1.0
 * @date 2020/2/23
 */
public interface UserService {
    public List<User> findAllUser();
}

4.2.9. 创建controller

package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 * 描述
 * @author wx
 * @packagename com.itheima.controller
 * @version 1.0
 * @date 2020/2/23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAllUser();
    }
}

4.2.10. 测试

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

4.2.11. 注意

报异常:

63428069610

解决:

1.执行命令

63428052742

2.修改my.ini

my.ini 修改 default-time-zone = '+8:00'

mybatis配置com.mysql.jdbc.Driver(低版本)   com.mysql.cj.jdbc.Driver(高版本)

3.重启mysql服务

63428055153

4.3. springboot整合redis

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

4.3.1. 整合redis实现数据添加

(1)springboot整合redis的步骤

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

(2)添加起步依赖

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

(3)配置redis链接信息

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/springboot_user?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 123456
  redis:
    host: localhost
    port: 6379
#配置mapper的映射文件的位置
mybatis:
  mapper-locations: classpath:mappers/*Mapper.xml

(4)service中进行调用

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> findAllUser() {

        //1.获取redis中的数据
        List<User> list = (List<User>) redisTemplate.boundValueOps("key_all").get();
        //2.判断 是否有,如果有则返回,如果没有则从mysql中获取设置到redis中再返回
        if (list != null && list.size() > 0) {
            return list;
        }
        List<User> allUser = userMapper.findAllUser();

        //3 从mysql中获取设置到redis中再返回
        redisTemplate.boundValueOps("key_all").set(allUser);

        return allUser;
    }
}

(5)启动redis

如图双击exe启动

1582792252446

(6)测试,如下图所示

1582792362271

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

4.3.2. redis的序列化机制

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

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

1582792722562

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

Control+N搜索RedisAutoConfiguration参考源码加载自定义序列化机制

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

整体配置如下:

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

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

}

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

清空数据:flushall

1582793228764

总结:

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

【小结】

1:掌握springboot整合junit

2:掌握springboot整合mybatis

3:掌握springboot整合redis

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

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

1582794808473

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

总结

1.重点理解 ssm整合存在问题 为什么使用springboot(自动配置、起步依赖) 什么是springboot?

2.Springboot入门案例 -重点掌握

3.Springboot配置文件使用- yml 配置(基础格式) 以及 读取(@value @ConfigurationProperties )

4.Springboot切换环境(yml配置文件方式)

5.Springboot集成第三方框架(junit mybatis redis)

posted on 2022-04-23 14:36  ofanimon  阅读(37)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css