ssh框架整合之配置文件
程序员文章站
2022-04-30 12:04:03
...
1、约束、这个就不用多说了
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
2、整合hibernate
所有的数据库连接都依赖于底层的连接,所以我们首先要配置一个连接池
这里配置c3p0连接
<context:property-placeholder location="classpath:dq.properties"/>//这里是读取外部的连接jdbc_properties配置文件
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.name}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
然后配置sessionFactory
<!--配置sessionFactory-->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!--注入连接池-->
<property name="dataSource" ref="dataSource"/>
<!--注入hibernate属性-->
<property name="hibernateProperties">
<props>
<!--方言-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<!--建表方式-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!--sql语句显示可选-->
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!--扫描包下的所有hibernate映射文件-->
<property name="mappingDirectoryLocations" value="classpath:bean"/>
</bean>
然后配置会用到的事务
事务底层是依赖于sessionFactory的
<!--事务管理器-->
<bean name="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
最后配置事务的作用范围,也就是Spring中的aop技术
<!--配置通知-->
<tx:advice id="MyAdvice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="get*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
<tx:method name="add*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="del*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="modify*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<!--常用开发两套-->
<tx:method name="query*" isolation="DEFAULT" read-only="true" propagation="REQUIRED"/>
<tx:method name="increase*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="remove*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
<tx:method name="update*" isolation="DEFAULT" read-only="false" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--将通知织入切点-->
<aop:config>
<aop:pointcut id="Mypt" expression="execution(* cn.hd.service.impl.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="MyAdvice" pointcut-ref="Mypt"/>
</aop:config>
上一篇: AMD锐龙三代暗藏实力40%:B350主板兼容成谜
下一篇: SSH框架整合配置文件