SpringBoot初级练习

SpringBoot

一、创建项目出现的问题

问题1、application.yml文件无法识别
*解决方法:File->Settions->Plugins目录下
选中YAML,重启IDEA
问题2、application.yml文件图标错误
解决方法:File->Settings->File Types下
删除
.yml

二、创建一个SpringBoot的项目

  1. New Project

  2. 选择Spring Initializr

  3. 选择Web下的Web,然后Flish

  4. 导入依赖



    org.springframework.boot
    spring-boot-starter-parent
    2.1.5.RELEASE




    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-devtools

  5. IDEA设置,完成热部署

    1. 在Settings中找到Compiler,选中Build Project automatically
    2. 按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.1



mysql
mysql-connector-java
5.1.6

第2步:yml配置文件
#数据库配置spring:
#配置Mybatis配置信息
#spring集成Mybatis环境
#pojo别名扫描包
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

mybatis:
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;

@Mapper
public interface UserMapper {

public List queryUserList();

}

第5步:创建映射文件




第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格式或字符串格式回写
@RestController
public 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-jpa



mysql
mysql-connector-java
5.1.6



javax.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;

@Entity
public 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;
}

@Override
public 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<User,Integer> {

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文件信息

redis

redis:
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<String,String> 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);
}
}

posted @ 2019-06-15 09:09  肥宅君  阅读(1036)  评论(0)    收藏  举报