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
标签:
查看更多关于MyEclipse 上使用sping+hibernate+mysql的详细内容...