spring整合mybatis配置

定义一个spring-mybatis.xml文件,首先在xml文件中加入需要用到的格式规范,如:

<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"
	xsi:schemaLocation="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">

1.配置数据库相关参数properties的属性(即在jdbc.properties文件中定义的内容)

<context:property-placeholder location="classpath:jdbc.properties"/>

2.配置数据库连接池

	<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 配置连接池属性 -->
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.master.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		
		<!-- c3p0连接池的私有属性 -->
		<property name="maxPoolSize" value="30" />
		<property name="minPoolSize" value="10" />
		<property name="initialPoolSize" value="10"/> 
		
		<!-- 关闭连接后不自动commit -->
		<property name="autoCommitOnClose" value="false" />
		
		<!-- 获取连接超时时间 -->
		<property name="checkoutTimeout" value="10000" />
		
		<!-- 当获取连接失败重试次数 -->
		<property name="acquireRetryAttempts" value="2" />
	</bean>

3.配置SqlSessionFactory对象,创建SqlSession工厂

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 扫描entity包 使用别名 -->
		<property name="typeAliasesPackage" value="com.xxx.xxx" />
		<!-- 扫描sql配置文件:mapper需要的xml文件 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml" />
	</bean>

4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 注入sqlSessionFactory -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	<!-- 给出需要扫描Dao接口包 -->
	<property name="basePackage" value="com.xxx.xxx" />
</bean>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值