创建、使用SpringBoot项目

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-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依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.study</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version> <name>springboot</name>
<description>Demo project for spring boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<!--Spring Web 相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--数据源-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>
<!--JSR3.0,校验-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.13.Final</version>
</dependency> <!--POJO简化-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/static/lib/lombok-1.18.2.jar</systemPath>
</dependency> <!--第三方工具-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.study.App</mainClass>
<layout>JAR</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

  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=9090
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8 #数据源配置信息
spring.datasource.url = jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
spring.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-8
spring.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}"进行绑定。

上一篇:color.xml


下一篇:部署过k8s的centos机器如何开启docker remote api 2375端口