spring框架,整合连接池,整合junit测试

让spring知道外部的数据库连接信息的配置文件 jdbc.properties 
让spring能够加载jdbc.properties文件
         spring提供了一个标签 可以加载外部的properties文件内容
         导context的名称空间和约束 才会有提示
1. 在/spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html 中找到约束

2.创建jdbc.properties 

可配置多个连接池

根据实际需要连接池配置,以键值对的方式存储数据(因为我的mysql是8.0x版本的所有url需要这么写)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/atm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
jdbc.username=root
jdbc.password=123


#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:oracle://localhost:3306/hibernate
#jdbc.username=root
#jdbc.password=1234


#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:db2://localhost:3306/hibernate
#jdbc.username=root
#jdbc.password=1234

 3.编写applicationContext.xml

<!-- 让spring知道外部的数据库连接信息的配置文件 jdbc.properties -->
	<!-- 让spring能够加载jdbc.properties文件
		 spring提供了一个标签 可以加载外部的properties文件内容
		 导context的名称空间和约束 才会有提示
	-->
	
	<context:property-placeholder location="jdbc.properties"/>
	<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	<!-- dbcp:
	
	<bean id="dbcp" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean> -->

spring整合Junit做测试
        普通测试的缺点:
                代码复用性大
                配置文件运行一次加载一次
        Spinrg整合junit开发步骤:              
                1 导包
                    spring-test.jar 增强
                    spring-aop.jar 可以写注解
                    junit.jar  还是之前的测试
                2 要告诉spring谁加载配置文件 (SpringJunt4ClassRunner.class)
                3 要告诉spring配置文件的地址
                4 分层测试

package com.it.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.it.dao.UserDao;
import com.it.service.UserService;

//Spinrg整合junit开发步骤:
//		
//		1 导包
//			spring-test.jar 增强
//			spring-aop.jar 可以写注解
//			junit.jar  还是之前的测试
//		2 要告诉spring谁加载配置文件 (SpringJunt4ClassRunner.class)
//		3 要告诉spring配置文件的地址
//		4 分层测试
/*1:告诉spring配置文件在哪个地方*/
@ContextConfiguration(value="classpath:applicationContext.xml")
/*2:告诉spring谁加载配置文件*/
@RunWith(value =SpringJUnit4ClassRunner.class)
public class Junit1 {
	@Autowired
	private UserService userservice;
	@Autowired
	private UserDao userdao;
	@Test
	public void t1() {
		userservice.find();
		userdao.find();
	
	}
}

代码运行结果:好处,可实现分层测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Exception.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值