Springboot
springboot 就是一个基于spring的一个框架。提供了一些自动配置的依赖包,自动嵌入servlet的容器,简化了我们开发的配置,提升开发人员的开发效率,并解决了包依赖的问题。
springboot解决的问题
使用springboot:可以不需要配置,可以不需要自己单独去获取tomcat,基本解决了包依赖冲突的问题,一键发布等等特性。
springboot的介绍
官方的介绍
特性:
- 创建独立的Spring应用程序
- 直接嵌入Tomcat,Jetty或Undertow(无需部署WAR文件)
- 提供“入门”依赖项(起步依赖),以简化构建配置
- 尽可能自动配置Spring和第三方库
- 提供可用于生产的功能,例如指标,运行状况检查和外部化配置
- 完全没有代码生成,也不需要XML配置
Springboot使用入门
1.创建一个maven工程
2.引入依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.创建启动类
package com.cf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QuickStartApplication {
public static void main(String[] args) {
SpringApplication.run(QuickStartApplication.class,args);
}
}
4.创建controller 实现展示hello world
package com.cf.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String sayHello(){
return "hello world";
}
}
5.测试
启动启动类的main方法,在浏览器中输入localhost:8080/hello,则在页面中显示hello world
Springboot的配置文件
SpringBoot是约定大于配置的,所以很多配置都有默认值。如果想修改默认配置,可以用application.properties或application.yml(application.yaml)自定义配置。SpringBoot默认从Resource目录加载自定义配置文件
properties文件
properties文件的配置多以key.key.key=value
的形式组成,那么springboot本身有默认的一些配置,如果要修改这些默认的配置,可以在application.properties中进行配置修改。
yaml或者yml文件
yaml文件等价于properties文件,在使用过程中都是一样的效果。但是yml文件书写的方式和properties文件不一样。更加简洁,那么我们可以根据需要选择性的使用properties和yml文件。如果同时存在两个文件,那么优先级properties要高于yml。
语法特点如下:
- 大小写敏感
- 数据值前必须有空格 ,作为分隔符
- 缩进的空格数目不重要,只需要对齐即可
#
表示注释
书写格式如下要求如下:key和key之间需要换行以及空格两次。 简单key value之间需要冒号加空格。
key1:
key2:
key3: value
key4: value4
注意:yml语法中,相同缩进代表同一个级别
获取配置文件中值
获取配置文件中的值我们一般有几种方式:
- @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代码:
```java
@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:
```java
```java
@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;
}
}
自定义配置提示:
```java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
profile切换环境
在开发的过程中,需要配置不同的环境,所以即使我们在application.yml中配置了相关的配置项,当时在测试是,需要修改数据源等端口路径的配置,测试完成之后,又上生产环境,这时配置又需要修改,修改起来很麻烦。
properties配置文件方式
properties配置方式
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
yml配置文件方式
application.yml:
#通过active指定选用配置环境
spring:
profiles:
active: pro
application-dev.yml:
#开发环境
server:
port: 8081
application-test.yml:
#测试环境
server:
port: 8082
application-por.yml:
#生产环境
server:
port: 8083
分隔符的方式
spring:
profiles:
active: dev
---
#开发环境
server:
port: 8081
spring:
profiles: dev
---
#测试环境
server:
port: 8082
spring:
profiles: test
---
#生产环境
server:
port: 8083
spring:
profiles: pro
启动时指定参数
.加入依赖:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
工程执行:clean package
.jar包所有在目录执行 java -jar xxx.jar --spring.profiles.active=test
jvm参数配置
编辑运行的选项
jvm虚拟机参数配置 -Dspring.profiles.active=dev
Springboot集成第三方框架
springboot整合junit1)
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
创建在/main/java/下一个类UserService用于测试:
package com.cf.service;
@Service
public class UserService {
public String getUser() {
System.out.println("获取用户的信息");
return "zhangsan";
}
}
在test/java/下创建测试类,类的包名和启动类的包名一致即可(不一致时,添加classes属性(启动类的字节码))
@RunWith(SpringRunner.class) //使用springrunner运行器
@SpringBootTest //启用springboot测试
// 启动类QuickStartApplication的包com.itheima
// SpringBootJunitTest的包com.itheima.test ,在启动类的包下,则可以省略(classes = {启动类.class})
public class SpringBootApplicationTests {
@Autowired
private UserService userService;
@Test
public void getUser() {
String userinfo = userService.getUser();
System.out.println(userinfo);
}
}
测试类与启动类不在同一包下
package com.cf;
import com.itheima.QuickStartApplication;
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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {QuickStartApplication.class})
// 启动类QuickStartApplication的包com.itheima
// SpringBootJunitTest的包com ,不在启动类的包下,必须加上(classes = {启动类.class})
public class SpringBootJunitTest2 {
@Autowired
private UserService userService;
@Test
public void tt(){
String user = userService.getUser();
System.out.println(user);
}
}
springboot整合mybatis
添加依赖
<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>
<!-- 最终打的文件名 demo03.jar
启动:java -jar xxx.jar 2横杆spring.profiles.active=test
-->
<finalName>demo03</finalName>
<!-- spring boot项目打包用的 -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
创建pojo (要有与之对应的数据库表)
public class User implements Serializable{
private Integer id;
private String username;//用户名
private String password;//密码
private String name;//姓名
//getter setter...
//toString
}
创建mapper接口(dao接口)
package com.cf.dao;
import com.cf.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> findAllUser();
}
创建UserMapper(mapper接口的映射,注意 包路径要与mapper接口相同)
<?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>
创建application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/{你的数据库名}?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
username: {你的数据库用户名}
password: {你的数据库密码}
#配置mapper的映射文件的位置
mybatis:
mapper-locations: classpath*:mappers/*Mapper.xml
创建启动类,加入mapper接口注解扫描
@SpringBootApplication
@MapperScan(basePackages = "com.cf.dao")
//MapperScan 用于扫描指定包下的所有的接口,将接口产生代理对象交给spriing容器
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class,args);
}
}
创建service 实现类和接口
service 实现类
package com.cf.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;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findAllUser() {
return userMapper.findAllUser();
}
}
service 接口
package com.cf.service;
import com.cf.pojo.User;
import java.util.List;
public interface UserService {
public List<User> findAllUser();
}
创建controller
package com.cf.controller;
import com.cf.pojo.User;
import com.cf.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;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAllUser();
}
}
测试
浏览器中发送请求:http://localhost:8080/user/findAll
注意Mysql8问题
my.ini 修改 default-time-zone = '+8:00'
mybatis配置连接驱动
com.mysql.jdbc.Driver(低版本)
com.mysql.cj.jdbc.Driver(高版本)
jdbc:mysql://localhost:3306/{你的数据库名}?useUnicode=true&characterEncoding=UTF8&serverTimezone=Asia/Shanghai
springboot整合redis
整合redis实现数据添加
1.添加起步依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.准备好redis服务器 并启动
3.在SerService中的方法中使用
- 3.1 注入redisTemplate
@Autowired
private RedisTemplate redisTemplate;
- 3.2 在方法中进行调用
//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);
4.配置yml 配置redis的服务器的地址
spring:
redis:
host: localhost
port: 6379
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
我们可以进行自定义序列化机制:例如:我们定义key 为字符串序列化机制,value:为JDK自带的方式则,应当处理如下:
@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.cf.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;
}
}