applicationContext.xml代码如下:
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
"
default-autowire="byName">
<mvc:annotation-driven />
<mvc:interceptors>
<ref bean="chartsetInterceptor" />
</mvc:interceptors>
<!-- 开启注解处理器 -->
<context:annotation-config />
<!-- 注解需要扫描的包 -->
<context:component-scan base-package="com.blrise" />
<!-- 扫描配置文件 -->
<context:property-placeholder location="classpath:*.properties" />
<!-- 定义使用C3P0连接池的数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driver}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
<property name="user" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<!-- hibernate配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/blrise/entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider
</prop>
<prop key="hibernate.generate_statistics">false</prop>
<!-- <prop key="hibernate.connection.release_mode">after_transaction</prop> -->
</props>
</property>
</bean>
<!-- 事务管理器配置,hibernate单数据源事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
</beans>
Dao层:
package com.blrise.daoImpl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.blrise.dao.IGenericDAO;
@Transactional
public class GenericDAOHibernate<T> implements IGenericDAO<T> {
private Class<T> clz;
private String entityName;
@Autowired
private SessionFactory sessionFactory;
/**
* Constructor
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public GenericDAOHibernate() {
Class c = getClass();
Type t = (Type) c.getGenericSuperclass();
if (t instanceof ParameterizedType ) {
Type[] p = ((ParameterizedType)t).getActualTypeArguments();
clz = (Class<T>) p[0];
entityName = clz.getSimpleName();
}
}
@Override
@Transactional()
public T save(T model) {
sessionFactory.openSession().save(model);
return model;
}
@Override
public T update(T model) {
sessionFactory.openSession().update(model);
return model;
}
@Override
public void delete(T model) {
sessionFactory.openSession().delete(model);
}
@SuppressWarnings("unchecked")
@Override
public List<T> list() {
Query query = sessionFactory.openSession().createQuery("from " + entityName);
List<T> result = query.list();
return result;
}
@SuppressWarnings("unchecked")
@Override
public T load(Serializable id) {
T model = (T) sessionFactory.openSession().load(clz, id);
return model;
}
@Override
public T findByFullName(String fullName) {
Query query = sessionFactory.openSession().createQuery("from " + entityName + " where fullName = '" + fullName + "'" );
return (T) query.uniqueResult();
}
}
managerDao:
package com.blrise.daoImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.blrise.dao.IManagerDao;
import com.blrise.entity.Manager;
@Repository
@Transactional
public class ManagerDaoImpl extends GenericDAOHibernate<Manager> implements IManagerDao {
}
ServiceDao:
package com.blrise.serviseImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.blrise.dao.IManagerDao;
import com.blrise.entity.Manager;
import com.blrise.servise.IManagerServise;
@Service("ManagerServiseImpl")
@Transactional(readOnly=false)
public class ManagerServiseImpl implements IManagerServise {
@Autowired
private IManagerDao managerDao;
@Override
public List<Manager> findAll() {
return managerDao.list();
}
@Override
@Transactional
public void save(Manager manager) {
managerDao.save(manager);
}
}
Control层:
package com.blrise.test;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.blrise.entity.Manager;
import com.blrise.servise.IManagerServise;
@Controller
@RequestMapping("/test")
public class Test {
private static Logger logger = Logger.getLogger(Test.class);
@Autowired
private IManagerServise managerServise;
@RequestMapping(method=RequestMethod.GET)
public ModelAndView test(Model model){
List<Manager> managers =managerServise.findAll();
Manager manager = new Manager();
manager.setName("中国");
managerServise.save(manager);
if (logger.isDebugEnabled()) {
logger.debug("Managers = " + manager);
}
model.addAttribute("manager",manager);
return new ModelAndView("Test.jsp");
}
}
后台能够输出查询语句,但是save的语句没有。大家帮帮忙。跪谢!~~~
11 个解决方案
#1
谁能过来看看嘛???
#2
后台没有报错信息吗?如果有报错信息,就贴上来看看!
#3
后台没有报错信息,很奇怪吧
#4
已经解决。。。谢谢大家。哎,有问题还是要靠自己啊。
#5
楼主,这个问题怎么解决的,小弟这边儿也遇到这样类似的问题,事务不起作用啦
#6
我估计是没有执行到你的save方法吧。
#7
很久没上这个,你的问题解决吗?如果没有解决可以联系我
#8
鄙视楼主这种,问题解决不写出来的!
#9
解决了,不写出来,装什么装啊
#10
如此墨迹。直接说不行吗
#11
serlvet-context.xml下
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.ht.*.*.action" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
搜索@Controller标注的类只搜索action包:多个包逗号隔开
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.ht.*.*.action" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
搜索@Controller标注的类只搜索action包:多个包逗号隔开
#1
谁能过来看看嘛???
#2
后台没有报错信息吗?如果有报错信息,就贴上来看看!
#3
后台没有报错信息,很奇怪吧
#4
已经解决。。。谢谢大家。哎,有问题还是要靠自己啊。
#5
楼主,这个问题怎么解决的,小弟这边儿也遇到这样类似的问题,事务不起作用啦
#6
我估计是没有执行到你的save方法吧。
#7
很久没上这个,你的问题解决吗?如果没有解决可以联系我
#8
鄙视楼主这种,问题解决不写出来的!
#9
解决了,不写出来,装什么装啊
#10
如此墨迹。直接说不行吗
#11
serlvet-context.xml下
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.ht.*.*.action" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
搜索@Controller标注的类只搜索action包:多个包逗号隔开
<!-- 自动搜索@Controller标注的类 -->
<context:component-scan base-package="com.ht.*.*.action" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
搜索@Controller标注的类只搜索action包:多个包逗号隔开