SpringBoot学习资料.docx

上传人:b****4 文档编号:4416797 上传时间:2022-12-01 格式:DOCX 页数:35 大小:87.57KB
下载 相关 举报
SpringBoot学习资料.docx_第1页
第1页 / 共35页
SpringBoot学习资料.docx_第2页
第2页 / 共35页
SpringBoot学习资料.docx_第3页
第3页 / 共35页
SpringBoot学习资料.docx_第4页
第4页 / 共35页
SpringBoot学习资料.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

SpringBoot学习资料.docx

《SpringBoot学习资料.docx》由会员分享,可在线阅读,更多相关《SpringBoot学习资料.docx(35页珍藏版)》请在冰豆网上搜索。

SpringBoot学习资料.docx

SpringBoot学习资料

SpringBoot

一、Spring介绍

1.1、SpringBoot简介

在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?

在你第n次使用Spring框架的时候,是否觉得一堆反复黏贴的配置有一些厌烦?

那么您就不妨来试试使用SpringBoot来让你更易上手,更简单快捷地构建Spring应用!

SpringBoot让我们的Spring应用变的更轻量化。

比如:

你可以仅仅依靠一个Java类来运行一个Spring引用。

你也可以打包你的应用为jar并通过使用java-jar来运行你的SpringWeb应用。

SpringBoot的主要优点:

为所有Spring开发者更快的入门

开箱即用,提供各种默认配置来简化项目配置

内嵌式容器简化Web项目

没有冗余代码生成和XML配置的要求

本章主要目标完成SpringBoot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对SpringBoot有一个初步的了解,并体验其结构简单、开发快速的特性。

1.2、系统要求:

Java7及以上

SpringFramework4.1.5及以上

本文采用Java1.8.0_73、SpringBoot1.3.2调试通过。

二、快速入门

2.1、创建一个Maven工程

名为”springboot-helloworld”类型为Jar工程项目

2.2、pom文件引入依赖

org.springframework.boot

spring-boot-starter-parent

1.3.3.RELEASE

—SpringBootweb组件-->

org.springframework.boot

spring-boot-starter-web

spring-boot-starter-parent作用

在pom.xml中引入spring-boot-start-parent,spring官方的解释叫什么staterpoms,它可以提供dependencymanagement,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了,后面可以看到。

spring-boot-starter-web作用

springweb核心组件

spring-boot-maven-plugin作用

 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。

如果使用maven 的spring-boot:

run的话是不需要此配置的。

(我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的。

2.3、编写HelloWorld服务

创建package命名为com.itmayiedu.controller(根据实际情况修改)

创建HelloController类,内容如下

@RestController

@EnableAutoConfiguration

publicclassHelloController{

@RequestMapping("/hello")

publicStringindex(){

return"HelloWorld";

}

publicstaticvoidmain(String[]args){

SpringApplication.run(HelloController.class,args);

}

}

2.4、@RestController

在上加上RestController表示修饰该Controller所有的方法返回JSON格式,直接可以编写

Restful接口

2.5、@EnableAutoConfiguration

注解:

作用在于让SpringBoot  根据应用所声明的依赖来对Spring框架进行自动配置

    这个注解告诉SpringBoot根据添加的jar依赖猜测你想如何配置Spring。

由于spring-boot-starter-web添加了Tomcat和SpringMVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。

2.6SpringApplication.run(HelloController.class,args);

标识为启动类

2.6.1

@SpringbootApplication

   使用@SpringbootApplication注解 可以解决根类或者配置类(我自己的说法,就是main所在类)头上注解过多的问题,一个@SpringbootApplication相当于@Configuration,@EnableAutoConfiguration和 @ComponentScan 并具有他们的默认属性值

@SpringBootApplication //等同于@Configuration@EnableAutoConfiguration@ComponentScanpublic

class Application{   

    public static void main(String[]args){

        SpringApplication.run(Application.class,args);

    }

 

2.7、SpringBoot启动方式1

Springboot默认端口号为8080

@RestController

@EnableAutoConfiguration

publicclassHelloController{

@RequestMapping("/hello")

publicStringindex(){

return"HelloWorld";

}

publicstaticvoidmain(String[]args){

SpringApplication.run(HelloController.class,args);

}

}

 

启动主程序,打开浏览器访问http:

//localhost:

8080/index,可以看到页面输出HelloWorld

2.8、SpringBoot启动方式2

@ComponentScan(basePackages="com.itmayiedu.controller")---控制器扫包范围

@ComponentScan(basePackages="com.itmayiedu.controller")

@EnableAutoConfiguration

publicclassApp{

publicstaticvoidmain(String[]args){

SpringApplication.run(App.class,args);

}

}

三、Web开发

3.1、静态资源访问

在我们开发Web应用的时候,需要引用大量的js、css、图片等静态资源。

默认配置

SpringBoot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:

/static

/public

/resources

/META-INF/resources

举例:

我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件。

启动程序后,尝试访问http:

//localhost:

8080/D.jpg。

如能显示图片,配置成功。

3.2、全局捕获异常

@ExceptionHandler表示拦截异常

∙@ControllerAdvice是controller的一个辅助类,最常用的就是作为全局异常处理的切面类

∙@ControllerAdvice可以指定扫描范围

∙@ControllerAdvice约定了几种可行的返回值,如果是直接返回model类的话,需要使用@ResponseBody进行json转换

o返回String,表示跳到某个view

o返回modelAndView

o返回model+@ResponseBody

@ControllerAdvice

publicclassGlobalExceptionHandler{

@ExceptionHandler(RuntimeException.class)

@ResponseBody

publicMapexceptionHandler(){

Mapmap=newHashMap();

map.put("errorCode","101");

map.put("errorMsg","系統错误!

");

returnmap;

}

}

3.3、渲染Web页面

渲染Web页面

在之前的示例中,我们都是通过@RestController来处理请求,所以返回的内容为json对象。

那么如果需要渲染html页面的时候,要如何实现呢?

模板引擎

在动态HTML实现上SpringBoot依然可以完美胜任,并且提供了多种模板引擎的默认配置支持,所以在推荐的模板引擎下,我们可以很快的上手开发动态网站。

SpringBoot提供了默认配置的模板引擎主要有以下几种:

∙Thymeleaf

∙FreeMarker

∙Velocity

∙Groovy

∙Mustache

SpringBoot建议使用这些模板引擎,避免使用JSP,若一定要使用JSP将无法实现SpringBoot的多种特性,具体可见后文:

支持JSP的配置

当你使用上述模板引擎中的任何一个,它们默认的模板配置路径为:

src/main/resources/templates。

当然也可以修改这个路径,具体如何修改,可在后续各模板引擎的配置属性中查询并修改。

3.4、使用Freemarker模板引擎渲染web视图

3.4.1、pom文件引入:

--引入freeMarker的依赖包.-->

org.springframework.boot

spring-boot-starter-freemarker

3.4.2、后台代码

在src/main/resources/创建一个templates文件夹,后缀为*.ftl

@RequestMapping("/index")

publicStringindex(Mapmap){

map.put("name","美丽的天使...");

return"index";

}

3.4.3、前台代码

DOCTYPEhtml>

${name}

3.4.4、Freemarker其他用法

@RequestMapping("/index")

publicStringindex(Mapmap){

map.put("name","###蚂蚁课堂###");

map.put("sex",1);

Listuserlist=newArrayList();

userlist.add("余胜军");

userlist.add("张三");

userlist.add("李四");

map.put("userlist",userlist);

return"index";

}

DOCTYPEhtml>

首页

${name}

<#ifsex==1>

<#elseifsex==2>

<#else>

其他

<#listuserlistasuser>

${user}

3.4.5、Freemarker配置

新建application.properties文件

########################################################

###FREEMARKER(FreeMarkerAutoConfiguration)

########################################################

spring.freemarker.allow-request-override=false

spring.freemarker.cache=true

spring.freemarker.check-template-location=true

spring.freemarker.charset=UTF-8

spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=false

spring.freemarker.expose-session-attributes=false

spring.freemarker.expose-spring-macro-helpers=false

#spring.freemarker.prefix=

#spring.freemarker.request-context-attribute=

#spring.freemarker.settings.*=

spring.freemarker.suffix=.ftl

spring.freemarker.template-loader-path=classpath:

/templates/

#comma-separatedlist

#spring.freemarker.view-names=#whitelistofviewnamesthatcanberesolved

3.5、使用JSP渲染Web视图

3.5.1、pom文件引入以下依赖

org.springframework.boot

spring-boot-starter-parent

1.3.3.RELEASE

--SpringBoot核心组件-->

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-tomcat

org.apache.tomcat.embed

tomcat-embed-jasper

3.5.2、在application.properties创建以下配置

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

3.5.3、后台代码

@Controller

publicclassIndexController{

@RequestMapping("/index")

publicStringindex(){

return"index";

}

}

 

四、数据访问

4.1、springboot整合使用JdbcTemplate

4.1.1pom文件引入

org.springframework.boot

spring-boot-starter-parent

1.5.2.RELEASE

org.springframework.boot

spring-boot-starter-jdbc

mysql

mysql-connector-java

5.1.21

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-web

4.1.2application.properties新增配置

spring.datasource.url=jdbc:

mysql:

//localhost:

3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

4.1.3UserService类

@Service

publicclassUserServiceImplimplementsUserService{

@Autowired

privateJdbcTemplatejdbcTemplate;

publicvoidcreateUser(Stringname,Integerage){

System.out.println("ssss");

jdbcTemplate.update("insertintousersvalues(null,?

?

);",name,age);

}

}

4.1.4App类

@ComponentScan(basePackages="com.itmayiedu")

@EnableAutoConfiguration

publicclassApp{

publicstaticvoidmain(String[]args){

SpringApplication.run(App.class,args);

}

}

注意:

spring-boot-starter-parent要在1.5以上

4.2、springboot整合使用mybatis

4.2.1、pom文件引入

org.springframework.boot

spring-boot-starter-parent

1.3.2.RELEASE

--lookupparentfromrepository-->

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.mybatis.spring.boot

mybatis-spring-boot-starter

1.1.1

mysql

mysql-connector-java

5.1.21

org.springframework.boot

spring-boot-starter-web

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1