热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

八、Mybatis整合spring之Mapper接口代理实现dao层

阅读本篇文章前建议先看看七、Mybatis整合spring之手动实现dao层环境准备我这里直接复制上篇文章的工程,然后将dao层删掉,把user.x

阅读本篇文章前建议先看看七、Mybatis整合spring之手动实现dao层


环境准备

我这里直接复制上篇文章的工程,然后将dao层删掉,把user.xml映射文件删掉,测试类删掉,删掉SqlMapConfig.xml配置中的user.xml的关联,删掉ApplicationContext.xml配置中的userDao的注入。

完成后的工程如下所示:
在这里插入图片描述


添加Mapper相关的操作

1.创建UserMapper接口类
在src目录下的根包中创建一个mapper包,定义UserMapper接口类

package blog.csdn.net.mchenys.mapper;import blog.csdn.net.mchenys.pojo.User;public interface UserMapper {public User findUserById(Integer id);
}

2.创建对应的mapper映射文件
在mapper包中创建UserMapper.xml文件,注意必须要和接口文件在同一个包下,配置内容如下:


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace&#61;"blog.csdn.net.mchenys.mapper.UserMapper"><select id&#61;"findUserById" parameterType&#61;"java.lang.Integer" resultType&#61;"blog.csdn.net.mchenys.pojo.User">select * from user where id &#61; #{id}select>
mapper>

完成后的包结构是这样的&#xff1a;
在这里插入图片描述
3.修改Mybatis配置文件

编辑SqlMapConfig.xml&#xff0c;在mappers节点开启包扫描的方式引入Mapper接口类


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration><mappers> <package name&#61;"blog.csdn.net.mchenys.mapper"/>mappers>
configuration>

修改Spring配置文件

这里有2种方式接入Mapper接口的代理实现类&#xff0c;先介绍手动注入的方式&#xff0c;编辑ApplicationContext.xml&#xff0c;在beans标签节点内添加如下内容&#xff1a;

<bean id&#61;"userMapper" class&#61;"org.mybatis.spring.mapper.MapperFactoryBean"><property name&#61;"mapperInterface" value&#61;"cn.itheima.mapper.UserMapper">property><property name&#61;"sqlSessionFactory" ref&#61;"sqlSessionFactory">property>bean>

关于org.mybatis.spring.mapper.MapperFactoryBean可以在mybatis-spring-1.2.2.jar中找到
在这里插入图片描述
完整的Spring配置文件如下&#xff1a;


<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:aop&#61;"http://www.springframework.org/schema/aop" xmlns:tx&#61;"http://www.springframework.org/schema/tx"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><context:property-placeholder location&#61;"classpath:db.properties" /><bean id&#61;"dataSource" class&#61;"org.apache.commons.dbcp.BasicDataSource"destroy-method&#61;"close"><property name&#61;"driverClassName" value&#61;"${jdbc.driver}" /><property name&#61;"url" value&#61;"${jdbc.url}" /><property name&#61;"username" value&#61;"${jdbc.username}" /><property name&#61;"password" value&#61;"${jdbc.password}" /><property name&#61;"maxActive" value&#61;"10" /><property name&#61;"maxIdle" value&#61;"5" />bean><bean id&#61;"sqlSessionFactory" class&#61;"org.mybatis.spring.SqlSessionFactoryBean"><property name&#61;"configLocation" value&#61;"classpath:SqlMapConfig.xml">property><property name&#61;"dataSource" ref&#61;"dataSource">property>bean><bean id&#61;"userMapper" class&#61;"org.mybatis.spring.mapper.MapperFactoryBean"><property name&#61;"mapperInterface" value&#61;"blog.csdn.net.mchenys.mapper.UserMapper">property><property name&#61;"sqlSessionFactory" ref&#61;"sqlSessionFactory">property>bean>beans>

测试

在测试包内创建UserMapperTest测试类

package blog.csdn.net.mchenys.test;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import blog.csdn.net.mchenys.mapper.UserMapper;
import blog.csdn.net.mchenys.pojo.User;public class UserMapperTest {private ApplicationContext applicatonContext;&#64;Beforepublic void setUp() throws Exception {applicatonContext &#61; new ClassPathXmlApplicationContext("ApplicationContext.xml");}&#64;Testpublic void testFindUserById() throws Exception{UserMapper userMapper &#61; (UserMapper)applicatonContext.getBean("userMapper");User user &#61; userMapper.findUserById(1);System.out.println(user);}
}

执行后&#xff0c;绿条
在这里插入图片描述
控制台结果如下&#xff0c;说明Mapper接口代理的方式也整合Spring也ok了。
在这里插入图片描述


优化

上面修改Spring配置文件的时候我说过还有另一种方式来注入Mapper接口的代理实现类&#xff0c;这种方式是比较推荐的&#xff0c;步骤如下&#xff1a;

1.首先删掉SqlMapConfig.xml配置文件&#xff0c;没错&#xff0c;这次可以不再需要Mybatis的配置文件了。

2.修改ApplicationContext.xml配置文件&#xff0c;将Sql会话工厂内的指定mybatis核心配置文件的那个property去掉

3.将手动注入Mapper接口代理实现的配置去掉

4.使用包扫描的方式批量引入Mapper&#xff08;重点&#xff09;&#xff0c;配置如下&#xff1a;

<bean class&#61;"org.mybatis.spring.mapper.MapperScannerConfigurer"><property name&#61;"basePackage" value&#61;"blog.csdn.net.mchenys.mapper"/>bean>

使用包扫描方式的好处就是可以避免频繁的修改Spring的配置文件&#xff0c;以后有新的Mapper接口和mapper映射文件就直接在Mapper包中添加即可&#xff0c;完整的Spring配置文件修改如下&#xff1a;


<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:aop&#61;"http://www.springframework.org/schema/aop" xmlns:tx&#61;"http://www.springframework.org/schema/tx"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><context:property-placeholder location&#61;"classpath:db.properties" /><bean id&#61;"dataSource" class&#61;"org.apache.commons.dbcp.BasicDataSource"destroy-method&#61;"close"><property name&#61;"driverClassName" value&#61;"${jdbc.driver}" /><property name&#61;"url" value&#61;"${jdbc.url}" /><property name&#61;"username" value&#61;"${jdbc.username}" /><property name&#61;"password" value&#61;"${jdbc.password}" /><property name&#61;"maxActive" value&#61;"10" /><property name&#61;"maxIdle" value&#61;"5" />bean><bean id&#61;"sqlSessionFactory" class&#61;"org.mybatis.spring.SqlSessionFactoryBean"><property name&#61;"dataSource" ref&#61;"dataSource">property>bean><bean class&#61;"org.mybatis.spring.mapper.MapperScannerConfigurer"><property name&#61;"basePackage" value&#61;"blog.csdn.net.mchenys.mapper"/>bean>
beans>

删掉Mybatis配置文件后的工程结构如下&#xff1a;
在这里插入图片描述
看起来就挺爽的&#xff0c;少了一个配置文件&#xff0c;执行测试类效果是一样的&#xff0c;这里就不啰嗦了。


推荐阅读
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了三种方法来实现在Win7系统中显示桌面的快捷方式,包括使用任务栏快速启动栏、运行命令和自己创建快捷方式的方法。具体操作步骤详细说明,并提供了保存图标的路径,方便以后使用。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
author-avatar
安乐乐520
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有