Spring整合mybaties

Spring整合mybaties主要是将mybaties的接口用spring配置文件进行管理,通过spring的配置文件将mybaties的配置文件和数据源进行整合

需要的配置文件有:数据源文件jdbc.properties,mybaties的配置文件spring-mybaties.xml,Spring的配置文件Spring-context.xml

jdbc.properties如下:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testu?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
jdbc.initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570  
jdbc.maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2  
jdbc.maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
jdbc.minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
jdbc.maxWait=60000

mybaties配置文件:spring-mybaties.xml

mybaties文件只需要扫描映射文件即可

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	<!-- 自动扫描 -->
	<context:component-scan base-package="com.weiguozhui.dao" />
</beans>

Spring的配置因为需要引入jdbc数据源配置文件,并创建数据源对象和sqlsessionFactory的代理类和事务策略。

Spring-context.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">

<context:component-scan base-package="com.weiguozhui">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:jdbc.properties"/>
    </bean>
    
	<!-- 数据源 --> 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接大小  -->
		<property name="initialSize" value="${jdbc.initialSize}"></property>
		<!-- 连接池最大数量  -->
		<property name="maxActive" value="${jdbc.maxActive}"></property>
		<!-- 连接池最大空闲  -->
		<property name="maxIdle" value="${jdbc.maxIdle}"></property>
		<!-- 连接池最小空闲  -->
		<property name="minIdle" value="${jdbc.minIdle}"></property>
		<!-- 获取连接最大等待时间  -->
		<property name="maxWait" value="${jdbc.maxWait}"></property>
	</bean>
	
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 -->
		<property name="mapperLocations" value="classpath:com/weiguozhui/dao/*.xml"></property>
	</bean>
	 
	<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.weiguozhui.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
   <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx  -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
    <!-- 2.扫描的包路径,这里不扫描被@Controller注解的类 
    使用<context:component-scan/> 可以不在配置<context:annotation-config/> 
    
    -->
    <import resource="classpath:spring-mybatis.xml" />
    
    
</beans>

================================================================================================记录一下下!!!!以防忘记。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值