junit4 测试save 的时候 成功 但是数据库没有值

云溪七 2016-10-13 08:25:44
以下是测试用例

package test;



import java.sql.SQLException;
import java.util.Date;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.surveypark.bean.User;
import com.surveypark.service.UserService;

public class TestUserService {

private static UserService us = null;

@BeforeClass
public static void iniUserService(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
us = (UserService) ac.getBean("userService");
}
@Test
public void testInsertUser() throws SQLException{
User user = new User();
user.setEmail("123@qq.com");
user.setPassword("123456");
user.setNickName("stone");
user.setName("yunxi");
user.setRegDate(new Date());

us.saveEntity(user);

}
}


以下是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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 分散配置 -->

<context:component-scan base-package="com.surveypark.dao.impl,com.surveypark.service.impl,com.surveypark.listener,com.surveypark.struts2.action" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name = "driverClass" value = "com.mysql.jdbc.Driver"/>
<property name = "jdbcUrl" value = "jdbc:mysql://localhost:3306/surveypark"/>
<property name = "user" value = "root"/>
<property name = "password" value = ""/>

<property name = "maxPoolSize" value = "10"/>
<property name = "minPoolSize" value = "2"/>
<property name = "initialPoolSize" value = "3"/>
<property name = "acquireIncrement" value = "2"/>

</bean>
<!-- 本地会话工厂bean(Spring整合hibernate的核心入口) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/surveypark/bean</value>
</list>
</property>
</bean>
<!-- hibernate事务管理器,用来在servece层面上实现事务管理,而且达到平台无关性 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事物通知 -->

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 写操作 -->
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/>
<!-- 读操作 -->
<tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" />
</tx:attributes>
</tx:advice>





<!-- aop配置 -->
<aop:config>
<!-- pointcut表达式要仔细学习 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/>
</aop:config>
</beans>


以下是hibernate配置


<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>

</session-factory>
</hibernate-configuration>

...全文
861 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengli.zou 2017-11-25
  • 打赏
  • 举报
回复
chengli.zou 2017-11-25
  • 打赏
  • 举报
回复
引用 7 楼 chenziyu_ccut 的回复:
兄弟,我昨天上班也遇到这个问题,junit测试毫无问题,就是插入不了数据库 原因就是junit下,插入数据会自动rollback,能查不能插,所以,你的save方法应该是没问题的
可以在Junit测试方法上面加上该注解@Rollback(false)
红糖小米粥 2016-10-21
  • 打赏
  • 举报
回复
兄弟,我昨天上班也遇到这个问题,junit测试毫无问题,就是插入不了数据库 原因就是junit下,插入数据会自动rollback,能查不能插,所以,你的save方法应该是没问题的
云溪七 2016-10-20
  • 打赏
  • 举报
回复
@尼古拉斯-全蛋 @Burgess1994 谢谢各位。 问题找到了。 其实是我Serviece 层少些了一行代码。正好执行的是这句。 麻烦各位了 其实真出了这个问题。 大家确实可以安装这两位的问题进行查看。 一个有可能是事务问题 这个问题根据我浅薄的见识应该占很大一部分。 没有带过去值也是个问题。 我这个问题也可以归纳到这里。 谢谢各位
___d 2016-10-18
  • 打赏
  • 举报
回复
引用 4 楼 u013232949 的回复:
@qnmdcsdn : 我一开始也觉得是事务的问题。 但是不清楚是哪里出问题。 我这里是用hibernate+写的。 不引用spring 里的配置 根本就无法操作数据库。 手动开启,开不开。 注解写死了。
去掉注解,自己写事务,jdbc难道没写过么
云溪七 2016-10-17
  • 打赏
  • 举报
回复
@qnmdcsdn : 我一开始也觉得是事务的问题。 但是不清楚是哪里出问题。 我这里是用hibernate+写的。 不引用spring 里的配置 根本就无法操作数据库。 手动开启,开不开。 注解写死了。
云溪七 2016-10-17
  • 打赏
  • 举报
回复
@M_siri : 数据好像确实没有带过去。 可我现在是在找不到问题出在哪。 debug 的时候执行到saveEntity的时候就进入aop的线程里了、 感觉根本就没有操作dao 层。 现在不知道问题出哪了。
Burgess_Z 2016-10-14
  • 打赏
  • 举报
回复
debug一下看看有没有到controller中去 ,数据有没有带过去
  • 打赏
  • 举报
回复
事务没起作用?先手动开事务测一下

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧