Person.java
package tohibernate.xml; public class Person { private long pid; private String pname; public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } }
Person.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="tohibernate.xml.Person" table="person" catalog="db"> <id name="pid" type="java.lang.Long"> <column name="pid" /> <generator class="increment" /> </id> <property name="pname" type="java.lang.String"> <column name="pname" length="20" /> </property> </class> </hibernate-mapping>
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="connection.username">root</property> <property name="connection.url">jdbc:mysql://localhost:3399/db</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.password">3333456</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="tohibernate/xml/Person.hbm.xml" /> </session-factory> </hibernate-configuration>
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: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"> <!-- 1、目标类 2、通知 3、进行aop的配置 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:tohibernate/xml/hibernate.cfg.xml</value> </property> </bean> <bean id="personDao" class="tohibernate.xml.PersonDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="personService" class="tohibernate.xml.PersonServiceImpl"> <property name="personDao"> <ref bean="personDao" /> </property> </bean> <!-- spring容器的角度出发写 --> <aop:config> <aop:pointcut expression="execution(* tohibernate.xml.PersonServiceImpl.*(..))" id="perform" /> <aop:advisor pointcut-ref="perform" advice-ref="tx" /> </aop:config> <!-- 通知 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <tx:advice id="tx" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" /> <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> </beans>
浙公网安备 33010602011771号