将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. 教你玩转XSS漏洞

    什么是存储性XSS那? 通俗理解就是”xss“语句存在服务器上,可以一直被客户端浏览使用,所有登陆某一个存在”存储性xss“的页面的人,都会中招,可以是管理员,可以是普通的用户,所以他的危害是持续性的 ...

  2. blueborne漏洞的联想

    本文作者:ice 0X00前言 昨天看到blueborne的漏洞,顺手给我的nexus6装了一个app,测试了一下,一脸懵逼,怎么修复啊,然后我联想了一下, 还有哪些协议和传输是我们身边的威胁了,于是 ...

  3. mxonline实战3,编写首页及用户登录页面1

          对应github地址:首页和用户登陆1     一. 显示首页   1. 修改mxonline/setttings.py 在TEMPLATES代码块修改DIRS为 'DIRS': [os. ...

  4. 逻辑卷磁盘管理和dd命令

      底层PV 中层VG 上层LV   PE(phsical extent):在逻辑层次上,VG把PV分成固定大小的块,这些块就叫PE,默认为4M ,创建LV的过程就是分多少个PE的过程. 自动分区的过 ...

  5. linux系统解决boot空间不足

    有时候更新Linux系统是会碰到boot空间不足的错误,原因基本上是安装时boot空间设置问题可以通过删除旧的内核来释放boot空间. ubuntu: 1.查看当前使用内核版本号       unam ...

  6. 【3】JMicro微服务-服务超时,重试,重试间隔

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 接下来的内容都基于[2]JMicro微服务-Hello World做Demo 微服务中,超时和重试是一个最基本问题下面Dem ...

  7. rbac表设计

  8. Bomb(要49)--数位dp

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submi ...

  9. 解决TeamViewer无法按给定网络地址联系伙伴

    说明:这种现象一般是断网后DNS改变了,或者路由重启了没有重启网络配合导致的. 解决方法: 1.Windows: ipconfig /flushdns 2.Linux: /etc/rc.d/init. ...

  10. win7 免安装MariaDB

    1.  官网下载MariaDB的windows版本 地址:https://downloads.mariadb.org/mariadb/10.0.17/ 目前最新版本是10.0.17 2.  下载完成后 ...