beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.yc"/>
<bean id="pphc"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:db.properties" />
</bean>
<!-- 配置dbcp数据源... 数据库联接池 ( jndi-> tomcat的数据库联接池 ) / c3p0 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
</bean>
<!--针对mybatis的整合配 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置所有的mapper文件的位置 -->
<property name="mapperLocations" value="classpath*:com/yc/dao/*.xml"/>
</bean>
<!--配置扫描器,扫描所有的mapper文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yc.dao"/>
<!--指定这个scanner所使用的sqlsessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--事务管理器jdbc -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/><!--Connection对象=dataSource。getConnection con.commit() -->
</bean>
<!--配置增强 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="retrieve*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="dagarid*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED"/>
<tx:method name="repair*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!--配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.yc.biz.impl.*.*(..))" id="service"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>
</beans>
mybatis的xml配置
<?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.yc.dao.StudentDao">
<resultMap type="com.yc.bean.Student" id="BaseResultMap">
<id column="id" property="id" jdbcType="CHAR"/>
<result column="sname" property="sname" jdbcType="VARCHAR"/>
<result column="age" property="age"/>
</resultMap>
<delete id="delete" parameterType="java.lang.String">
delete from student where id=#{id}
</delete>
<insert id="add" parameterType="com.yc.bean.Student">
insert into student(id,sname,age) values(#{id},#{sname},#{age})
</insert>
<update id="update" parameterType="com.yc.bean.Student">
update student set sname=#{sname},age=#{age} where id=#{id}
</update>
<select id="findById" resultMap="BaseResultMap" parameterType="java.lang.String">
select * from student where id=#{id}
</select>
</mapper>