使用hibernate4 + spring3 mvc,事务不提交是怎么回事

 我来答
千锋教育
2016-12-17 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
一、配置如下:
1)applicationContext-common.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>

<!-- 加载jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置数据源dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<property name="minEvictableIdleTimeMillis" value="18000000" />
<property name="testOnBorrow" value="true" />
</bean>

<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.jdbc.fatch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>WEB-INF/classes/pojo/*.hbm.xml</value>
</list>
</property>
</bean>

<!-- 扫描service目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.service" />

<!-- 声明式事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<!-- 配置事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- 注册com.base.HibernateDAO -->
<bean class="com.base.HibernateDAO" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

2、spring-servlet.xml
Java代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>

<!-- 扫描action目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.action" />

<!-- <context:annotation-config /> -->

<!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/view/" p:suffix=".jsp" />

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />

<!-- 自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个实例 -->
<mvc:annotation-driven />

<mvc:interceptors>

<!--openSessionInViewInterceptor -->
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="authInterceptor" class="com.security.AuthInterceptor" />
</mvc:interceptor>

<mvc:interceptor>
<mvc:mapping path="/*" />
<bean id="timerInterceptor" class="com.security.TimerInterceptor" />
</mvc:interceptor>

</mvc:interceptors>

</beans>

二、相关代码如下:

1)action中的删除操作

Java代码
/**
* 删除记录
*
* @param id
*/
@RequestMapping(value = "log_delete")
public String delete(HttpServletRequest request, Long id) {

if (id != null) {

Eq.println("============^_^=============执行删除操作,id=" + id);
logService.delete(id);
Eq.println("============^_^=============操作执行结束=======");
}

Eq.println("**********地址来源=" + Eq.getReferer(request));

return "redirect:log_list";

}

2)service中的删除操作

Java代码 收藏代码
/**
* 根据主键删除实体
*/
@Override
public void delete(Long id) {

dao.delete(Log.class, id);
//dao.flush();

}

3)dao中的删除操作

Java代码 收藏代码
public boolean delete(final Class<?> clazz, final Long id) {

Assert.notNull(id, "id不能为空");

try {

getSession().delete(getSession().load(clazz, id));

logger.debug("delete entity {},id is {}", clazz.getSimpleName(), id);

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

三、启动tomcat时,,log4j打印出来的信息:
org.springframework.beans.factory.support.DefaultListableBeanFactory@66e47d: defining beans [testAction,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.view.InternalResourceViewResolver#0,multipartResolver,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.handler.MappedInterceptor#1,org.springframework.web.servlet.handler.MappedInterceptor#2,org.springframework.web.servlet.handler.MappedInterceptor#3,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@7abaab

2012-02-17 15:25:49 [ Thread-5:6340 ] - [ INFO ] Mapped "{[/log_list],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mod.action.TestAction.list(java.lang.Integer,org.springframework.ui.ModelMap) throws java.lang.Exception
2012-02-17 15:25:49 [ Thread-5:6340 ] - [ INFO ] Mapped "{[/log_store],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mod.action.TestAction.store(pojo.Log,org.springframework.validation.BindingResult)
2012-02-17 15:25:49 [ Thread-5:6340 ] - [ INFO ] Mapped "{[/log_delete],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mod.action.TestAction.delete(javax.servlet.http.HttpServletRequest,java.lang.Long)
2012-02-17 15:25:49 [ Thread-5:6340 ] - [ INFO ] Mapped "{[/log_edit],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mod.action.TestAction.edit(java.lang.Long,org.springframework.ui.ModelMap)
2012-02-17 15:25:49 [ Thread-5:6341 ] - [ INFO ] Mapped "{[/log_detail],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String mod.action.TestAction.detail(java.lang.Long,org.springframework.ui.ModelMap)
2012-02-17 15:25:49 [ Thread-5:6998 ] - [ INFO ] FrameworkServlet 'spring': initialization completed in 1597 ms

问题:插入数据没问题,就是修改和删除记录时,,看myeclipse打印的sql语句,,检索了数据,但没有提交更新和删除sql语句
service层需要分别配置了:

1)applicationContext.xml

Java代码
<!-- 扫描service目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.service" />

2)spring-serlevt.xml

Java代码
<!-- 扫描action目录,把作了注解的类转换为bean -->
<context:component-scan base-package="mod.action" />
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式