標籤:
以下為入門層級代碼,高手請務見笑~
我的MyEclipse 上spring最高版是3.* 而hibernate 最高版本是4.* 在做項目時用的都是最高版,於是代碼寫好之後調試報了個異常,居然說spring 3.*與hibernate 4.* 某個地方不相容!!推薦使用spring3.*+hibernate3.* 搭配!!於是就只能建立java項目重新貼代碼~
由於之前的hibernate 4.* 訪問mysql5.* 方言需要設成(org.hibernate.dialect.MySQLInno5DBDialect),換成hibernate3.* 之後方言也需要改成(org.hibernate.dialect.MySQLInnoDBDialect),spring中配置的bean類全名都需要進行相應的調整。
另:項目除了由myEclipse自動添加spring與hibernate引用,還需要單獨引用個jar包(hibernate-jpa-2.0-api-1.0.1.Final.jar), http://zhidao.baidu.com/share/d7e7d8f60fc44a127ba702d43e71abec.html
OK,上面己將我記的坑寫明了。現在開始貼代碼了。
我的項目是參考IBM官網上的Demo改改調調。IBM網頁:https://www.ibm.com/developerworks/cn/java/wa-spring2/
我的項目就是拿IBM上面在本地重新搭建,部分配置手工寫寫,寫上自己的理解。
本地工程結構
那些java 基本都是直接拿來用,重點是設定檔的配置。
設定檔分兩種,*.hbm.xml與*.xml *.hbm.xml為資料表對應檔;*.xml為spring 的bean配置核心。
*.hbm.xml就不說了,它是hibernate的基礎.多對一,多的一方配置處理了屬性另再加個標籤<many-to-one>。一的一方,配置處理了屬性另再加個標籤<set>
來看看我的 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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx"> <!-- definition data source --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql:///test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- definition sessionFactory object --> <bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> <prop key="hibernate.query.substitutions">true ‘T‘, false ‘F‘</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.c3p0.minPoolSize">5</prop> <prop key="hibernate.c3p0.maxPoolSize">20</prop> <prop key="hibernate.c3p0.timeout">600</prop> <prop key="hibernate.c3p0.max_statement">50</prop> <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop> </props> </property> <property name="mappingResources"> <list> <value>jackicalHibernate/Customer.hbm.xml</value> <value>jackicalHibernate/Account.hbm.xml</value> </list> </property> </bean> <!-- definition transaction manager object --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactoryBean" /> </bean> <!-- after this setting is Bean setting by author:jackical Datetime:2015-05-13 --> <!-- Pass the session factory to our UserDAO --> <bean id="customerDAOTarget" class="jackicalHibernate.CustomerDAOImpl"> <property name="sessionFactory"><ref local="sessionFactoryBean"/></property> </bean> <bean id="userDAO" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref local="transactionManager"/></property> <property name="target"><ref local="customerDAOTarget"/></property> <property name="transactionAttributes"> <props> <prop key="addCustomer">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
分別來看看:
dataSource:定義資料訪問基礎資訊(你看成資料庫連結資訊)。
sessionFactoryBean:hibernate開啟前需要先申明sessionFactory 對像。
transactionManager:使用申明的sessionFactory 對像,開啟交易管理員。
customerDAOTarget:使用Bean申明一個impl對象(資料操作執行個體)
userDAO:spring中對hibernate的事務的操作是使用aop來做的。見:http://uule.iteye.com/blog/893890
參考資料:
IBM一手資料 https://www.ibm.com/developerworks/cn/java/wa-spring2/
缺失jar包下載 http://zhidao.baidu.com/share/d7e7d8f60fc44a127ba702d43e71abec.html
事務介紹 http://uule.iteye.com/blog/893890
MyEclipse 上使用sping+hibernate+mysql