package com.kkks.jdbc.dao.impl;
import org.springframework.context.ApplicationContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import com.kkks.jdbc.dao.UserDao;
import com.kkks.pojo.Users;
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext_JDBC.xml")
public class TestJdbc {
@Test
@Rollback(true)
public void saveUser(){
ApplicationContext ct = new ClassPathXmlApplicationContext("applicationContext_JDBC.xml");
UserDao dao = (UserDao) ct.getBean("userDAO");
// Users users1 = new Users(2,"JDBC", "mima");
Users users2 = new Users(null, "mima");
Users users3 = new Users("JDBC", "mima");
// dao.inserUser(users1);
dao.inserUser(users2);
dao.inserUser(users3);
}
}
该单元测试的特点:运用注释,使得编写测试更加简单,以及可以设置是否回滚。
@RunWith(SpringJUnit4ClassRunner.class)
表示该测试用例是运用junit4进行测试,也可以换成其他测试框架
@TransactionConfiguration(transactionManager="transactionManager")为可选项,该项不会影响回滚的设置。
@ContextConfiguration(locations={"../../../applicationContext.xml","../../../daoContext.xml"})
该路径的设置时相当于该单元测试所在的路径,也可以用classpath进行设置,该设置还有一个inheritLocations的属性,默认为 true,表示子类可以继承该设置。
@Autowired
表示bean自动加载,而不用像之前的两个类要添加一个set的方法。
@Test
表示该方法是测试用例
@Rollback(false)
表示该测试用例不回滚
-----------------------------------------------------------------------------------------------------------------------------------------------
package com.kkks.jdbc.dao.impl;
import javax.sql.DataSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import com.kkks.jdbc.dao.UserDao;
import com.kkks.pojo.Users;
public class UserDaoImpl implements UserDao {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void inserUser(Users users) {
NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(dataSource);
String sql = "insert into Users(ID,USERNAME,PASSWORD) values (:id,:username,:password)";
Long id = users.getId();
String username = users.getUsername();
String password = users.getPassword();
SqlParameterSource addParameterSource = new MapSqlParameterSource().addValue("id", id).addValue("username", username).addValue("password", password);
jt.update(sql, addParameterSource);
}
}
SPRING 配置文件:
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!--
PropertyPlaceholderConfigurer这个类就是读取jdbc.
properties文件,并将它们设置到这个类的属性中。然后再将下面数据源配置中定义的这些${jdbc.driver}、${jdbc.url}字符串换成属性文件中相同名称的值。
${}这种写法,是类里面方法解析用的,网上都说这是叫占位符,我看了源代码的,其实是把它们当成字符串截取前后的特殊字符,再根据里面定义的名称找属性文件中对应的值。
所以这个类只能读取properties格式的文件,你如果还有其它需要加入的属性文件,可以在list之间加入,写在value标签里面。
-->
<bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/kkks/config/jdbc.properties</value>
</property>
</bean>
<!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxWait" value="60000" />
<property name="poolPreparedStatements" value="true" />
</bean>
<bean id="userDAO" class="com.kkks.jdbc.dao.impl.UserDaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 为数据源配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
<!-- 为DAO 配置事务代理 -->
<bean id="userDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<ref local="userDAO"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
</beans>