IDEA快速构建一个SpringBoot项目
1.new一个Spring Initialiar 项目
2.设置项目名字等
3.选择依赖,随便选几个就行,后面再到POM加依赖就行
4.POM文件加依赖,要用什么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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jinv</groupId>
<artifactId>studentinfo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>studentinfo</name>
<description>a system of studentinfo management</description>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--Pagehelper分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--引入Aspect-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!--使用druid数据库连接池和阿里fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--数据库5.7最好不要配置8以上的connector-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入atlassian(将markdown形式转成html格式的)-->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.14.0</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-gfm-tables</artifactId>
<version>0.14.0</version>
</dependency>
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-heading-anchor</artifactId>
<version>0.14.0</version>
</dependency>
<!--引入@Data所用的依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.设置application.yml配置文件
spring:
thymeleaf:
prefix:
classpath: /templates #访问template下的html文件需要配置模板,映射
mode: HTML
cache: false #关闭缓存,可以看到页面实时情况
encoding: UTF-8
profiles:
active: dev
servlet:
content-type: text/html
multipart:
max-file-size: 30MB
max-request-size: 30MB
devtools:
restart:
enabled: true
mvc:
hiddenmethod:
filter:
enabled: true
#配饰mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml #注意:一定要对应mapper映射xml文件的所在路径
type-aliases-package: com.jinv.studentinfo.domain # 注意:对应实体类的路径
server:
servlet:
context-path: /studentinfo #配置项目根路径
port: 8081
#comment.avatar: /images/avatar.jpg
6.可能还有application-dev.yml(开发开发)和application-pro.yml(项目运营)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# filters: stat # 监控统计拦截的filters
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://120.25.251.42:3506/ali_studentmanagement?useEncoding=true&characterEncoding=utf8
username: root
password: 123456
initial-size: 1 #初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
min-idle: 5 #最小连接池数量
max-active: 50 #最大连接池数量
max-wait: 60000 #获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降
time-between-eviction-runs-millis: 60000 #间隔多久进行一次检测,检测需要关闭的空闲连接
min-evictable-idle-time-millis: 30000 #配置一个连接在池中最小生存的时间,单位是毫秒
validation-query: SELECT 'x' #用来检测连接是否有效的sql,要求是一个查询语句。
test-while-idle: true #建议配置为true,不影响性能,并且保证安全性。
test-on-borrow: false #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
test-on-return: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
#打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false
pool-prepared-statements: false #是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
max-pool-prepared-statement-per-connection-size: 20
redis:
host: 39.97.123.123
port: 6379
database: 0 #总共有16个database(0~15)
timeout: 1000s #数据库链接超时时间,2.0中这个参数的类型为Duration,这里在配置的时候需要指明单位
password: 123456 #redis数据库密码
# 连接池配置,2.0中直接使用jedis或者lettuce配置连接池
jedis:
pool:
#最大空闲链接数
max-idle: 500
#最小空闲连接数
min-idle: 50
#等待可用链接的最大时间,-1无限制
max-wait: -1
#最大活跃连接数,-1无限制
max-active: -1
cache:
redis:
time-to-live: -1 #毫秒
#配置日志
logging:
level:
root: info
com.jinv.blog: info
file: log/studentinfo-dev.log
#配置pagehelper分页
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
7.设置启动类加入dao层扫描不然无法将dao组建加入spring容器导致项目无法正常运行
8.设置目录结构domain、dao(@Repository)、service(service的实现类impl@Service)、web(@Controller)层
9.设置mapper文件(这里是随便搞一个测试的)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.jinv.studentinfo.dao.UserDao">
<resultMap id="user" type="com.jinv.studentinfo.domain.User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="description" column="description"/>
</resultMap>
<select id="findByUserId" resultMap="user" parameterType="java.lang.Long">
select * from user where id=#{id}
</select>
</mapper>
10.test包下测试框架架起来没有
11.还有一些拦截器(登录拦截器),错误类,工具类以及很多其他组件需要额外添加,这里不一一细说