SpringMVC整合Spring

xiaoxiao2021-02-28  77

三、测试数据库连接

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,下一篇接着看)

上一篇:springfox整合SpringMVC

下一篇:SpringMVC整合Mybatis

转载请注明原文地址: https://www.6miu.com/read-73337.html

最新回复(0)