sping 配置数据源、事务。

来源:互联网 发布:好老师淘宝店 编辑:程序博客网 时间:2024/05/02 01:30

1、schema


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jee="http://www.springframework.org/schema/jee"
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/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/jee
   http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">


2、数据源,基于properties文件

<context:property-placeholder location="classpath:database.properties" ignore-unresolvable="true"/>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<property name="driverClassName" value="${driverClassName}" />

<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>


3、事务

<!-- 配置事务管理器
        (理论上可配置多个transactionManager来适应多个数据源-不过不能是同一ID) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!--
对事务管理器进行事务设置。增加如下代码: 这里创建了一个advice(通知),对事务管理器进行事务设置,这里意思是指,
对于以save、del、update开头的方法应用事务。
-->
<tx:advice id="smAdvice" transaction-manager="transactionManager">
<!-- 设置事务传播属性 -->
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="remove*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="edit*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>


<!-- 下面就把事务应用到具体的类  -->
<aop:config>
<aop:pointcut id="smMethod" expression="execution(* com.hy.service.serviceImpl.*.*(..))" />
<aop:advisor pointcut-ref="smMethod" advice-ref="smAdvice" />
</aop:config>


4、基于Mybatis配置


1、映射文件

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<mapper namespace="com.mybatis.UserDao">
    <select id="countAll" resultType="int">
        select count(*) c from user;
    </select>
</mapper>


2、 mybatis配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <mappers>
        <mapper resource="com/mybatis/UserDaoMapper.xml"/>
    </mappers>
</configuration>


3、spring配置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/hlp?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"></property>
        <property name="username" value="root"></property>
        <property name="password" value="1234"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="30"></property>
        <property name="maxWait" value="500"></property>
        <property name="defaultAutoCommit" value="true"></property>
    </bean>


    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
        <property name="dataSource" ref="dataSource" />
    </bean>

简洁之道

4:使用包名<!-- Register all interfaces in a package as mappers --><mappers><package name="org.mybatis.builder"/></mappers>

0 0
原创粉丝点击