一、创建项目出现的问题
问题1、application.yml文件无法识别 *解决方法:File->Settions->Plugins目录下 选中YAML,重启IDEA问题2、application.yml文件图标错误 *解决方法:File->Settings->File Types下 删除*.yml
二、创建一个SpringBoot的项目
- New Project
- 选择Spring Initializr
- 选择Web下的Web,然后Flish
- 导入依赖
org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASEorg.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools
IDEA设置,完成热部署
在Settings中找到Compiler,选中Build Project automatically
按Ctrl+Shift+Alt+/:选中compiler.automake.allow.when.app.running
底层分析:
spring-boot-starter-parent:springboot起步依赖 在spring-boot-starter-parent中 resources资源引入: ${basedir}/src/main/resources下的 application*.yml application*.yaml application*.properties文件 在spring-boot-dependencies中 自动根据spring-boot-starter-parent的版本匹配相应的版本,进行了版本控制的作用 自动配置分析: @SpringBootApplication 标志该类是一个配置类:@Configration
三、SpringBoot整合Mybatis
第1步:导依赖:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1mysql mysql-connector-java 5.1.6
第2步:yml配置文件
数据库配置spring:
配置Mybatis配置信息
spring集成Mybatis环境
pojo别名扫描包
spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=falseusername: rootpassword: 123456mybatis: type-aliases-package: com.zero.domain mapper-locations: classpath:mapper/*Mapper.xml
第3步:创建实体
package com.zero.domain;public class User { private Integer id; private String name; private String pass; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + ''' + ", pass='" + pass + ''' + '}'; } }
第4步:创建接口
package com.zero.mapper;import com.zero.domain.User;import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapperpublic interface UserMapper { public List queryUserList();}
第5步:创建映射文件
<?xml version="1.0" encoding="utf-8" ?> SELECT * FROM demo
第6步:测试
package com.zero.controller;import com.zero.domain.User;import com.zero.mapper.UserMapper;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;//以json格式或字符串格式回写@RestControllerpublic class democontroller { @Autowired private UserMapper userMapper; @RequestMapping("/quick") public List queryUserList(){ List users = userMapper.queryUserList(); return users; }}
四、SpringBoot整合Spring Data JPA
第1步:导入依赖
org.springframework.boot spring-boot-starter-data-jpamysql mysql-connector-java 5.1.6javax.xml.bind jaxb-api 2.3.0
第2步:创建实体,使用注解进行配置
package com.zero.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;@Entitypublic class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Integer id;private String name;private String pass;public Integer getId() { return id;}public void setId(Integer id) { this.id = id;}public String getName() { return name;}public void setName(String name) { this.name = name;}public String getPass() { return pass;}public void setPass(String pass) { this.pass = pass;}@Overridepublic String toString() { return "User{" + "id=" + id + ", name='" + name + ''' + ", pass='" + pass + ''' + '}';}}
第3步:创建接口
package com.zero.reposytory;import com.zero.domain.User;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;public interface UserReposytory extends JpaRepository { public List findAll();}
第4步:创建yml配置
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456#JPA Configuration jpa: database: MySQL show-sql: true generate-ddl: true hibernate: ddl-auto: update
第5步:测试
package com.zero;import com.zero.domain.User;import com.zero.reposytory.UserReposytory;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;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes = Demo614Application.class)public class JpaTest { @Autowired private UserReposytory userReposytory; @Test public void test(){ List all = userReposytory.findAll(); System.out.println(all); }}
五、Redis缓存
第1步:配置yml文件信息
#redisredis: host: 127.0.0.1 port: 6379
第2步:测试
package com.zero;
import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import com.zero.domain.User;import com.zero.reposytory.UserReposytory;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.data.redis.core.RedisTemplate;import org.springframework.test.context.junit4.SpringRunner;import java.util.List;@RunWith(SpringRunner.class)@SpringBootTest(classes = Demo614Application.class)public class RedisTest { @Autowired private RedisTemplate redisTemplate; @Autowired private UserReposytory userReposytory; @Test public void test() throws JsonProcessingException { //1.从Redis中获取数据,json字符串 String s = redisTemplate.boundValueOps("user.findall").get(); //2.判断Redis中是否存在想要的数据 if(null==s){ //3.1:不存在,从数据库中查询 List all = userReposytory.findAll(); //3.2:将查询出的数据存储到Redis中 //通过web先将集合换换成json的字符串,使用Jackson进行转换 ObjectMapper ob = new ObjectMapper(); s = ob.writeValueAsString(all); redisTemplate.boundValueOps("user.findall").set(s); System.out.println("从数据库中获取数据"); }else { System.out.println("从Redis缓存中获取数据"); } //4.将数据打印在控制台 System.out.println(s); }