springBoot使用文档10V10.docx
《springBoot使用文档10V10.docx》由会员分享,可在线阅读,更多相关《springBoot使用文档10V10.docx(23页珍藏版)》请在冰豆网上搜索。
![springBoot使用文档10V10.docx](https://file1.bdocx.com/fileroot1/2023-4/20/b23f6004-58bf-4174-840b-8fe05a6d717f/b23f6004-58bf-4174-840b-8fe05a6d717f1.gif)
springBoot使用文档10V10
SpringBoot使用文档
说明:
一、SpringBoot简介
1.1SpringBoot自述
世界上最好的文档来源自官方的《SpringBootReferenceGuide》,是这样介绍的:
SpringBootmakesiteasytocreatestand-alone,production-gradeSpringbasedApplicationsthatyoucan“justrun”...MostSpringBootapplicationsneedverylittleSpringconfiguration.
SpringBoot(英文中是“引导”的意思),是用来简化Spring应用的搭建到开发的过程。
应用开箱即用,只要通过“justrun”(可能是java-jar或tomcat或maven插件run或shell脚本),就可以启动项目。
二者,SpringBoot只要很少的Spring配置文件(例如那些xml,property)。
因为“习惯优先于配置”的原则,使得SpringBoot在快速开发应用和微服务架构实践中得到广泛应用。
1.2SpringBoot特点
1.3SpringBoot环境支持
二、SpringBoot入门案例-HelloSpringBoot
2.1HelloWord案例
使用maven创建一个基础web项目
添加pom.xml配置如下:
xmlversion="1.0"encoding="UTF-8"?
>
//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.0http:
//maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
springboot
springboot-helloworld
0.0.1-SNAPSHOT
springboot-helloworld:
:
HelloWorldDemo
--SpringBoot启动父依赖-->
org.springframework.boot
spring-boot-starter-parent
1.3.3.RELEASE
--SpringBootweb依赖-->
org.springframework.boot
spring-boot-starter-web
--Junit-->
junit
junit
4.12
2.2创建应用启动类
/**
*程序入口
*@authorAdministrator
*/
@SpringBootApplication
publicclassHelloWordApp{
publicstaticvoidmain(String[]args){
SpringApplication.run(HelloWordApp.class,args);
}
}
@SpringBootApplication注解是springboot应用的标识,表明该应用是springboot应用,main方法为该应用程序的入口,可以直接运行此main方法,通过启用内置的Tomcat并初始化spring环境及各组件;
2.3controller层的编写
/**
*springboot案例-HelloWord
*@authorAdministrator
*/
@RestController
publicclassHelloController{
@RequestMapping("/")
publicStringhelloWord(){
return"HelloWord!
HelloSpringBoot!
";
}
}
@RestController和@RequestMapping注解是来自SpringMVC的注解,它们不是SpringBoot的特定部分。
1.@RestController:
提供实现了RESTAPI,可以服务JSON,XML或者其他。
这里是以String的形式渲染出结果。
2.@RequestMapping:
提供路由信息,"/“路径的HTTPRequest都会被映射到helloWord方法进行处理。
2.4启动与访问
启动项目
访问
内置tomcat启动后默认端口8080,通过浏览器访问http:
//localhost:
8080即可实现访问;
三、SpringBoot整合fastjson
SpringBoot默认使用jackson作为json解析工具;
pom.xml
--spring-boot-starter-web依赖-->
--使用fastjson,先排除对jackson的依赖-->
org.springframework.boot
spring-boot-starter-web
--排除jackson-databind的依赖,自动解除jackson-databind所依赖的资源-->
com.fasterxml.jackson.core
jackson-databind
--fastjson依赖-->
--fastjson1.2.10+之后FastJsonHttpMessageConverter4支持spring4.2以上版本-->
com.alibaba
fastjson
Java代码如下:
/**
*配置SpringBoot支持FastJson有两种方法;
*第一种:
覆写configureMessageConverters的方式完成对fastjson的配置支持;
*1.1启动类extendWebMvcConfigurerAdapter,1.2覆写configureMessageConverters方法
*第二种:
使用Bean注入的方式配置Boot对fastjson的支持(推荐);
*2.1编写相关配置;2.2注入带有fastjson配置信息的HttpMessageConverters;
*@authorSorryNO4
*/
@SpringBootApplication
publicclassFastJsonAppextendsWebMvcConfigurerAdapter{
publicstaticvoidmain(String[]args){
SpringApplication.run(FastJsonApp.class,args);
}
/**
*1.启动类extendWebMvcConfigurerAdapter
*
*2.覆写configureMessageConverters方法
*/
@Override
publicvoidconfigureMessageConverters(List>>converters){
super.configureMessageConverters(converters);
//初始化转换器
FastJsonHttpMessageConverter4fastJsonHttpMessageConverter4=newFastJsonHttpMessageConverter4();
//初始化一个转换器配置
FastJsonConfigfastJsonConfig=newFastJsonConfig();
//设置fastjson的配置信息,如设置是否格式化返回信息
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
//配置转换器
fastJsonHttpMessageConverter4.setFastJsonConfig(fastJsonConfig);
//添加转换器到转换器list中
converters.add(fastJsonHttpMessageConverter4);
}
/**
*第二种方式添加对fastjson的支持
*@return
*/
/*@Bean
publicHttpMessageConvertersfastJsonHttpMessageConverters(){
FastJsonHttpMessageConverter4fastJsonHttpMessageConverter4=newFastJsonHttpMessageConverter4();
FastJsonConfigfastJsonConfig=newFastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastJsonHttpMessageConverter4.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter
>converter4=fastJsonHttpMessageConverter4;
HttpMessageConvertershttpMessageConverters=newHttpMessageConverters(converter4);
returnhttpMessageConverters;
}*/
}
使用fastjson解析工具后就可以使用fastjson工具的特性
/**
*测试fastjson的实体类;
*添加了对fastjson的支持后就可以使用其特性;如@JSONField(format="yyyy-MM-ddHH24:
mm")
*@authorSorryNO4
*/
publicclassFastJsonVo{
privateIntegerid;
/*设置不序列化的字段*/
@JSONField(serialize=false)
privateStringmsg;
/*时间格式化*/
@JSONField(format="yyyy-MM-ddHH24:
mm")
privateDatecreatrtime;
Getterandsetter......
Controller
@RestController
@RequestMapping("/")
publicclassFastJsonController{
/**
*测试SpringBoot对fastjson的支持
*http:
//localhost:
8080/fastjsonvo
*@returnFastJsonVoasjson
*{"creatrtime":
1508001089654,
*"id":
1002,"msg":
"测试SpringBoot对FastJson的支持!
"}
*/
@RequestMapping("fastjsonvo")
publicFastJsonVouseFsatJson(){
FastJsonVovo=newFastJsonVo();
vo.setCreatrtime(newDate());
vo.setId(1002);
vo.setMsg("测试SpringBoot对FastJson的支持!
");
returnvo;
}
}
四、SpringBoot整合springboot-freemarker
pom.xml
org.springframework.boot
spring-boot-starter-web
--添加freemarker依赖-->
org.springframework.boot
spring-boot-starter-freemarker
application.properties
#freemaker
spring.freemarker.allow-request-override=false
spring.freemarker.cache=false
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.suffix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.template-loader-path=
#spring.freemarker.view-names=
Templates/myftl.ftl
Hellofreemarker!
Hello${name}!
controller
@Controller
publicclassFreemarkerController{
@RequestMapping("/myftl")
publicModelAndViewmyFty(HashMapmap){
ModelAndViewview=newModelAndView("myftl");
map.put("name","jack");
returnview;
}
}
五、SpringBoot整合thymeleaf
pom.xml
org.springframework.boot
spring-boot-starter-web
--添加thymeleaf模板-->
org.springframework.boot
spring-boot-starter-thymeleaf
application.properties
#thymeleaf
#关闭缓存,开发过程中关闭缓存
spring.thymeleaf.cache=false
#spring.thymeleaf.prefix=classpath:
/templates/
#spring.thymeleaf.suffix=
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
#spring.thymeleaf.content-type=text/html;charset=utf-8
Templates/one.html
Hellothymeleaf!
Hellotext="${name}">!
controller
@Controller
publicclassTemplatesController{
@RequestMapping("/one")
publicModelAndViewtheOne(HashMapmap){
ModelAndViewview=newModelAndView("one");
map.put("name","tom");
returnview;
}
}
六、SpringBoot整合jsp
Springboot提供对jsp的支持,注意打包方式为war,项目最好具有web结构,在springboot中不建议使用jsp,目前对tomcat7服务器支持较好,对其他的服务器的支持较弱,建议使用thymeleaf渲染数据;
Pom.xml
war
org.springframework.boot
spring-boot-starter-web
javax.servlet
jstl
--springboot添加对jsp的支持,一般整合jsp后建议打包方式为war-->
org.apache.tomcat.embed
tomcat-embed-jasper
provided
application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=8082
controller
@RestController
@RequestMapping("/")
publicclassJspController{
@RequestMapping("index")
publicModelAndViewshowMsg(HashMapmap){
ModelAndViewview=newModelAndView("index");
UserForJspuserForJsp=newUserForJsp();
userForJsp.setId("user001");
userForJsp.setUsername("MAXtom");
userForJsp.setPassword("123");
map.put("name","Tom");
map.put("user",userForJsp);
returnview;
}
}
工程结构
七、SpringBoot整合Oracle数据库
Springboot整合Oracle数据库,springboot本身集成了springdata,对持久层技术的选择很多,如hibernate,springdata,jdbctemplates等,还可以集成mybatis等持久层框架;
Pom.xml
org.springframework.boot
spring-boot-starter-web
org.springfra