spring和hibernate的集成
生活随笔
收集整理的這篇文章主要介紹了
spring和hibernate的集成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
集成關系圖:
項目目錄樹:
?
User.java
package com.donghai.bean;public class User {private String id;private String name;private String password;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}?
UserManager.java
package com.donghai.bean;public interface UserManager {public void addUser(String name, String password) throws Exception; }?
UserManagerImpl.java
package com.donghai.bean;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class UserManagerImpl extends HibernateDaoSupport implements UserManager {@Overridepublic void addUser(String name, String password) throws Exception {User user = new User();user.setName(name);user.setPassword(password);this.getHibernateTemplate().save(user);System.out.println("UserManagerImpl.addUser()---->name: " + name + " password: " + password);}}User.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"><hibernate-mapping><class name="com.donghai.bean.User" table="tbl_user"><id name="id"><!-- <generator class="native" />--><generator class="uuid" /></id><property name="name" /><property name="password" /></class> </hibernate-mapping>ExportDB.java(數據庫導出)
package com.donghai.Client;import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB {public static void main(String[] args){Configuration cfg = new Configuration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true, true);} }Client.java
package com.donghai.Client;import org.hibernate.cfg.Configuration; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.donghai.bean.User; import com.donghai.bean.UserManager;public class Client {public static void main(String[] args){// 當struts集成的時候,就把該類也注入到spring中,就不用再new 了ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");UserManager userManager = (UserManager)factory.getBean("userManager");try {userManager.addUser("ddd", "aaa");} catch (Exception e) {e.printStackTrace();}} }?
hibernate.cfg.xml
<!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://172.16.100.10:3306/hibernate_first?useUnicode=true&characterEncoding=GBK</property><property name="hibernate.connection.username">hibernate</property><property name="hibernate.connection.password">hibernate</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.show_sql">true</property><property name="hibernate.current_session_context_class">thread</property><mapping resource="com/donghai/bean/User.hbm.xml"/></session-factory> </hibernate-configuration>applicationContext.xml
<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"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" > <bean id="userManager" class="com.donghai.bean.UserManagerImpl"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- 配置SessionFactory --><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="add*" propagation="REQUIRED"/><tx:method name="del*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="*" propagation="REQUIRED" read-only="true"/></tx:attributes></tx:advice><aop:config><aop:pointcut expression="execution(* com.donghai.bean.*.*(..))" id="allManagerMethod"/><aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/></aop:config> </beans>?
轉載于:https://www.cnblogs.com/djoker/p/6441305.html
總結
以上是生活随笔為你收集整理的spring和hibernate的集成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从CMOS到触发器(一)
- 下一篇: win10+ubuntu14.04双系统