美文网首页
SpringBoot

SpringBoot

作者: 金石_832e | 来源:发表于2019-06-10 17:27 被阅读0次

    typora-root-url: ./

    SpringBoot

    什么是SpringBoot

    快速启动一个生产级的项目,简化开发流程

    回顾SSM

    • 复杂、大量的配置文件
    • 大量的依赖,没有人管理(容易造成依赖冲突,高低版本不兼容)
    SpringBoot能解决上述问题
    • 只需要一个配置文件,配置文件的名称必须是 application-.properties或者是 application-.yml这种形式命名

      application-dev.properties

      application-pro.properties

      jdbc.driverClassName = com.mysql.jdbc.Driver
      jdbc.url = jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
      jdbc.username = root
      jdbc.password = root
      

    但是SpringBoot更推荐使用.yml文件去作为配置文件

    application-dev.yml:开发环境

    application-pro.yml:生产环境

    application-dev.yaml

    application-pro.yaml

    jdbc:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
      username: root
      password: root
    
    • SpringBoot项目有一个“依赖”的父工程,可以引入其他启动器依赖完成依赖管理

    创建SpringBoot项目

    • 1.创建maven项目有
    1560134041172.png 1560134150457.png 1560134186960.png
    • 2.在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.neuedu</groupId>
          <artifactId>springbootdemo1</artifactId>
          <version>1.0-SNAPSHOT</version>
        <!-- 引入父工程,当前最新版本2.1.5 -->
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.1.5.RELEASE</version>
          </parent>
      </project>
      
    • 3.web项目,需要引入web启动器依赖,版本由父工程管理

      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      </dependencies>
      

    如何启动SpringBoot项目

    1560136198438.png
    package com.neuedu;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class BootApplication {
        public static void main(String[] args) {
            SpringApplication.run(BootApplication.class);
        }
    }
    

    在application.yml中指定启动端口(默认8080)

    # 指定启动端口
    server:
      port: 8888
    

    启动启动类

    1560137037487.png

    这个启动类要想生效,它的路径必须在所有子包的父级路径中

    SpringBoot中有自动扫描,是以启动类@SpringBootApplication为起点向下扫描所有子包,所以直接就可以写web层,愉快的开发了

    1560137145423.png
    package com.neuedu.web;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class HelloController {
        @RequestMapping("hello")
        @ResponseBody
        public String hello(){
            return "hello,StringBoot!";
        }
    }
    
    1560137180169.png

    简单测试,效果完美!

    整合数据库连接池,mybatis,和事务

    引入mybatis启动器和mysql依赖

       <dependencies>
            <!--web启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.4</version>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.39</version>
            </dependency>
            <!-- mybatis启动器 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
        </dependencies>
    

    application.yml

    # 数据库连接参数
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/java2demo?useUnicode=true&characterEncoding=utf8
        username: root
        password: root
        # 数据库连接池
        hikari:
          maximum-pool-size: 30     # 最大连接数
          minimum-idle: 10          # 最小连接数
          idle-timeout: 60000       # 超时时间
          
    # 指定mapper文件路径 , 配置包中类别名
    mybatis:
      mapper-locations: classpath:mappers/*.xml
      type-aliases-package: com.neuedu.pojo
     
    # 指定日志打印级别,输出SQL语句
    logging:
      level:
        com.neuedu.mapper: debug
    

    需要注意的是,mapper层要想被正确装配,需要在mapper层接口上添加@Mapper注解或者在启动上添加@MapperScan注解完成扫描

    @SpringBootApplication
    @MapperScan("com.neuedu.mapper") // 指定mapper包
    public class BootApplication {
        public static void main(String[] args) {
            SpringApplication.run(BootApplication.class);
        }
    }
    

    完整展示

    pom文件

    <?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.zpwd</groupId>
        <artifactId>springBootDemo1</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <!-- 引入父工程,当前最新版本2.1.5 -->
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
        </parent>
    
        <dependencies>
            <!--web启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!--lombok-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.4</version>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.39</version>
            </dependency>
            <!-- mybatis启动器 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!--redis-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <!--fastjson-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>
        </dependencies>
    </project>
    

    application.yml

    # 指定启动端口
    server:
      port: 8080
    
    # 数据库连接参数
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/zpwd?useUnicode=true&characterEncoding=utf8
        username : root
        password : root
    # 数据库连接池
        hikari:
          idle-timeout: 6000
          minimum-idle: 1
          maximum-pool-size: 30
    #集成redis
      redis:
        host: 127.0.0.1
        port: 6379
        password: 123456
        jedis:
          pool:
            max-idle: 300
            max-wait: 1000
            max-active: 8
            min-idle: 10
    
    #指定mapper文件路径
    mybatis:
      mapper-locations: classpath:mappers/*.xml
      type-aliases-package: com.zpwd.pojo
    
    #指定日志打印级别
    logging:
      level:
        com.zpwd.mapper: debug
    

    BootApplication

    package com.zpwd;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication // 快速启动
    @MapperScan("com.zpwd.mapper") // 扫描mapper文件
    public class BootApplication {
        public static void main(String[] args) {
            SpringApplication.run( BootApplication.class );
        }
    }
    

    Controller

    @CrossOrigin // 前后端分离
    @Controller // 托管
    @RequestMapping("user") // 类映射
    public class UserController {
        @Autowired // 自动装配
        UserinfoService userinfoService;
        @RequestMapping("findById") // 方法映射
        @ResponseBody // 响应ajax
        public Userinfo findById(@RequestParam("id") Long id){
            return userinfoService.findById( id );
        }
    }
    

    serviceImpl

    @Service
    public class UserinfoServiceImpl implements UserinfoService {
        @Autowired // 装配redis
        StringRedisTemplate stringRedisTemplate;
    
        @Autowired
        UserinfoMapper userinfoMapper;
        @Override
        public Userinfo findById(Long id) {
            stringRedisTemplate.opsForValue().set("userinfo","hello");
            return userinfoMapper.findById( id );
        }
    }
    

    注意
    mapper.xml文件在resources文件夹下创建

    image.png

    SpringBoot脚手架

    1560153689533.png
    下一步后选择对应的依赖,自动生成下图。
    image.png
    还记得静态资源放行吗

    resources下只要是static、resources、templates文件夹下的文件,springboot自动会对其放行。不过目前流行的前后端分离项目,该处用处不大。

    相关文章

      网友评论

          本文标题:SpringBoot

          本文链接:https://www.haomeiwen.com/subject/cqftfctx.html