将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理。。所以该整合,只需将SQLSessionFactory的对象生成器SQLSessionFactoryBean注册到Spring容器中,再将其注入给Dao的实现类即可完成整合。

可以通过2种方式来实现Spring与MyBatis的整合:

  • Mapper动态代理
  • 支持扫描的Mapper动态代理

一、环境搭建

二、定义映射文件

 import java.util.List;

 import com.jmu.beans.Student;

 //Dao增删改查
public interface IStudentDao {
void insertStudent(Student student);
void deleteById(int id);
void updateStudent(Student student); List<String> selectAllStudentsNames();
String selectStudentNameById(int id); List<Student> selectAllStudents();
Student selectStudentById(int id);
}

IStudentDao

 <?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.jmu.dao.IStudentDao">
<insert id="insertStudent">
insert into student(name,age) values(#{name},#{age})
</insert> <delete id="deleteById">
delete from student where id=#{XXX}
</delete> <update id="updateStudent">
update student set name=#{name},age=#{age} where id=#{id}
</update> <select id="selectAllStudents" resultType="student">
select id,name,age from student
</select> <select id="selectStudentById" resultType="student">
select id,name,age from student where id=#{XXX}
</select>
</mapper>

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> <!--配置别名 -->
<typeAliases>
<package name="com.jmu.beans" />
</typeAliases> <mappers>
<!-- <mapper resource="com/jmu/dao/mapper.xml"/> -->
<!--xml文件和dao同名 -->
<package name="com.jmu.dao" /> </mappers> </configuration>

mybatis.xml

四、Mapper动态代理方式生成Dao代理对象

 <?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.xsd"> <!--注册数据源:C3P0 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 注册属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml"></property>
<property name="dataSource" ref="myDataSource"></property>
</bean> <!--生成Dao的代理对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory" />
<property name="mapperInterface" value="com.jmu.dao.IStudentDao" />
</bean> <!-- 注册Service -->
<bean id="studentService" class="com.jmu.service.StudentServiceImpl">
<property name="dao" ref="studentDao" />
</bean>
</beans>

applicationContext.xml

五、测试

 import java.util.List;

 import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jmu.beans.Student;
import com.jmu.service.IStudentService; public class MyTest { private IStudentService service; @Before
public void before() {
//创建容器对象
String resource = "applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
service = (IStudentService) ac.getBean("studentService");
} @Test
public void test01() {
Student student=new Student("张三", 23);
service.addStudent(student);
} @Test
public void test02() {
service.removeById(2);
} @Test
public void test03() {
Student student=new Student("王意义", 23);
student.setId(3);
service.modifyStudent(student);
}
@Test
public void test04() {
List<String> names = service.findAllStudentsNames();
System.out.println(names);
}
@Test
public void test05() {
String names = service.findStudentNameById(3);
System.out.println(names);
} @Test
public void test06() {
List<Student> students=service.findAllStudents();
for (Student student : students) {
System.out.println(student);
}
} @Test
public void test07() {
Student student=service.findStudentById(3);
System.out.println(student);
} }

MyTest

对于IstudentDao.xml中没有写到的List<String> selectAllStudentsNames()和String selectStudentNameById(int id)进行修改

六、支持扫描的动态代理

对于上面的情况,如果有多个Dao接口,就要写多次的以下字段

<!--生成Dao的代理对象 -->
<bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="mySqlSessionFactory" />
<property name="mapperInterface" value="com.jmu.dao.IStudentDao" />
</bean>
 <!--生成Dao的代理对象
当前配置会被本包中所有的接口生成代理对象
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
<property name="basePackage" value="com.jmu.dao" />
</bean> <!-- 注册Service -->
<bean id="studentService" class="com.jmu.service.StudentServiceImpl">
<!-- 这里的Dao的注入需要使用ref属性,且其作为接口的简单类名 -->
<property name="dao" ref="studentDao" />
</bean>

applicationContext


Spring与MyBatis整合上_Mapper动态代理方式的更多相关文章

  1. Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)

    1.原始方法开发Dao Dao接口 package cn.sm1234.dao; import java.util.List; import cn.sm1234.domain.Customer; pu ...

  2. MyBatis开发Dao层的两种方式(Mapper动态代理方式)

    MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...

  3. 七 MyBatis整合Spring,DAO开发(传统DAO&动态代理DAO)

    整合思路: 1.SQLSessionFactory对象应该放到Spring中作为单例存在 2.传统dao开发方式中,应该从Spring容器中获得SqlSession对象 3.Mapper代理行驶中,应 ...

  4. Spring+springmvc+Mybatis整合案例 xml配置版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:xml版(myeclipse) 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version ...

  5. Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    前言 本文将分析mybatis与spring整合的MapperScannerConfigurer的底层原理,之前已经分析过java中实现动态,可以使用jdk自带api和cglib第三方库生成动态代理. ...

  6. Mybatis学习(7)spring和mybatis整合

    整合思路: 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(spr ...

  7. 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理

    Spring AOP详解 . JDK动态代理.CGLib动态代理  原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...

  8. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  9. 转:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析

    原文地址:Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析 前言 本文将分析mybatis与spring整合的MapperScannerConfigur ...

随机推荐

  1. 使用Python Pandas处理亿级数据

    在数据分析领域,最热门的莫过于Python和R语言,此前有一篇文章<别老扯什么Hadoop了,你的数据根本不够大>指出:只有在超过5TB数据量的规模下,Hadoop才是一个合理的技术选择. ...

  2. SAE实践——创建新应用开启MySQL服务

    1. 创建SAE应用 当创建完成SAE账户之后,即可创建SAE应用.点击创建新应用按钮,创建一个新的SAE 应用 阅读提示信息,等待五秒,点继续创建. 填写应用信息完成创建.可选择PHP5.3和空应用 ...

  3. js的语言的理解

    1.所谓字面量,就是语言语法 2.在js编译器读到语法时候,执行时候创建对象:在赋值的时候创建一个对象,或者是一个匿名对象. 3.函数定义本身是一个对象:执行时候不产生实例对象:这跟python类不一 ...

  4. 4、TensorFlow基础(二)常用API与变量作用域

    1.图.操作和张量 TensorFlow 的计算表现为数据流图,所以 tf.Graph 类中包含一系列表示计算的操作对象(tf.Operation),以及在操作之间流动的数据 — 张量对象(tf.Te ...

  5. pycharm+gitee

    Git操作 前言: 由于各种原因,很多时候我们写代码的电脑并不会随身携带,所以有的时候突发灵感想继续写代码就变得难以实现.相信大部分同学对此都有了解,那就通过代码托管平台来管理.原本想用GitHub来 ...

  6. bzoj4361:isn(dp+容斥+树状数组)

    题面 darkbzoj 题解 \(g[i]\)表示长度为\(i\)的非降序列的个数 那么, \[ ans = \sum_{i=1}^{n}g[i]*(n-i)!-g[i+1]*(n-i-1)!*(i+ ...

  7. Compile git version inside go binary

    Compile git version inside go binary Abstract 在我们编写的程序中总是希望可以直接查阅程序的版本,通过--version参数就会输出如下版本信息. Buil ...

  8. .NET(C#):使用反射来获取枚举的名称、值和特性

    首先需要从内部了解一下枚举(Enumeration),相信许多人已经知道了,当我们声明一个这样的枚举类型: enum MyEnum { AAA, BBB, CCC } 背后的IL是这样的: .clas ...

  9. C# 委托的一些使用上的小技巧

    1.委托是一种数据类型,我们可以在任何定义类的地方定义委托,在任何声明类的地方声明委托 2.初始化委托有两种方式,代码如下: (1).像类一样初始化委托 public delegate void Sa ...

  10. 第1章—Spring之旅—容纳你的Bean

    容纳你的Bean 在基于Spring的应用中,你的应用对象生存于Spring容器中.Spring负责创建对象,装配他们,配置他们并管理他们整个生命周期,从生存到死亡(在这里 可能是new 到 fina ...