spring整合mybatis

用maven项目时。注意spring和mybatisjar之间的相互兼容性。。有时因为包间版本不支持耽误太多时间了。。。在maven仓库里可以看到jar的各版本支持性。

这事applicationContext.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"
    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-4.2.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    
    <!-- 配置mybatis支持 -->
    <!-- 加载数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    	<property name="driverClassName" value="${jdbc.driver}"/>
    	<property name="url" value="${jdbc.connect}"/>
    	<property name="username" value="${jdbc.username}"/>
    	<property name="password" value="${jdbc.password}"/>
    	<property name="maxTotal" value="10"/>
    	<property name="minIdle" value="5"/>
    </bean>
    
    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<property name="configLocation" value="classpath:mybatis/myBatisConfig.xml"/>
    	<property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 单个mapper配置 -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    	<property name="mapperInterface" value="com.samrtian.mapper.UserMapper"/>
    	<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
    
    <!-- 批量扫描包 mapper.java和mapper.xml名称要一致且在同一个包内, 扫描出来的mapper的beanID是类名首字母小写-->
<!--     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
   		<property name="basePackage" value="com.samrtian.mapper"/>
    	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean> -->

</beans>

这是mybatis配置

<?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>
	
	<settings>
<!-- 		懒加载开启 -->
		<setting name="lazyLoadingEnabled" value="true"/>
<!-- 		为真,直接查询关联数据,为假按需查询 -->
		<setting name="aggressiveLazyLoading" value="false"/>
<!-- 		开启二级缓存 -->
		<setting name="cacheEnabled" value="true"/>
	</settings>

	
	<!-- 	别名定义 ,一定要放在environments标签前面-->
	<typeAliases>
<!-- 		单个类别名定义 -->
<!-- 		<typeAlias type="com.samrtian.entity.User" alias="User"/> -->
<!-- 		批量,扫描整个包别名定义,名字就是类名 -->
<!--  		<package name="com.samrtian.entity"/>   -->
	</typeAliases>
	
 
	<mappers>
<!-- 	映射文件 -->
	<!-- 	<mapper resource="com.samrtian.mapper.UserMapper.xml"/> -->
<!-- 	代理方法映射文件 -->
<!-- 		<mapper resource="com/samrtian/mapper/UserMapper.xml"/> -->
<!-- 	通过代理接口映射文件XML和类名必须一致且在同一个目录内 -->
<!-- 		<mapper class="com.samrtian.mapper.UserMapper"/> -->
<!-- 	通过包名批量加载代理接口映射文件 -->
		<!-- <package name="com.samrtian.mapper"/> -->
	</mappers>
 
	
</configuration>

mybatis的xml和java的名字要一致且在同一个包内才能扫描通过

最后添加测试代码

OK。。最简单配置成功了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值