mybatis 与spring整合配置

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值