springboot入门学习

目录

第1节 Spring Boot

什么是Spring boot?

Spring Boot特性

Spring Boot特性理解

Hello World 之pom.xml

Hello World 之coding

第2节 spring boot返回json数据

第3节 Spring Boot完美使用FastJson解析JSON数据

引入fastjson依赖库

配置fastjon(支持两种方法)

第4节 Spring Boot热部署(springloader)

使用方式

 第5节 springboot + devtools(热部署)

spring-boot-devtools

使用方法

测试方法

不能使用分析

第6节  Spring Boot JPA/Hibernate/Spring Data概念

第7节 Spring Boot JPA-Hibernate

在pom.xml添加mysql,spring-data-jpa依赖

在application.properties文件中配置mysql连接配置文件、JPA配置信息

编写测试例子

第8节 Spring Boot Spring Data JPA介绍

Repository接

CrudRepository接口

PagingAndSortingRepository接口

其它接口

第9节 Spring Boot JdbcTemplate

在pom.xml加入jdbcTemplate的依赖

代码使用

编写DemoDao类

编写DemoService类,引入DemoDao进行使用

编写Demo2Controller进行简单测试

第10节全局异常捕捉

核心代码

第11节 Spring Boot之Hello World访问404

第12节 配置server信息

配置端口号

配置context-path

其它server配置

第13节 spring boot使用thymeleaf

在pom.xml中引入thymeleaf

如何关闭thymeleaf缓存

编写模板文件.html

编写访问模板文件controller

第14节 Spring Boot 使用freemarker

如何关闭freemarker缓存

编写模板文件.ftl

编写访问文件的controller

第15节 Spring Boot添加JSP支持

(1)创建Maven web project

(2)在pom.xml文件添加依赖

(3)application.properties配置

(4)编写测试Controller

(5)编写JSP页面

(6)编写启动类

第16节 Spring Boot集成MyBatis

操作步骤

(1)新建maven project;

(2)在pom.xml文件中引入相关依赖

(3)创建启动类App.java

(4)在application.properties添加配置文件

(5)编写Demo测试类

(6)编写DemoMapper

(7)编写DemoService

(8)编写DemoController

(9)加入PageHelper

(10)获取自增长ID

第17节task任务

二、starter的实现方法

1、新建一个工程

2、pom依赖

3、定义一个实体类映射配置信息

4、定义一个Service

5,定义一个配置类

 6、最重要的来了


第1节 Spring Boot

什么是Spring boot?

Spring Boot其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。

Spring Boot特性

1. 创建独立的Spring应用程序

2. 嵌入的Tomcat,无需部署WAR文件

3. 简化Maven配置

4. 自动配置Spring

5. 提供生产就绪型功能,如指标,健康检查和外部配置

6.开箱即用,没有代码生成,也无需XML配置。

Spring Boot特性理解

  1. 为基于Spring的开发提供更快的入门体验
  1. 开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求。
  1. 提供了一些大型项目中常见的非功能特性,如嵌入式服务器、安全、指标,健康检测、外部配置等。
  1. Spring Boot并不是对Spring功能上的增强,而是提供了一种快速使用Spring的方式。

Hello World 之pom.xml

<projectxmlns=" POM/4.0.0"xmlns:xsi=" /2001/XMLSchema-instance"

xsi:schemaLocation= /POM/4.0.0 /xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.kfit</groupId>

<artifactId>spring-boot-hello</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>spring-boot-hello</name>

<url> apache.org</url>

    <!--

spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加。

 -->

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.4.1.RELEASE</version>

    </parent>

   

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->

<java.version>1.8</java.version>

</properties>

<dependencies>

        <!--

           spring-boot-starter-web: MVC,AOP的依赖包....

         -->

        <dependency>

               <groupId>org.springframework.boot</groupId>

               <artifactId>spring-boot-starter-web</artifactId>

               <!--

                   <version></version>

                   由于我们在上面指定了 parent(spring boot)

                -->

        </dependency>

<dependencies>

</project>

Hello World 之coding

  1. Codeing 步骤:
  1. 新建一个Controller类

package com.kfit;

import java.util.Date;

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

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

/**

 * 在这里我们使用RestController  (等待于 @Controller 和 @RequestBody)

 */

@RestController

public class HelloController {

   

    /**

     * 在这里我们使用@RequestMapping 建立请求映射:

     * http://127.0.0.1:8080/hello

     * @return

     */

    @RequestMapping("/hello")

    public String hello(){

        return "hello-2016-12-11.v.0";

    }

   

    @RequestMapping("/hello2")

    public String hello2(){

        return "hello2-2016";

    }

   

    @RequestMapping("/hello3")

    public String hello3(){

        return "hello3";

    }

   

    /**

     * Spring Boot默认使用的json解析框架是jackson

     * @return

     */

    @RequestMapping("/getDemo")

    public Demo getDemo(){

        Demo demo = new Demo();

        demo.setId(1);

        demo.setName("张三");

        demo.setCreateTime(new Date());

        demo.setRemarks("这是备注信息");

        return demo;

    }

}

  1. 新建启动类(App – Main方法)

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;

import org.springframework.context.annotation.Bean;

import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**

 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.

 */

//extends WebMvcConfigurerAdapter

@SpringBootApplication

public class App{

    /**

     * 这是springloader的配置方式:-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

     * @param args

     */

    public static void main(String[] args) {

        /*

         * 在main方法进行启动我们的应用程序.

         */

        SpringApplication.run(App.class, args);

    }

}

第2节 spring boot返回json数据

  1. 步骤:
  1. 1. 编写实体类Demo

package com.kfit;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;

/**

 * 这是一个测试实体类.

 */

public class Demo {

    private int id;

    private String name;

   

    //com.alibaba.fastjson.annotation.JSONField

    @JSONField(format="yyyy-MM-dd HH:mm")

    private Date createTime;//创建时间.

   

    /*

     * 我们不想返回remarks?

     * serialize:是否需要序列化属性.

     */

    @JSONField(serialize=false)

    private String remarks;//备注信息.

   

   

    public String getRemarks() {

        return remarks;

    }

    public void setRemarks(String remarks) {

        this.remarks = remarks;

    }

    public Date getCreateTime() {

        return createTime;

    }

    public void setCreateTime(Date createTime) {

        this.createTime = createTime;

    }

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

}  

  1. 2. 编写getDemo()方法

/**

     * Spring Boot默认使用的json解析框架是jackson

     * @return

     */

    @RequestMapping("/getDemo")

    public Demo getDemo(){

        Demo demo = new Demo();

        demo.setId(1);

        demo.setName("张三");

        demo.setCreateTime(new Date());

        demo.setRemarks("这是备注信息");

        returndemo;

    }

第3节 Spring Boot完美使用FastJson解析JSON数据

  1. 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来比较不习惯,所以很自然我就想我能不能使用fastjson进行json解析呢?

引入fastjson依赖库

<dependency>

           <groupId>com.alibaba</groupId>

           <artifactId>fastjson</artifactId>

           <version>1.2.15</version>

</dependency>

这里要说下很重要的话,官方文档说的1.2.10以后,会有两个方法支持HttpMessageconvert,一个是FastJsonHttpMessageConverter,支持4.2以下的版本,一个是FastJsonHttpMessageConverter4支持4.2以上的版本,具体有什么区别暂时没有深入研究。这里也就是说:低版本的就不支持了,所以这里最低要求就是1.2.10+。

配置fastjon(支持两种方法)

  1. 第一种方法就是:
  1. (1)启动类继承extends WebMvcConfigurerAdapter
  1. (2)覆盖方法configureMessageConverters

package com.kfit;

import java.util.List;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.http.converter.HttpMessageConverter;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**

 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.

 */

//extends WebMvcConfigurerAdapter

@SpringBootApplication

publicclass App extends WebMvcConfigurerAdapter{

   

    @Override

    publicvoid configureMessageConverters(List<HttpMessageConverter<?>>converters) {

        super.configureMessageConverters(converters);

       

        // 1、需要先定义一个 convert 转换消息的对象;

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

       

        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(

                SerializerFeature.PrettyFormat

        );

       

        //3、在convert中添加配置信息.

fastConverter.setFastJsonConfig(fastJsonConfig);

//4、将convert添加到converters当中.

    converters.add(fastConverter);

    }

    /**

     * 这是springloader的配置方式:-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

     * @param args

     */

    publicstaticvoid main(String[] args) {

        /*

         * main方法进行启动我们的应用程序.

         */

        SpringApplication.run(App.class, args);

    }

}

第二种方法

  1. (1)在App.java启动类中,注入Bean : HttpMessageConverters

package com.kfit;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;

import org.springframework.context.annotation.Bean;

import org.springframework.http.converter.HttpMessageConverter;

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/**

 * 在这里我们使用@SpringBootApplication指定这是一个 spring boot的应用程序.

 */

@SpringBootApplication

public class App{

    /**

     * 在这里我们使用 @Bean注入 fastJsonHttpMessageConvert

     * @return

     */

    @Bean

    public HttpMessageConverters fastJsonHttpMessageConverters() {

        // 1、需要先定义一个 convert 转换消息的对象;

        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

       

        //2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;

        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

       

        //3、在convert中添加配置信息.

        fastConverter.setFastJsonConfig(fastJsonConfig);

       

        HttpMessageConverter<?> converter = fastConverter;

        return new HttpMessageConverters(converter);

    }

   

    /**

     * 这是springloader的配置方式:-javaagent:.\lib\springloaded-1.2.4.RELEASE.jar -noverify

     * @param args

     */

    public static void main(String[] args) {

        /*

         * 在main方法进行启动我们的应用程序.

         */

        SpringApplication.run(App.class, args);

    }

}

第4节 Spring Boot热部署(springloader)

  1. 问题的提出:
  1. 在编写代码的时候,你会发现我们只是简单把打印信息改变了,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来就真的是打几个Hello World就下班了。那么如何解决热部署的问题呢?那就是springloaded

使用方式

  1. 在pom.xml文件添加依赖包:

<!-- 在这里添加springloaderplugin-->

           <plugin>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-maven-plugin</artifactId>

        <dependencies>

        <dependency>

        <groupId>org.springframework</groupId>

        <artifactId>springloaded</artifactId>

        <version>1.2.4.RELEASE</version>

        </dependency>

        </dependencies>

        <executions>

        <execution>

        <goals>

        <goal>repackage</goal>

        </goals>

        <configuration>

        <classifier>exec</classifier>

        </configuration>

        </execution>

           </executions>

           </plugin>

          

运行方法一:使用spring-boot:run

运行方法二:

    • 23
      点赞
    • 29
      收藏
      觉得还不错? 一键收藏
    • 打赏
      打赏
    • 0
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包

    打赏作者

    棉花糖老丫

    你的鼓励将是我创作的最大动力

    ¥1 ¥2 ¥4 ¥6 ¥10 ¥20
    扫码支付:¥1
    获取中
    扫码支付

    您的余额不足,请更换扫码支付或充值

    打赏作者

    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

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

    余额充值