SpringBoot

01、SpringBoot的概述

一、SpringBoot就是为了解决Spring开发的问题(缺点)而生的!!!

​ 1)解决重配置的问题

​ 基于约定优于配置的思想

​ 2)jar包兼容性问题

​ 使用起步依赖解决jar包兼容性问题

二、SpringBoot不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式

02、SpringBoot的核心功能

目标
记住SpringBoot的两个核心功能

核心功能一:

​ — 起步依赖

简单的说,起步依赖就是将具备某种功能的坐标打包到一起,并提供一些默认的功能

​ 解决jar包冲突的问题

核心功能二:
– 自动配置

通俗点说,SpringBoot提供了对Spring框架的默认配置,不需要修改默认配置就可以直接使用,当然也可以去修改默认配置。(约定大于配置思想)

​ 解决配置繁琐的问题

03、入门:创建工程和导入起步依赖【掌握】

目标
掌握SpringBoot中导入的起步依赖
<?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.springboot</groupId>
    <artifactId>springboot_01_quick</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- 公共父工程 (起步父工程)  SpringBoot项目必须到的依赖 -->
    <!-- 这里并没有真正的导入任何包,只是版本锁定 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- 真正导入依赖 -->
    <dependencies>
        <!-- SpringMVC起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


</project>

04、入门:编写引导类

目标
掌握SpringBoot中引导(启动)类的编写
package com.springboot;

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

/**
 *  引导类
 *    两个要求:
 *     1)在类上面必须有一个@SpringBootApplication注解
 *     2)有一个main方法,使用SpringApplication的run方法运行引导类
 */
@SpringBootApplication
public class MySpringBootApplication {

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

}

05、入门:编写一个Controller

目标
掌握SpringBoot中编写一个Controller的原则
package com.springboot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *  入门Controller
 */
@RestController    //@RestController = @Controller+@ResponseBody
public class QuickController {

    /**
     * 入门方法
     */
    @RequestMapping("/quick")
    public String quick(){
        return "SpringBoot入门";
    }

}

注意:Controller必须放在引导类的同级或子目录下!!

06、原理分析:起步依赖原理

看图知理
在这里插入图片描述

spring-boot-starter-parent:

​ 1)加载spirngboot核心配置

​ 2)锁定依赖版本

起步依赖:spring-boot-starter-web

​ 利用maven依赖传递,把其他依赖导入进来

07、原理分析:自动配置原理

小结
在SpringBoot的自动配置中,自动配置tomcat的端口号 port 的前缀是什么?

08、配置文件:properties配置

1557305393817

SpringBoot项目修改自动配置,可以定义两种格式

​ application.yml 或 application.yaml

​ application.properties

两种格式同时存在,优先以application.properties生效!!!!

在resources根目录下建立application.properties文件

可以修改的属性名称

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-application-properties

# 修改tomcat启动端口
server.port=9090
# 修改项目的访问路径
server.servlet.context-path=/crm

09、配置文件:yml配置【掌握】

什么是yml格式?

YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。

yml格式比properties更容易被人阅读,比xml更加简洁!!!

YML文件的扩展名可以使用.yml或者.yaml

在这里插入图片描述

# 定义普通数据(字符串,整数等)
username: 小苍

# 定义对象
user:
  username: 小玛
  age: 18
  addr: 广州

# 数组
citys:
  city:
    - 北京
    - 广州
    - 深圳

# 对象数组
userList:
  users:
    - username: 张三
      age: 18
      addr: 广州
    - username: 李四
      age: 20
      addr: 北京
    - username: 王五
      age: 22
      addr: 东莞

10、配置文件:读取配置文件内容【掌握】

目标
掌握读取配置文件的两种方法

SpringBoot程序读取配置文件两种方式:

​ 1)使用@Value注解

​ 2)使用@ConfigurationProperties注解

第一种:@Value注解

​ 适合读取什么类型: 普通数据,对象

/**
 * 读取配置文件
 *    @Value注解读取
 */
@RestController
@RequestMapping("/config1")
public class ConfigController1 {

    //普通数据
    @Value("${nickname}")
    private String name;

    //对象
    @Value("${user.username}")
    private String name2;

    @RequestMapping("/test1")
    public String test1(){
        return name+"==="+name2;
    }
}

第二种: @ConfigurationProperties注解

​ 适合读取什么类型:对象,数组,对象数组

对象:

/**
 * 读取配置文件
 *    @ConfigurationProperties注解读取
 */
@RestController
@RequestMapping("/config2")
@ConfigurationProperties(prefix = "user") // prefix: 前缀
public class ConfigController2 {

    //对象 (注意:1) 类的属性名称和yml的属性名称保持一致的  2)提供setter方法)
    private String username;
    private Integer age;
    private String addr;

    public void setUsername(String username) {
        this.username = username;
    }

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

    public void setAddr(String addr) {
        this.addr = addr;
    }



    @RequestMapping("/test1")
    public String test1(){
        return username+"=="+age+"==="+addr;
    }
}

读取有比较多的属性对象

数组:

@RestController
@RequestMapping("/config3")
@ConfigurationProperties(prefix = "citys") // prefix: 前缀
public class ConfigController3 {

    //数组
    private String[] city;
    public void setCity(String[] city) {
        this.city = city;
    }

    @RequestMapping("/test1")
    public String test1(){
        return "===="+Arrays.asList(city);

    }
}

对象数组:

@RestController
@RequestMapping("/config4")
@ConfigurationProperties(prefix = "userlist") // prefix: 前缀
public class ConfigController4 {

    //对象数组
    private List<User> users;

    public void setUsers(List<User> users) {
        this.users = users;
    }

    @RequestMapping("/test1")
    public String test1(){
        return "===="+Arrays.asList(users);

    }
}

11、SpringBoot整合Mybatis

目标
重点掌握SpringBoot整合MyBatis

在这里插入图片描述

1)导入mybatis起步依赖

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

    <dependencies>
        <!--mybatis起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
         <!-- SpringMVC起步依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- MySQL连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>

2)添加数据库驱动

<!-- MySQL连接驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3)创建application.yml添加数据库连接配置

spring:
  datasource: # 数据源
    url: jdbc:mysql://localhost:3306/springboot?characterEncoding=UTF8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root

4)创建用户表

/*
SQLyog Ultimate v11.33 (64 bit)
MySQL - 5.6.22 : Database - test
*********************************************************************
*/


/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

/*Table structure for table `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=4 DEFAULT CHARSET=utf8;

/*Data for the table `user` */

insert  into `user`(`id`,`username`,`password`,`name`) values (1,'zhangsan','123','张三');
insert  into `user`(`id`,`username`,`password`,`name`) values (2,'lisi','123','李四');
insert  into `user`(`id`,`username`,`password`,`name`) values (3,'feifei','456','飞飞');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

  1. 创建用户实体类
/**
 * 用户实体
 */

public class User implements Serializable{

    private Integer id;
    private String username;
    private String password;
    private String name;

6)创建Mapper接口

package com.springboot.dao;

import com.springboot.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 用户Mapper接口
 */
@Mapper  // Mapper接口扫描
public interface UserMapper {

    public List<User> queryAll();

}

7)定义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.springboot.dao.UserMapper">

    <select id="queryAll" resultType="user">
        select * from user
    </select>
</mapper>

8)application.yml添加mybatis配置

# 修改mybatis配置
mybatis:
 # 扫描Mapper映射配置路径
  mapper-locations: classpath:mapper/*Mapper.xml
  # 扫描pojo目录,定义实体别名
  type-aliases-package: com.springboot.pojo

9)编写Controller

package com.springboot.controller;

import com.springboot.dao.UserMapper;
import com.springboot.pojo.User;
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;

/**
 * 用户Controller
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询所有用户
     */
    @RequestMapping("/queryAllUser")
    public List<User> queryAllUser(){
        return userMapper.queryAll();
    }

}

10)编写引导类,启动测试

package com.springboot;

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

/**
 * 引导类
 */
@SpringBootApplication
public class MySpringBootApplication {

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

}

在这里插入图片描述

11、SpringBoot整合Junit

目标
掌握SpringBoot整合Junit

在这里插入图片描述

1)导入junit起步依赖

<!--springboot集成junit起步依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

2)SpringBoot测试类

/**
 * SpringBoot整合Junit
 */
@RunWith(SpringRunner.class)  // SpringRunner是SpringJUnit4ClassRunner子接口
@SpringBootTest(classes = MySpringBootApplication.class) // 加载引导类(配置类)
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void test(){
        List<User> list = userMapper.queryAll();
        for(User u:list){
            System.out.println(u);
        }
    }



}

12、SpringBoot整合SpringDataJpa

目标
重点掌握SpringBoot整合SpringDataJpa

在这里插入图片描述

  1. 导入spring data jpa起步依赖
<!-- springBoot JPA的起步依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2)添加数据库驱动

<!-- MySQL连接驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

3)编写application.yml添加数据源和jpa配置

spring:
  datasource: # 数据源
    url: jdbc:mysql://localhost:3306/springboot?characterEncoding=UTF8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
# 配置jpa
  jpa:
    show-sql: true # 打印sql
    generate-ddl: true # 自动维护表

4)编写实体及JPA映射

/**
 * 用户实体
 */
@Entity
@Table(name = "user")
public class User implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String username;
    private String password;
    private String name;

5)编写dao接口

package com.springboot.dao;

import com.springboot.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * 用户Dao
 */
public interface UserDao extends JpaRepository<User,Integer>,JpaSpecificationExecutor<User>{
}

6)编写引导类,编写测试类

package com.springboot;

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

/**
 * 引导类
 */
@SpringBootApplication
public class MySpringBootApplication {

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

}
/**
 * 测试SpringDataJPa
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class UserDaoTest {

    @Autowired
    private UserDao userDao;

    @Test
    public void test(){
        List<User> list = userDao.findAll();
        for(User u:list){
            System.out.println(u);
        }
    }

}

13、SpringBoot整合Redis

目标
重点掌握SpringBoot整合Redis

在这里插入图片描述

1)导入redis起步依赖

<!-- 配置使用redis启动器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2)application.yml配置redis

#redis配置
  redis:
    host: localhost # 可以省略的
    port: 6379  # 可以省略的

3)编写测试类

/**
 * Redis测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    //存入
    @Test
    public void test1(){
        redisTemplate.boundValueOps("name").set("eric");
    }

    //取出
    @Test
    public void test2(){
        String name = (String)redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值