Spring-Hibernate

来源:互联网 发布:java第三方登录接口 编辑:程序博客网 时间:2024/06/05 19:14

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" 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.5.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-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/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   <!--    sessionFactory      *  具备sessionFactoryImpl的功能      *  具备spring的DI的功能   实现sessionFactory有两中方法      *  <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="dataSource" ref="myDataSource"/>    <property name="mappingResources">      <list>        <value>product.hbm.xml</value>      </list>    </property>    <property name="hibernateProperties">      <value>        hibernate.dialect=org.hibernate.dialect.HSQLDialect      </value>    </property>  </bean>     *  可以把hibernate.cfg.xml配置文件直接导入进来     -->   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   <property name="configLocation">   <value>classpath:hibernate.cfg.xml</value>   </property>   </bean>    <!--     程序员做的事情     -->    <bean id="personDao" class="cn.itcast.spring0909.hibernate.transaction.xml.PersonDaoImpl">    <property name="sessionFactory">    <ref bean="sessionFactory"/>        </property>    </bean>        <bean id="personService" class="cn.itcast.spring0909.hibernate.transaction.xml.PersonServiceImpl">    <property name="personDao">    <ref bean="personDao"/>    </property>    </bean>    <!--     spring容器做的事情     -->     <aop:config>     <aop:pointcut expression="execution(* cn.itcast.spring0909.hibernate.transaction.xml.PersonServiceImpl.*(..))" id="perform"/>     <aop:advisor advice-ref="tx" pointcut-ref="perform"/>     </aop:config>    <!--     事务管理器     -->     <bean id="transactionmanager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     <property name="sessionFactory">     <ref bean="sessionFactory"/>     </property>     </bean>    <tx:advice transaction-manager="transactionmanager" id="tx">    <tx:attributes>    <tx:method name="save*" read-only="false"/>    </tx:attributes>    </tx:advice> </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.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate0909</property><!-- username--><property name="connection.username">root</property><!-- password--><property name="connection.password">  </property><property name="hbm2ddl.auto">update</property><property name="show_sql">true</property><mappingresource="cn/itcast/spring0909/hibernate/transaction/xml/Person.hbm.xml" /></session-factory></hibernate-configuration>

public class Person implements Serializable{private Long pid;private String pname;private String psex;public String getPsex() {return psex;}public void setPsex(String psex) {this.psex = psex;}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="cn.itcast.spring0909.hibernate.transaction.xml.Person" table="person" catalog="hibernate0909">        <id name="pid" type="long">            <column name="pid" />            <generator class="increment" />        </id>        <property name="pname" type="string">            <column name="pname"/>        </property>        <property name="psex" type="string">            <column name="psex" length="5" />        </property>    </class></hibernate-mapping>

public interface PersonDao {public void savePerson(Person person);}

public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao{@Overridepublic void savePerson(Person person) {this.getHibernateTemplate().save(person);}}

public interface PersonService {public void savePerson(Person person);}

public class PersonServiceImpl implements PersonService{private PersonDao personDao;public PersonDao getPersonDao() {return personDao;}public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}@Overridepublic void savePerson(Person person) {this.personDao.savePerson(person);}}

public class PersonTest extends SpringHelper{static{path = "cn/itcast/spring0909/hibernate/transaction/xml/applicationContext.xml";}@Testpublic void test(){PersonService personService = (PersonService)context.getBean("personService");Person person = new Person();person.setPname("干露露");person.setPsex("aaa");personService.savePerson(person);}}
注解的方式提交事务

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" 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.5.xsd           http://www.springframework.org/schema/aop            http://www.springframework.org/schema/aop/spring-aop-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/tx            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">   <property name="sessionFactory">   <ref bean="sessionFactory"/>   </property>   </bean>   <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>   <context:component-scan base-package="cn.itcast.spring0909.hibernate.transaction.annotation"></context:component-scan>   <tx:annotation-driven transaction-manager="transactionManager"/> </beans>

@Repository("personDao")public class PersonDaoImpl  implements PersonDao{@Resource(name="hibernateTemplate")private HibernateTemplate hibernateTemplate;@Overridepublic void savePerson(Person person) {// TODO Auto-generated method stubthis.hibernateTemplate.save(person);}}

@Service("personService")public class PersonServiceImpl implements PersonService{@Resource(name="personDao")private PersonDao personDao;@Transactional(readOnly=false)public void savePerson(Person person) {// TODO Auto-generated method stubthis.personDao.savePerson(person);}}



0 0
原创粉丝点击