spring配置mybatis自动扫描*mapper.java和*mapper.xml配置文件

默认mybatis需要在配置文件中加载每个mapper.xml,例如:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- <properties resource="db.properties" /> -->
	<settings>
		<!-- 打开延迟加载的开关 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 将积极加载修改为消极加载,即延迟加载 -->
		<setting name="aggressiveLazyLoading" value="false" />
		<!-- 开启二级缓存 -->
		<setting name="cacheEnabled" value="true" />
	</settings>
	<typeAliases>
		<package name="cn.study.mybatis.po" />
	</typeAliases>

	<!-- 和Spring整合后environment配置都会被干掉 -->
	<environments default="development"> 
		<environment id="development"> 
			<!-- 使用jdbc事务管理,目前由mybatis来管理  -->
			<transactionManager type="JDBC" /> 
			<!--数据库连接池,目前由mybatis来管理 -->
			<dataSource type="POOLED"> 
			<property name="driver" value="${jdbc.driver}"/> 
			<property name="url" value="${jdbc.url}" /> 
			<property name="username" value="${jdbc.username}"/> 
			<property name="password" value="${jdbc.password}"/> 
			</dataSource> </environment> 
	</environments>
	<mappers>
		<mapper resource="cn/study/mybatis/mapper/UserMapper.xml" />
		批量加载mapper 和spring整合后,使用mapper扫描器,这里不需要配置了 
		<package name="cn.study.mybatis.dao" />
	</mappers>

</configuration>
如果有多个mapper.xml则需要写N个繁琐且代码冗杂,所以与spring整合后用下面的方式会方面很多:

<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
	<!-- myBatis文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
		<property name="mapperLocations" value="classpath*:com/gbanker/golddashi/**/mapper/*.xml" />
                <!-- 配置实体映射别名在*.xml中直接使用类名即可 -->
<property name="typeAliasesPackage" value="com.study.ssm.**.po" /><property name="configLocation" value="classpath:config/MybatisConf.xml"></property></bean><!-- 扫包 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.study.ssm.**.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /></bean></beans>


大家如果觉得有哪里不对欢迎指出,谢谢。http://blog.csdn.net/y534560449

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值