Spring整合MyBatis框架

11 篇文章 0 订阅

思路

MyBatis整合Spring的思路如下:

  1. SqlSessionFactory对象应该放到spring容器中作为单例存在。
  2. 传统Dao的开发方式中,应该从spring容器中获得sqlsession对象。
  3. Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
  4. 数据库的连接以及数据库连接池事务管理都交给spring容器来完成。

jar包

  1. Spring所需jar包
  2. MyBatis所需jar包
  3. MyBatis+Spring整合jar包:mybatis-spring-1.3.0.jar
  4. MySql的数据库驱动jar包
  5. 数据库连接池的jar包

步骤

  1. 创建一个java/web工程,导入jar包(上面提到的jar包)。
  2. 编写mybatis的配置文件——SqlMapConfig.xml。
  3. 编写Spring的配置文件。
    • 数据库连接及连接池
    • 事务管理(暂时可以不配置)
    • sqlsessionFactory对象,配置到spring容器中
    • mapeer代理对象或者是dao实现类配置到spring容器中
  4. 编写Dao或者mapper.xml映射文件。
  5. 测试。

1)搭建项目环境,导入jar包,db.properties,SqlMapConfig.xml与applicationContext.xml配置文件

在这里插入图片描述

2)编写SqlMapConfig.xml配置

因为数据库的连接以及数据库连接池事务管理都交给spring容器来完成。所以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>	
	
</configuration>

3)编写Spring的applicationContext.xml配置

需要在Spring容器中配置数据库连接池以及SqlSessionFactory

<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.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.xsd">
   
   	<!--指定注解扫描包路径-->
  	<context:component-scan base-package="com.oak"/>
 	
	<!-- 导入资源文件 db.properties-->
  	<context:property-placeholder location="classpath:db.properties"/>
 	 
 	<!-- 配置dbcp连接池参数使用Spring表达式来传值 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
   	destroy-method="close">
   		<property name="driverClassName" value="${driver}"></property>
    	<property name="username" value="${user}"></property>
    	<property name="password" value="${password}"></property>
    	<property name="url" value="${url}"></property>
    	<!-- 连接池启动时的初始值 -->
		<property name="initialSize" value="${initsize}" />
		<!-- 连接池的最大值 -->
		<property name="maxActive" value="${maxsize}" />
    </bean>
    
    <!-- SqlSessionFactory的配置 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置数据库连接池 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 加载mybatis的核心配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
    </bean>
</beans>

在这里插入图片描述


Dao层开发

Dao层的开发有三种实现方式:

  1. 传统Dao层的开发方式(暂且不提)。
  2. 使用mapper代理形式开发方式。
  3. 使用扫描包配置mapper代理。

Mapper代理形式开发Dao层

在com.oak.mapper包中新建接口StudentMapper,编写查询方法

public interface StudentMapper {
	/**
	 * @return
	 */
	List<Student> findAll();
}

在SqlMapConfig.xml中开启类命名配合:

<typeAliases>
	<package name="com.oak.po"/>
</typeAliases>

在同包下新建StudentMapper.xml映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oak.mapper.StudentMapper">
	<select id="findAll" resultType="student">
		select  
		student_id studentId
		,name
		,age
		,grade_id gradeId
		from `student` 
	</select>
</mapper>

在Spring容器中配置mapper代理:

<!-- 第一种方式,配置代理对象 -->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <!-- 设置代理的mapper接口,即为哪一个接口创建代理对象 -->
    <property name="mapperInterface" value="com.oak.mapper.StudentMapper"></property>
    <!-- 由于MapperFactoryBean这个类继承自SqlSessionDaoSupport类,所以要注入sqlSessionFactory这个属性 -->
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

测试:

@Test
public void testUserMapper() {
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentMapper stuMapper = ctx.getBean(StudentMapper.class);
	List<Student> stus=stuMapper.findAll();
    System.out.println(stus);
}

扫描包形式配置mapper代理

使用扫描包的形式配置mapper代理来开发Dao层,需要在application-context.xml文件中配置一个包扫描器:

<!-- 第二种方式,配置包扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 配置要扫描的包 -->
    <property name="basePackage" value="com.oak.mapper" />
</bean>

注意:

  • 该扫描器会自动去Spring容器中去找你已经初始化好后的sqlSessionFactory, 所以在此并不需要配置。

  • 如果要扫描多个包,那么使用半角逗号分隔。

  • 使用扫描包的形式配置mapper代理之后,每个mapper代理对象的id就是类名,且首字母小写。

测试上述方法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

robona

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值