<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config> 我在daoimpl类中分别用两种方法第一种:(dao中注入了sessionFactoryUtil 管理session也可以手动管理trancastion不过比较麻烦老是要在类中写代码):public class LoginDaoImpl implements ILoginDao {
private SessionFactoryUtil sessionFactoryUtil;
public TUser getTUser(String username, String password) { return null;
} public void saveTUser(TUser user) {
sessionFactoryUtil.getSession().save(user);
sessionFactoryUtil.endTransaction(true);
}
public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
this.sessionFactoryUtil = sessionFactoryUtil;
}
}另外的是把//sessionFactoryUtil.endTransaction(true);注释掉了 public class LoginDaoImpl implements ILoginDao {
private SessionFactoryUtil sessionFactoryUtil;
public TUser getTUser(String username, String password) { return null;
} public void saveTUser(TUser user) {
sessionFactoryUtil.getSession().save(user);
//sessionFactoryUtil.endTransaction(true);注释掉了就不执行insert语句
}
public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
this.sessionFactoryUtil = sessionFactoryUtil;
}
}其中的sessionFactoryUtil 为:public class SessionFactoryUtil {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
session = sessionFactory.openSession();
transaction = session.beginTransaction();
} public void endTransaction(boolean commite) {
if (commite) {
transaction.commit();
} else {
transaction.rollback();
}
session.close();
} public Session getSession() {
return session;
} public Transaction getTransaction() {
return transaction;
} public SessionFactory getSessionFactory() {
return sessionFactory;
}
}第二种 不执行insert sql语句 为啥spring的aop事务不起作用,该怎么配置?
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">注意倒数第2句:“http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
”
配置文件的声明部份和你给的一样
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
这句话是说事物将织入到哪些方法里,
你写的表达式是说事物将织入到org.wx.service.impl包下的任何类中的任何方法中
LZ的LoginDaoImpl 类在org.wx.service.impl下么?
LoginDaoImpl在org.wx.dao.impl下面
我的包结构是:
org.wx.action
org.wx.dao
org.wx.dao.impl
org.wx.service
org.wx.service.impl
org.wx.bean
LZ把这句改成
<aop:pointcut id="allManagerMethod" expression="execution(* org.wx.dao.impl.*.*(..))" />
这样再试试
sessionFactoryUtil.getSession().save(user);
sessionFactoryUtil.endTransaction(true);
}改用spring的aop事务管理 把sessionFactoryUtil.endTransaction(true);注释掉后就不能保存了
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod" expression="execution(* org.wx.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
</aop:config> <bean id="sessionFactoryUtil" class="org.wx.dao.util.SessionFactoryUtil">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="loginDaoImpl" class="org.wx.dao.impl.LoginDaoImpl">
<property name="sessionFactoryUtil">
<ref bean="sessionFactoryUtil"/>
</property>
</bean>
<bean id="loginServiceImpl" class="org.wx.service.impl.LoginServiceImpl">
<property name="loginDaoImpl">
<ref bean="loginDaoImpl" />
</property>
</bean>
<bean id="loginAction" class="org.wx.action.LoginAction" scope="prototype">
<property name="loginServiceImpl">
<ref bean="loginServiceImpl"/>
</property>
</bean>
</beans>hibernate.cfg.xml<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>
<property name="connection.username">bbs_web</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="myeclipse.connection.profile">
oracleDriver
</property>
<property name="connection.password">wwwwww</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="show_sql">true</property>
<mapping resource="org/wx/bean/Banji.hbm.xml" />
<mapping resource="org/wx/bean/Student.hbm.xml" />
<mapping resource="org/wx/bean/TUser.hbm.xml" />
</session-factory></hibernate-configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<constant name="struts.action.extension" value="do,action"/>
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<package name="struts2" extends="struts-default">
<action name="login" class="loginAction" method="execute">
<result name="toList" type="redirect">/userList.jsp</result>
<result name="fail" >/login.jsp</result>
<result name="input" >/login.jsp</result>
</action>
</package>
</struts>jsp界面<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<metahttp-equiv="pragma" content="no-cache">
<metahttp-equiv="cache-control" content="no-cache">
<metahttp-equiv="expires" content="0">
</head>
<body>
<s:form name="form1" action="/login.action">
<s:textfield name="username" label="username"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror/>
</body>
</html>action类package org.wx.action;import java.util.Iterator;import org.wx.bean.TUser;
import org.wx.service.ILoginService;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport {
private ILoginService loginServiceImpl;
public String execute() throws Exception {
System.out.println("---------execute-------");
if (username != null && password != null) {
TUser tuser = new TUser();
tuser.setPassword(getPassword());
tuser.setUsername(getUsername());
loginServiceImpl.saveTUser(tuser);
}
return "toList";
}
public String userList(){
Iterator<TUser> iterator = loginServiceImpl.getAllUsers();
ActionContext.getContext().put("userList", iterator);
return "list";
}
private String username;
private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public void validate() {
username = getUsername();
password = getPassword();
if (password == null || password.length() == 0) {
super.addActionError("密码不能为空!");
}
if (username == null||username.length() == 0 ) {
super.addActionError("用户名不能为空!");
}
} public void setLoginServiceImpl(ILoginService loginServiceImpl) {
this.loginServiceImpl = loginServiceImpl;
}}service实现类package org.wx.service.impl;import java.util.Iterator;import org.wx.bean.TUser;
import org.wx.dao.ILoginDao;
import org.wx.service.ILoginService;public class LoginServiceImpl implements ILoginService { private ILoginDao loginDaoImpl ;
public void setLoginDaoImpl(ILoginDao loginDaoImpl) {
this.loginDaoImpl = loginDaoImpl;
}
public TUser getTUser(String username,String password) {
return null;
}
public void deleteTuser(TUser user) {
}
public void saveTUser(TUser user) {
System.out.println("---------LoginServiceImpl saveTuser-------");
try {
loginDaoImpl.saveTUser(user);
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateTuser(TUser user) {
}
public Iterator<TUser> getAllUsers() {
return loginDaoImpl.getAllUsers();
}
public TUser getUserByID(Integer userid) {
return null;
}}dao实现类package org.wx.dao.impl;import java.util.Iterator;import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.wx.bean.TUser;
import org.wx.dao.ILoginDao;
import org.wx.dao.util.HibernateBase;
import org.wx.dao.util.SessionFactoryUtil;public class LoginDaoImpl implements ILoginDao {
private SessionFactoryUtil sessionFactoryUtil;
public TUser getTUser(String username, String password) { return null;
} public void saveTUser(TUser user) throws Exception{
System.out.println("---------LoginDaoImplsaveTUser-------");
sessionFactoryUtil.getSession().save(user);
//sessionFactoryUtil.endTransaction(true);把这里注释掉就不执行保存操作了为什么?
} public void beginTransaction() {
} public void endTransaction() {
} public void setSessionFactoryUtil(SessionFactoryUtil sessionFactoryUtil) {
this.sessionFactoryUtil = sessionFactoryUtil;
} public Iterator<TUser> getAllUsers() {
Query query = sessionFactoryUtil.getSession().createQuery("select u from TUser u");
Iterator<TUser> iterator =query.iterate();
return iterator;
} public TUser getUserByID(Integer userid) {
// TODO Auto-generated method stub
return null;
}
}sessionFactoryUtil类package org.wx.dao.util;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;public class SessionFactoryUtil {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
this.session = sessionFactory.openSession();
this.transaction = session.beginTransaction();
} public void endTransaction(boolean commite) {
if (commite) {
transaction.commit();
} else {
transaction.rollback();
}
session.close();
} public Session getSession() {
return session;
} public Transaction getTransaction() {
return transaction;
} public SessionFactory getSessionFactory() {
return sessionFactory;
}
}
配置成 dao.impl.*.*(..);可以嘛? 还是要具体到具体类的上一级目录呢?