appcationContext.xml

appcationContext.xml

<beans>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
</bean>
<bean id="exampleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingResources">
<list>
<value>Test.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<ref bean="exampleHibernateProperties" />
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="baseDao" abstract="true" singleton="false">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="testDao" parent="baseDao" class="ssh.naxj.dao.TestDao" singleton="true"/>
<bean id="testBoTarget" class="ssh.naxj.bo.TestBo" singleton="true">
<property name="testDao" ref="testDao"/>
</bean>

<bean id="testBo"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="testBoTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

</beans>
第一部分 数据源

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>//驱动名称
</property>
<property name="url">
<value>jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=UTF-8</value>//数据库地址
</property>
<property name="username">
<value>root</value>//用户名
</property>
<property name="password">
<value>root</value>//密码
</property>
</bean>

第二部分 hibernate的配置

<bean id="exampleHibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>//对应所使用的数据库,我所用的是MYSQL
<prop key="hibernate.query.substitutions">true '0', false '1'</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>
</bean>

第三部分 hibernate的sessionFactory 一个关联于特定数据库的全局工厂

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />//先前定义的数据源
</property>
<property name="mappingResources">
<list>
<value>Test.hbm.xml</value>//hibernate O/R map文件
</list>
</property>
<property name="hibernateProperties">
<ref bean="exampleHibernateProperties" />//先前定义的hibernate 配置
</property>
</bean>

第四部分 事务 transactionManager

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory" />//先前定义的sessinFactory
</property>
</bean>

第五部分 Dao 抽象

<bean id="baseDao" abstract="true" singleton="false">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
定义baseDao的好处就是在以下的Dao中再不用每个bean 都要注入sessionFactory了

第六部分 Dao

<bean id="testDao" parent="baseDao" //继承于先前定义的Dao 抽象
class="ssh.naxj.dao.TestDao" singleton="true"/>

第七部分 定义 Target (BO)

<bean id="testBoTarget" class="ssh.naxj.bo.TestBo" singleton="true">
<property name="testDao" ref="testDao"/>//IOC 注入testDao
</bean>

第八部分 使用事务的BO

<bean id="testBo"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>//transactionManager
<property name="target"><ref local="testBoTarget"/></property>//Target
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>//“*”为所有的方法使用事务
</props>
</property>
</bean>

你可能感兴趣的:(DAO,xml,Hibernate,mysql,jdbc)