>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-VirgoArt,www.cnblogs.com
一、环境
JDK:1.8.0
IDEA:2018.3.4
Maven:3.3.9
SpringBoot-Start-Parten:2.1.3.RELEASE
二、步骤
1.新建Maven项目,编辑pom.xml:由于本机pom无法正确识别lombok依赖,无奈使用system方式管理,需要在项目中建立lib文件夹,添加lombok.jar依赖
4.0.0 cn.study springboot 1.0-SNAPSHOT springboot Demo project for spring boot org.springframework.boot spring-boot-starter-parent 2.1.3.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-devtools org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java 5.1.35 com.alibaba druid 1.0.19 org.hibernate hibernate-validator 6.0.13.Final org.projectlombok lombok 1.18.2 system ${project.basedir}/src/main/resources/static/lib/lombok-1.18.2.jar com.google.code.gson gson 2.8.5 org.apache.maven.plugins maven-compiler-plugin org.springframework.boot spring-boot-maven-plugin com.study.App JAR repackage
2.建立三级包结构,并在二级目录下建立项目启动主类:App.java:app中,添加了过滤器的扫描路径,添加了全局配置类ProjectBaseInfo的注入,同时初始化数据库连接池
package com.study;import com.alibaba.druid.pool.DruidDataSource;import com.study.domain.ProjectBaseInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.boot.web.servlet.ServletComponentScan;import org.springframework.context.annotation.Bean;import org.springframework.core.env.Environment;import javax.sql.DataSource;@SpringBootApplication@EnableConfigurationProperties({ProjectBaseInfo.class})@ServletComponentScan("com.study.filter")public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Autowired private Environment env; @Bean() public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(env.getProperty("spring.datasource.url")); dataSource.setUsername(env.getProperty("spring.datasource.username")); dataSource.setPassword(env.getProperty("spring.datasource.password")); dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); //初始化时建立物理连接的个数 dataSource.setInitialSize(2); //最大连接池数量 dataSource.setMaxActive(20); //最小连接池数量 dataSource.setMinIdle(0); //获取连接时最大等待时间,单位毫秒。 dataSource.setMaxWait(60000); //用来检测连接是否有效的sql dataSource.setValidationQuery("SELECT 1"); //申请连接时执行validationQuery检测连接是否有效 dataSource.setTestOnBorrow(false); //建议配置为true,不影响性能,并且保证安全性。 dataSource.setTestWhileIdle(true); //是否缓存preparedStatement,也就是PSCache dataSource.setPoolPreparedStatements(false); return dataSource; }}
3.在类路径下建立application.properties配置文件,建立static资源存放目录,建立templates模板文件存放目录
#全局项目信息#project.baseInfo.name = studySpringBoot#project.baseInfo.author = virgo#project.baseInfo.aim = CRM System#服务器配置信息server.port=9090server.session-timeout=30server.tomcat.uri-encoding=UTF-8#数据源配置信息spring.datasource.url = jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8spring.datasource.username = rootspring.datasource.password = 123456spring.datasource.driver-class-name = com.mysql.jdbc.Driver# THYMELEAF配置#开启模板缓存(默认值:true)spring.thymeleaf.cache=false spring.thymeleaf.check-template=true #检查模板位置是否正确(默认值:true)spring.thymeleaf.check-template-location=true#Content-Type的值(默认值:text/html)spring.thymeleaf.content-type=text/html#开启MVC Thymeleaf视图解析(默认值:true)spring.thymeleaf.enabled=true#模板编码spring.thymeleaf.encoding=UTF-8spring.thymeleaf.mode=HTML5#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)spring.thymeleaf.prefix=classpath:/templates/#在构建URL时添加到视图名称后的后缀(默认值:.html)spring.thymeleaf.suffix=.html#静态资源路径配置spring.mvc.static-path-pattern=/**spring.resources.static-locations = classpath:/static/
4.开始实现项目业务代码,填充controller(@Controller)、domain(@Data)、filter(implements Filter)、service(@Service)、dao(@Repository)
三、注意点
1.关于DaoImpl使用Spring jdbcTemplate,IDEA会报无法加载Bean问题,解决方法:调整IDEA报错级别设置,关闭Spring Bean依赖检查
2.关于使用自定义Fileter后导致静态资源被拦截的情况,建议使用资源路径限定的方式,在Fileter中判断:/static/res/css(/js/font/image...),url.indexOf("/res/css/")!=-1。如果使用后缀法,如果项目上线,浏览器在第一次无Cookie加载时,会在URL后拼接jsessionid字段,导致URL.endsWith(".js")判断为false
3.使用 ModelAndView 时,在构造中处理返回的模板地址时,前面不要添加“/”,原因是当Maven install打成JAR后,页面后端请求默认路径为templates,如果加上“/”,会导致进入项目根目录查找。
4.关于使用Thymeleaf的自动对象绑定时,需要在渲染模板时,传入空的交互对象,然后使用Form表单中的th:object="${bean}"进行绑定。