今天第一次使用IDEA开发工具,搭建SpringBoot项目,记录下重点步骤。
1、IDEA 创建基于Maven管理的Java项目。
2、pom.xml 文件添加
jar包依赖:SpringBoot版本、MyBatis、MySQL8驱动和Alibaba Druid 数据库连接池。
插件依赖:添加maven 插件和MyBatis-Generator 代码生产工具。
4.0.0 org.springframework.boot spring-boot-starter-parent2.2.2.RELEASE org.zzg SmartHouse1.0-SNAPSHOT 1.8 8 8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.junit.vintage junit-vintage-enginejunit junitRELEASE compile org.mybatis.spring.boot mybatis-spring-boot-starter1.3.2 com.alibaba druid-spring-boot-starter1.1.10 mysql mysql-connector-java8.0.12 org.springframework.boot spring-boot-maven-plugin2.6.6 org.mybatis.generator mybatis-generator-maven-plugin1.3.5 src/main/resources/mybatis-generator/mybatis-generator-cfg.xml true true org.mybatis.generator mybatis-generator-core1.3.5 mysql mysql-connector-java8.0.12
3、添加application.properties 配置文件,主要包含如下配置:项目端口、项目名称设置、数据库连接、druid 连接配置参数、mybatis扫描配置和日志输出级别设置。
server.prot=8080
server.servlet.context-path=/smartHouse
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/house?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# druid 配置
# 初始化时建立物理连接的个数
spring.datasource.druid.initial-size=5
# 最大连接池数量
spring.datasource.druid.max-active=30
# 最小连接池数量
spring.datasource.druid.min-idle=5
# 获取连接时最大等待时间,单位毫秒
spring.datasource.druid.max-wait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.druid.time-between-eviction-runs-millis=60000
# 连接保持空闲而不被驱逐的最小时间
spring.datasource.druid.min-evictable-idle-time-millis=300000
# 用来检测连接是否有效的sql,要求是一个查询语句
spring.datasource.druid.validation-query=SELECt 1 FROM DUAL
# 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
spring.datasource.druid.test-while-idle=true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
spring.datasource.druid.test-on-borrow=false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
spring.datasource.druid.test-on-return=false
# 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
spring.datasource.druid.pool-prepared-statements=true
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=50
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计
spring.datasource.druid.filters=stat,wall
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 合并多个DruidDataSource的监控数据
spring.datasource.druid.use-global-data-source-stat=true
# mybatis 配置
mybatis.mapper-locations=classpath:mapper
@SpringBootApplication
@MapperScan("com.zzg.mapper")
public class ApplicationStart {
public static void main(String[] args) {
SpringApplication.run(ApplicationStart.class, args);
}
}
FirstController:主要实现Mapper 接口调用。
package com.zzg.controller;
import com.zzg.mapper.UserMapper;
import com.zzg.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class FirstController {
@Autowired
private UserMapper mapper;
@RequestMapping("/hello")
public String get() {
List list = mapper.selectAll();
return "Hello Spring Boot!";
}
}



