zoukankan      html  css  js  c++  java
  • SpringBoot简介以及案例

    1什么是SpringBoot

    Spring Boot 是所有基于 Spring 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。简单来说就是SpringBoot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架(不知道这样比喻是否合适)。

    2.2、SpringBoot四个主要特性

    1、SpringBoot Starter:他将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中;

    2、自动配置:SpringBoot的自动配置特性利用了Spring4对条件化配置的支持,合理地推测应用所需的bean并自动化配置他们;

    3、命令行接口:(Command-line-interface, CLI):SpringBoot的CLI发挥了Groovy编程语言的优势,并结合自动配置进一步简化Spring应用的开发;

    4、Actuatir:它为SpringBoot应用的所有特性构建一个小型的应用程序。但首先,我们快速了解每项特性,更好的体验他们如何简化

    回顾我们之前的 SSM 项目,搭建过程还是比较繁琐的,需要:

    1、配置web.xml,加载spring和spring mvc

    2、配置数据库连接、配置spring事务

    3、配置加载配置文件的读取,开启注解

    。。。

    配置完成之后部署tomcat 调试

    而使用 Spring Boot 来开发项目则只需要非常少的几个配置就可以搭建起来一个 Web 项目,并且利用 IDEA 可以自动生成生成,这简直是太爽了...

    华丽的分割线----------------------------------------------------------------------------------------------------

    案例:

    一.导入依赖

     <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>jquery</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.1.1</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
            <!-- 添加freemarker模版的依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    View Code

    application.properties文件中新增freemarker配置

    ## Freemarker 配置
    spring.freemarker.template-loader-path=classpath:/templates/
    spring.freemarker.cache=false
    spring.freemarker.charset=UTF-8
    spring.freemarker.check-template-location=true
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=false
    spring.freemarker.expose-session-attributes=false
    spring.freemarker.request-context-attribute=request
    spring.freemarker.prefix=/
    spring.freemarker.suffix=.ftl

    在src/main/resource/templates文件夹中创建index01.ftl文件

     创建controller层

    package com.my.springboot01.controller;
    
    import com.my.springboot01.dao.Student;
    import com.my.springboot01.entity.Estapro;
    import com.my.springboot01.service.EstaService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.*;
    
    @Controller
    @RequestMapping("/boot")
    public class HellowordldController {
        @Autowired
        EstaService service;
    
        @RequestMapping("/hello")
        public String hello(){
            return "我是NB的SpringBoot666";
        }
    
        @RequestMapping("/getAll")
        @ResponseBody
        public List<Estapro> getAll(){
            return service.getAll();
        }
    
        /*异常处理*/
        @RequestMapping("/getTwo")
        @ResponseBody
        public String getTwo(){
            int num=5/0;
            return "异常处理";
        }
    
        @RequestMapping("/freeFirst")
        public String freeFirst(ModelMap map){
            map.put("name","goiaogiao包");
            return "index01";//找index01.ftl
        }
    
        /*返回student类型数据*/
        @RequestMapping("/getstudent")
        @ResponseBody
        public Student getstudent(){
            Student stu1=new Student(1,"");
            return stu1;
        }
    
        /*返回list集合数据*/
        @RequestMapping("/getlist")
        @ResponseBody
        public List<Student> getlist(){
            List<Student> list=new ArrayList<>();
            Student stu1=new Student(1,"鲁班");
            Student stu2=new Student(2,"曾总");
            list.add(stu1);
            list.add(stu2);
            return list;
        }
    
        /*返回set集合数据*/
        @RequestMapping("/getset")
        @ResponseBody
        public Set<Student> getset(){
            Set<Student> set=new HashSet<>();
            Student set1=new Student(1,"");
            Student set2=new Student(2,"");
            set.add(set1);
            set.add(set2);
            return set;
        }
    
        /*返回map集合数据*/
        @RequestMapping("/getmap")
        @ResponseBody
        public Map<Integer,Student> getmap(){
            Map<Integer,Student> map=new HashMap<>();
            Student map1=new Student(1,"你们");
            Student map2=new Student(2,"我们");
            map.put(1,map1);
            map.put(2,map2);
            return map;
        }
    }
    View Code
  • 相关阅读:
    关于http协议详解
    python记录_day15 面向对象初识
    python记录_day14 内置函数二 迭代 二分法
    python记录_day13 内置函数
    python记录_day12 生成器
    python记录_day11 闭包 迭代器
    函数默认参数的初始化问题
    python记录_day10 动态传参 命名空间 作用域
    python记录_day09 初识函数
    python记录_day08
  • 原文地址:https://www.cnblogs.com/mayuan01/p/12016478.html
Copyright © 2011-2022 走看看