先说下出现的问题:只能查询而不能删除或者插入数据???难道得配置事务管理,应该怎么写呢?
测试类:import java.util.Iterator;
import java.util.List;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;public class test { /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
CustomerDAO dao=(CustomerDAO) factory.getBean("CustomerDAO");
Customer c=new Customer();
c.setName("000000000000");
dao.save(c);
}
}DAO类:import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
* Data access object (DAO) for domain model class Customer.
* @see .Customer
* @author MyEclipse Persistence Tools
*/public class CustomerDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomerDAO.class);
//property constants
public static final String NAME = "name";
public static final String PASSWORD = "password"; protected void initDao() {
//do nothing
}
public void save(Customer transientInstance)
{
// Transaction Tx=this.getSession().beginTransaction();
log.debug("saving Customer instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
// Tx.commit();
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
.....
public static CustomerDAO getFromApplicationContext(ApplicationContext ctx)
{
return (CustomerDAO) ctx.getBean("CustomerDAO");
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="CustomerDAO" class="CustomerDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" ></ref>
</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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="myeclipse.connection.profile">test</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">tea110</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="Customer.hbm.xml" /></session-factory>
</hibernate-configuration>
Customer.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Customer" table="customer" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="45" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" length="19" />
</property>
<property name="registeredTime" type="java.util.Date">
<column name="REGISTERED_TIME" length="19" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="45" />
</property>
</class>
</hibernate-mapping>
测试类:import java.util.Iterator;
import java.util.List;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;public class test { /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext factory=new FileSystemXmlApplicationContext("src/applicationContext.xml");
CustomerDAO dao=(CustomerDAO) factory.getBean("CustomerDAO");
Customer c=new Customer();
c.setName("000000000000");
dao.save(c);
}
}DAO类:import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/**
* Data access object (DAO) for domain model class Customer.
* @see .Customer
* @author MyEclipse Persistence Tools
*/public class CustomerDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomerDAO.class);
//property constants
public static final String NAME = "name";
public static final String PASSWORD = "password"; protected void initDao() {
//do nothing
}
public void save(Customer transientInstance)
{
// Transaction Tx=this.getSession().beginTransaction();
log.debug("saving Customer instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
// Tx.commit();
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
.....
public static CustomerDAO getFromApplicationContext(ApplicationContext ctx)
{
return (CustomerDAO) ctx.getBean("CustomerDAO");
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="CustomerDAO" class="CustomerDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" ></ref>
</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"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="myeclipse.connection.profile">test</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">tea110</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="Customer.hbm.xml" /></session-factory>
</hibernate-configuration>
Customer.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Customer" table="customer" catalog="test">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="45" />
</property>
<property name="birthday" type="java.util.Date">
<column name="BIRTHDAY" length="19" />
</property>
<property name="registeredTime" type="java.util.Date">
<column name="REGISTERED_TIME" length="19" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="45" />
</property>
</class>
</hibernate-mapping>
打个断点 看看dao
事务配置如下:
<!-- 注册事物管理器,并自动注入sessionFactory -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
autowire="byType" /> <!-- 定义通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="delete*" />
<tx:method name="update*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice> <!-- 定义切入点,引入事物通知 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* *(..))" />
</aop:config>
出现错误:<tx:advice>这个标签有误