本文主要是介绍SpringBoot框架集成Thymeleaf:、以及使用Hikari方式访问数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SpringBoot框架集成Thymeleaf:、以及使用Hikari方式访问数据库
开发环境:
MySQL:5.1.30
Maven:3.5.2
IDEA:2019.03
项目目录:
添加依赖:
配置pom.xml文件,
<?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.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.zhaojian</groupId><artifactId>demo</artifactId><version>1.0.0-SNAPSHOT</version><name>daily</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!-- 整合thymeleaf前端页面 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId><version>1.9.22</version></dependency><!-- 主要增加 HikariCP依赖 --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><exclusions><!--排除默认的tomcat-jdbc--><exclusion><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId></exclusion></exclusions></dependency><!-- Mysql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.30</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置JDBC连接:
jdbc.properties文件
#数据库设置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/数据库名 useSSL=false&useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root
application.yml文件
spring:application:name: zhaojianthymeleaf:cache: falsemode: HTMLencoding: UTF-8servlet:content-type: text/htmldatasource:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/ptzy?useSSL=false&useUnicode=true&characterEncoding=utf-8username: rootpassword: roothikari:minimum-idle: 5idle-timeout: 600000maximum-pool-size: 10auto-commit: truemax-lifetime: 1800000connection-timeout: 30000connection-test-query: SELECT 1
这篇关于SpringBoot框架集成Thymeleaf:、以及使用Hikari方式访问数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!