三、測試數據庫連接
1.添加jar
mybatis-3.4.2.jar
mysql-connector-java-5.1.40-bin.jar
junit-4.12.jar
2.在resources下添加db-config.properties
## MySQL
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bphss?useSSL=true
username=root
password=123456
## Oracle
## oracle=jdbc:oracle:thin:@10.20.149.85:1521:ocnauto
#定義初始連接數 ,缺省值:0
initialSize=0
#定義最大連接池數量,缺省值:8
maxActive=20
#定義最小空閒
minIdle=1
#定義最大空閒,缺省值:8;已經不再使用,配置了也沒效果
## maxIdle=20
## 定義最長等待時間,單位:毫秒;
## 配置了maxWait之後,缺省啓用公平鎖,併發效率會有所下降,
## 如果需要可以通過配置useUnfairLock屬性爲true使用非公平鎖。
maxWait=60000
#配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
timeBetweenEvictionRunsMillis=60000
#配置一個連接在池中最小生存的時間,單位是毫秒
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
#是否緩存preparedStatement,也就是PSCache。 缺省爲false;
#PSCache對支持遊標的數據庫性能提升巨大,比如說oracle。
#在mysql5.5以下的版本中沒有PSCache功能。
#可通過監控界面發現PSCache有緩存命中率記錄。
poolPreparedStatements=false
#指定每個連接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize=20
3.添加com/chensan/web/test/TestDataBaseConnection.java
TestDataBaseConnection
package com.chensan.config;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.Test;
public class TestDataBaseConnection {
@Test
public void getConnection(){
Connection connection = null;
try {
//加載屬性文件,讀取數據庫連接配置信息
Properties prop = new Properties();
try {
prop.load(TestDataBaseConnection.class.getResourceAsStream("/db-config.properties"));
} catch (IOException e) {
System.out.println("未找到配置文件!!!");
}
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
connection = DriverManager.getConnection(url, username, password);
System.out.println("數據庫連接【成功】。。。");
System.out.println("數據庫連接:"+ connection);
} catch (SQLException e) {
System.out.println("數據庫連接【失敗】。。。");
e.printStackTrace();
}
}
}
測試報錯:
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
原因:junit4.1.1中沒有hamcrest包了。
使用是導入包的方案:junit.jar + hamcrest-core.jar + hamcrest-library.jar
或者是:junit-dep.ajr+hancrest-all.jar
hamcreate的jar可在http://www.jarvana.com/下載;
這兩種導入方法雖然儘量避免了導入重複的包,但使用時還是遇到了衝突。查看包中各類和文檔後發現有些類(例如:斷言is())同時出現在了org.hamcrest.Mathchers和org.hamcrest.core中,則在用到時候引入的時候需要注意。
我這裏添加了hamcreate-core-1.3.jar、hamcreate-library-1.3.jar
測試:TestDataBaseConnection的getConnection右鍵Run As-->Junit Test
根據控制檯打印信息,查看數據庫連接是否成功。
四、SpringMVC整合Spring
1.加入jar:
spring-jdbc-4.3.4.RELEASE.jar
spring-tx-4.3.4.RELEASE.jar
druid-1.0.27.jar
2.在web.xml添加
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3.spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db-config.properties" />
</bean>
<!-- 數據源配置 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
<!-- 配置獲取連接等待超時的時間 -->
<property name="maxWait" value="${maxWait}" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<!-- 打開PSCache,並且指定每個連接上PSCache的大小 -->
<property name="poolPreparedStatements" value="${poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${maxPoolPreparedStatementPerConnectionSize}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
</beans>
數據庫連接的配置單獨放在db-config.properties中是爲了後期改變數據庫連接配置方便;當然可以直接在spring-config.xml中寫定;啓動項目不報錯,
http://localhost/bphss-sample/swagger-ui.html可展現出Controller的列表;
http://localhost/bphss-sample/index/test1,訪問/WEB-INF/jsp/test/index.jsp,
則SpringMVC整合Spring成功!(顯然SpringMVC整合Spring是沒有意義的,需要整合ORM,下一篇接着看)