在上一节基础上增加数据控制层(mysql)
话不多说,直接上干货
增加的目录结构如下:
新增依赖配置如下
configurations.all {
exclude group: "org.slf4j", module: "slf4j-log4j12"
exclude group: "log4j", module: "log4j"
}
buildscript {
ext {
springBootVersion = '1.4.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/plugins-snapshot" }
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.6.0.BUILD-SNAPSHOT"
classpath "org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE"
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
war {
baseName = 'TESTBOOT'
version = '1.0.0'
}
repositories {
mavenCentral()
}
//JDK版本
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
//providedCompile("org.springframework.boot:spring-boot-starter-tomcat")
compile group: 'com.alibaba', name: 'druid', version:'0.2.15'
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-amqp")
compile("org.springframework.boot:spring-boot-starter-actuator")
compile("org.springframework.boot:spring-boot-starter-test")
compile("org.springframework:spring-tx")
compile("org.mybatis:mybatis-spring:1.2.5")
compile("org.mybatis:mybatis:3.3.1")
compile("io.springfox:springfox-swagger2:2.2.2")
compile("io.springfox:springfox-swagger-ui:2.2.2")
compile("org.apache.httpcomponents:httpclient:4.5.1")
compile('com.alibaba:fastjson:1.2.47')
compile('mysql:mysql-connector-java:5.1.18')
compile('com.google.zxing:core:3.2.1')
compile "ch.qos.logback:logback-classic:1.1.3"
compile "org.slf4j:log4j-over-slf4j:1.7.13"
compile fileTree(dir: './libs', include: '*.jar')
testCompile("junit:junit")
}
//添加以下配置
task wrapper(type: Wrapper) {
gradleVersion = '3.4'
}
如果有自己的自定义包直接放到libs文件夹内,刷新gradle即可;
增加数据库配置信息application-dev.properties
#数据源配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ant?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root
#验证连接的有效性
spring.datasource.test-while-idle=true
#获取连接时候验证,会影响性能
spring.datasource.test-on-borrow=true
#在连接归还到连接池时是否测试该连接
spring.datasource.test-on-return=true
spring.datasource.validation-query=SELECT 1 FROM DUAL
#空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
spring.datasource.time-between-eviction-runs-millis=300000
#连接池空闲连接的有效时间 ,设置30分钟
spring.datasource.min-evictable-idle-time-millis=1800000
spring.datasource.max-idle=20
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
#开发环境端口配置
server.port=8725
#log4j日志输出配置
logging.level.org.apache.ibatis=DEBUG
logging.level.org.springframework.jdbc=DEBUG
logging.level.org.springframework.boot=DEBUG
logging.level.java.sql.Connection=DEBUG
logging.level.java.sql.Statement=DEBUG
logging.level.java.sql.PreparedStatement=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
增加dao层代码
package com.ant.app.dao;
import com.ant.app.entity.AboutUs;
import org.apache.ibatis.annotations.Select;<