>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>欢迎转载,转载请注明出处-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}"进行绑定。

创建、使用SpringBoot项目的更多相关文章

  1. Intellij创建简单Springboot项目

    Intellij创建简单Springboot项目 第一步:选择创建新项目——file-new-project 第二步:选择项目类型——Spring Initializr-next 第三步:输入项目信息 ...

  2. spting Boot 创建一个springBoot项目

    spting Boot 创建一个springBoot项目 1)学习springBoot使用软件:IDEA软件(前面的文章有安装idea的过程). 也可以使用另一种方法在https://start.sp ...

  3. 使用IDEA创建一个springboot项目

    工欲善其事,必先利其器. 不难发现,还是有很多小朋友在使用eclipse开发java项目.当你接触IDEA后,一切都变得美好了. 使用IDEA创建一个springboot项目是一件极其简单的事情.界面 ...

  4. 如何使用IDEA快速创建一个springboot项目

    如何使用IDEA快速创建一个springboot项目 https://jingyan.baidu.com/article/0964eca24fdd938284f53640.html

  5. 快速创建一个springboot项目

    创建一个maven项目(springboot.mybatis-plus) 目标:可以访问ftl页面.对象(json字符串),可以进行单元测试 1.新建一个maven项目,选择模板maven-arche ...

  6. IDEA上创建 Maven SpringBoot项目发布到Tomcat

    概述 上篇记录了IDEA上创建Maven SpringBoot+mybatisplus+thymeleaf 项目,但是如何将SpringBoot发布到Tomcat,直接采用Maven 命令Clear- ...

  7. idea创建一个springboot项目

    第一种通过maven创建: 1.点击Create New Project 2.创建maven项目,选择jdk版本,点击next. 3.填写GroupId和ArtifactId,都是自定义的,然后点击n ...

  8. 从零开始的SpringBoot项目 ( 二 ) 使用IDEA创建一个SpringBoot项目

    工欲善其事 , 必先利其器 . IntelliJ IDEA 2019.3.3 x64的安装与破解 下面详细说明下如何使用idea创建我们的第一个springboot项目: 首先打开idea主界面选择 ...

  9. IDEA快速创建Maven+SpringBoot项目时:Cannot download https://start.spring.io;Status:403

    先展示一下我遇到的问题: 用浏览器搜索是有页面的,但是但是但是呢,用IDEA快速构建的时候就报403 咳咳!巴格虐我万千遍,我待技术如初恋... 我看到的解决办法有以下两种,当然,我只想说:" ...

  10. IDEA创建简单SpringBoot项目

    环境:jdk 1.打开IDEA -->New --> Project -->Spring Initalizer-->next 2.此处,只做创建示例,所以next后Group等 ...

随机推荐

  1. flink部署操作-flink standalone集群安装部署

    flink集群安装部署 standalone集群模式 必须依赖 必须的软件 JAVA_HOME配置 flink安装 配置flink 启动flink 添加Jobmanager/taskmanager 实 ...

  2. springMVC统一异常处理

    Spring MVC处理异常有3种方式: 使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver: 实现Spring的异常处理接口HandlerExc ...

  3. CF 1145 (愚人节比赛)

    D题 题目就是让你找出题面中拼错的单词,拼错的字母组合成一句话就是正确的题面. two plus xor of third and min elements #include<bits/stdc ...

  4. redis-cli 通过管道 --pipe 快速导入数据到redis中

    最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入 一:先看看命令 cat redis.txt | redis-cli -h 12 ...

  5. 超越村后端开发(2:新建models.py+xadmin的引入)

    1.新建Model 1.users数据 1.在apps/users/models.py中: from datetime import datetime from django.db import mo ...

  6. python8--文件操作 with。。。open语法

    复习   一.类型转换 1.数字类型:int() | bool() | float()  2.str与int:int('10') | int('-10') | int('0') | float('-. ...

  7. vue-输入框change事件并获取值

    1.html <input type="text" @change="specifiName($event)" /> 2.js var vm = n ...

  8. libiconv交叉编译提示arm-none-linux-gnueabi-gcc

    title: libiconv交叉编译提示arm-none-linux-gnueabi-gcc date: 2019/3/6 17:45:48 toc: true --- libiconv交叉编译提示 ...

  9. .NET框架 - NETFramework + API + EF(DBFirst) + MYSQL

    .NET框架 - NETFramework + MVC+ EF(DBFirst) + MYSQL 1. 安装3个MYSQL插件 ①mysql-for-visualstudio-1.2.8    vs的 ...

  10. Git打标签、还原到具体标签版本代码

    1.打标签:(在需要合并的分支上先打一个标签,方便回滚) $ git tag 标签名 -----此标签将打在本地,还需要推送至远程仓库 $ git push origin 标签名 -----此步骤执行 ...