SpringMVC + MyBatis整合

本文介绍了一个基于Spring3.1.1、MyBatis3.2.8和mybatis-spring1.2.3的整合案例。主要内容包括spring-resources.xml配置文件详解,展示如何实现Spring与MyBatis的整合,以及如何配置事务管理和数据库连接池等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:spring3.1.1+mybatis3.2.8+mybatis-spring1.2.3

0. project 目录

1. spring-resources.xml

这个文件是用来完成spring和mybatis的整合的xml。注意properties文件的读入方式。

<?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:p="http://www.springframework.org/schema/p"  
    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-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd" 
	default-autowire="byName" default-lazy-init="false">
	
	<!-- 自动扫描组件,需要把controller去掉,否则影响事务管理 -->  
    <context:component-scan base-package="org.com.mars">  
        <context:exclude-filter type="regex"  
            expression="org.com.mars.controller.*" />  
    </context:component-scan>

	<!-- 引入jdbc配置文件 -->  
    <context:property-placeholder location="classpath:prop/jdbc.properties" /> 

	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean> 
    
    <!-- 声明式事务管理 -->
    <aop:config>  
        <aop:advisor pointcut="execution(* org.com.mars.service..*.*(..))"  
            advice-ref="myAdvice" />  
    </aop:config>  
    <tx:advice id="myAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />  
            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />  
  
            <tx:method name="create*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="modify*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
  
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />  
        </tx:attributes>  
    </tx:advice>  
    
    <!-- 创建SqlSessionFactory,同时指定数据源 -->  
    <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath*:maper/*.map.xml" />
    </bean> 
    
    <!-- DAO接口所在包名,Spring会自动查找其下的类 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="org.com.mars.dao" />
        <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
    </bean>
    
    <!-- 可通过注解控制事务 -->  
    <tx:annotation-driven />
    
    <!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<!-- Connection Info -->
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />

		<!-- Connection Pooling Info -->
		<property name="maxActive" value="${dbcp.maxActive}" />
		<property name="maxIdle" value="${dbcp.maxIdle}" />
		<property name="defaultAutoCommit" value="false" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="3600000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="3600000" />
	</bean>
</beans>

2. mapper.xml

该xml 如何使用配置,请参看spring-resources.xml,注意mapper.xml中 namespace的配置。

<?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="org.com.mars.dao.test.UserDao">

 <!-- mapping -->
 <resultMap id="User_t" type="org.com.mars.pojo.test.User">
 <result property="userId" column="user_id" jdbcType="VARCHAR" />
 <result property="userName" column="user_name" jdbcType="VARCHAR" />
 </resultMap>

 <sql id="columns">
 user_id,
 user_name
 </sql>


 <!-- 根据ID查询用户信息 -->
 <select id="queryUserById" resultMap="User_t">
 SELECT
 <include refid="columns" />
 FROM
 user_t
 WHERE
 user_id = #{userId}
 </select>

</mapper>

springmvc + mybatis整合详细,及遇到的问题请参看以下资料:

参考资料:

http://www.itnose.net/detail/6074493.html

http://my.oschina.net/wangbiglei/blog/489583

http://my.oschina.net/wangbiglei/blog/489604


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值