spring和mybatis整合以后怎么测试
1个回答
展开全部
<context:component-scan base-package="com"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- sessionFactory 配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mapperLocations" value="classpath:com/zhikun/bean/basexml/*.xml"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>
<bean id="baseDao" class="com.zhikun.dao.base.BaseDaoImpl2">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<tx:annotation-driven />
mybatis.xml
<configuration>
basedao
public class BaseDaoImpl2 implements IBaseDao {
public SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int save(T t) {
return sqlSessionTemplate.insert("com.zhikun.bean.Person.save", t);
}
Test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestDao extends AbstractJUnit4SpringContextTests {
@Test
public void save(){
PersonDao persondao = new PersonDao();
Person p = new Person();
p.setName("zhouhang");
p.setNickname("周航");
p.setSeqce(123L);
p.setBirthday(new Date());
//sqlSessionTemplate.insert("com.zhikun.bean.Person.save", p);
persondao.save(p);
}
}
}
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<!-- sessionFactory 配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mapperLocations" value="classpath:com/zhikun/bean/basexml/*.xml"/>
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" />
</bean>
<bean id="baseDao" class="com.zhikun.dao.base.BaseDaoImpl2">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property>
</bean>
<tx:annotation-driven />
mybatis.xml
<configuration>
basedao
public class BaseDaoImpl2 implements IBaseDao {
public SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
@Override
public int save(T t) {
return sqlSessionTemplate.insert("com.zhikun.bean.Person.save", t);
}
Test类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class TestDao extends AbstractJUnit4SpringContextTests {
@Test
public void save(){
PersonDao persondao = new PersonDao();
Person p = new Person();
p.setName("zhouhang");
p.setNickname("周航");
p.setSeqce(123L);
p.setBirthday(new Date());
//sqlSessionTemplate.insert("com.zhikun.bean.Person.save", p);
persondao.save(p);
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询