Spring与Mybatis整合

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


提示:以下整理了spring-mybatis整合的相关知识与配置资料

一、Spring-Mybatis整合所需要的jar包

点击下载jar包
提取码:yo4l

二、如何整合

1.配置Spring的applicationContext.xml文件

代码如下(示例):
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: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-4.0.xsd">
	
	
	<bean id="dept01" class="com.hwua.po.Dept">
		<property name="deptno" value="1"></property>
		<property name="dname" value="张三"></property>
		<property name="loc" value="海南"></property>
	</bean>
	
	<!-- 自动装配     自动扫描指定目录下注解 -->
	<context:component-scan base-package="com.hwua"></context:component-scan>
		
</beans>

2.配置Spring-Mybatis的 spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
		
	<!-- 组件扫描 -->
	<context:component-scan base-package="com.hwua"></context:component-scan>
	
	<!-- 进行数据库的配置 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
		<property name="user" value="scott"></property>
		<property name="password" value="root"></property>	
	</bean>
	
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 启用事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	
	<!-- Spring整合Mybatis -->
	<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- MyBatis的配置文件 -->
		<property name="configLocation" 
                 value="classpath:mybatis-config.xml"></property>
                 
        <!-- 配置mybatis映射文件 -->      
        <property name="mapperLocations" 
                 value="classpath:com/hwua/dao/*.xml"></property>
        <!-- 处理别名 --> 
        <property name="typeAliasesPackage" 
                 value="com.hwua.po"></property>       
	</bean>
	<!-- Mapper接口
		MapperScannerConfigurer 为指定包下的Mapper接口批量生成代理实现类.bean的默认id是接口名首字母小写. 
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.hwua.dao"></property>
	</bean>
	
</beans>		

3.测试

注意:Po类与Dao类、Dao.xml文件的创建方式与mybatis中配置的方式样,只是测试的时候getMapper()改为了getBean()。
点击跳转mybatis中的配置

@org.junit.Test
	public void findById() {
		//读取配置文件
		ApplicationContext app = 
				new ClassPathXmlApplicationContext("spring-mybatis.xml");
		//获取bean
		DeptDao dd = app.getBean("deptDao", DeptDao.class);
		//调用方法
		Dept d = dd.findById(40);
		System.out.println(d);
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值