一、基本概述

在前面的博客中介绍到Mybatis的逆向生成工具,为我们生成了每个实体的基本增删改查的代码,那么每个实体都是那么多的代码,我们很容易的发现,有很大的相似性。对于这部分代码,应该予以抽象封装。

首先,看一下思路,在用工程生成代码的时候 ,我们发现,有一个Mapper.xml文件,在这里面有具体的sql语句,其实,就相当于,我们的DAO底层实现,而还有一个mapper的接口类,这个就相当于是DAO的接口。在mapper.xml和mapper接口类中,每个实体都有很大的相似度,所以,抽象封装的思路是:在mapper.xml中,定义其基本的公用方法实现,在mapper接口类中,定义基本的操作接口。然后,在用每个具体的实体mapper,去实现自身特别的需求。

先看一下简易思路图(PS:本应该用UML图表示的,唉)

二、具体实现

在刚开始的时候,我想着自己从头开始,一步一步的封装,但是在查资料的过程中,发现其实Mybatis的基本底层封装,别人都已经做好了。我们都知道Hibernate有一个HibernateTemplate模板类提供了基础的数据库操作方法,事实上,在Mybatis中,也存在这么一个模板类sqlsessiontemplate,在这个类里面,同样为我们封装了基本的操作方法。

2.1,sqlsessiontemplate

<span style="font-family:KaiTi_GB2312;font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://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.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 数据库连接池 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:resource/*.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="10" />
<property name="minIdle" value="5" />
</bean>
<!-- 让spring管理sqlsessionfactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean> <strong><span style="color:#ff0000;"> <!-- 定义SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean> </span></strong> <!-- 配置扫描包,加载mapper代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Angel.mapper" />
</bean>
</beans></span>

然后,在dao层的使用:

<span style="font-family:KaiTi_GB2312;font-size:18px;">private SqlSessionTemplate sqlSessionTemplate;</span>

声明模板之后,就可以调用模板里面的方法。它包含的方法有:

请查看文档:http://www.boyunjian.com/javadoc/org.mybatis/mybatis-spring/1.2.2/_/org/mybatis/spring/SqlSessionTemplate.html

2.2,通用mapper插件

事实上,上面的方法其实还是需要开发的时候,做一些改动或者修改的。不过,还有一种比上面使用sqlsessiontemplate模板类更为简单的方法,就是直接引入通用mapper插件,这个插件引入之后,通过使用具体的mapper去继承,就拥有了很多符合我们开发习惯的方法。(PS:sqlsessiontemplate的参数类型,不是那么的符合日常开发习惯,至少是我个人,比如我要添加一条数据,那我希望的调用方式是Test.insert(Tb
tb)型的)
这个通用mapper的配置和测试,将在下一篇博客中介绍!

【SSM 7】Mybatis底层封装思路的更多相关文章

  1. mybatis底层源码分析之--配置文件读取和解析

    现在企业级开发中ssm是很常见的技术标配,mybatis比hibernate轻量了很多,而且学习成本相对较低,简单易上手. 那么,问题来了,简单好用的mybatis底层到底是如何实现的呢?都使用了什么 ...

  2. 老调重弹:对kvo的封装思路

    关于kvo,kvo能做什么? kvo作为cocoa框架的重要特性之一,在底层框架中被大量使用.在特定的场合使用该特性往往能够带来难以想象的好处,让整个方案变得相当简洁和优雅.比如大名鼎鼎的下拉刷新的s ...

  3. Java基础-SSM之mybatis快速入门篇

    Java基础-SSM之mybatis快速入门篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 其实你可能会问什么是SSM,简单的说就是spring mvc + Spring + m ...

  4. SpringBoot集成MyBatis底层原理及简易实现

    MyBatis是可以说是目前最主流的Spring持久层框架了,本文主要探讨SpringBoot集成MyBatis的底层原理.完整代码可移步Github. 如何使用MyBatis 一般情况下,我们在Sp ...

  5. C#利用Emit反射实现AOP,以及平台化框架封装思路

    C#利用Emit反射实现AOP,以及平台化框架封装思路 这是前两天扒的一段动态代理AOP代码,用的Emit反射生成子类来实现代理模式,在这里做个小笔记,然后讨论一下AOP框架的实现思路. 首先是主函数 ...

  6. AFNetworking封装思路简析

    http://blog.csdn.net/qq_34101611/article/details/51698473 一.AFNetworking的发展 1. AFN 1.0版本 AFN 的基础部分是 ...

  7. ASP.NET底层封装HttpModule实例---FormsAuthentication类的分析

    HttpModule是用来注册HttpApplication事件的,实现IHttpModule接口的托管代码模块可以访问该请求管道的所有事件.那么对于我们最常用的ASP.NET Forms身份验证模块 ...

  8. Java基础-SSM之mybatis的统计函数和分页查询

    Java基础-SSM之mybatis的统计函数和分页查询 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  9. Java基础-SSM之mybatis一对一关联

    Java基础-SSM之mybatis一对一关联 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.准备测试环境(创建数据库表)  1>.创建husbands和wifes表并建 ...

随机推荐

  1. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

  2. tcpdf最新版 6.2版

    tcpdf6.2版,地址记 录 http://download.csdn.net/detail/hayywcy/9547873

  3. X86 Socket 通信

    struct txd_socket_handler_t { int fd; }; txd_socket_handler_t *txd_tcp_socket_create() { txd_socket_ ...

  4. C 语言中的优先级

    先看一段代码: /********************************************************************* * @fn bdAddr2Str * * ...

  5. Chapter 4: Troubleshoot and debug web applications

    Prevent and troubleshoot runtime issues Troubleshooting performance, security and errors using perfo ...

  6. Ext GridPanel

    Extjs GridPanel用法详解 创建GridPanel 要使用GridPanel,首先要定义Store,而在创建Store的时候必须要有Model,因此我们首先来定义Model: //1.定义 ...

  7. 044. asp.net主题之三应用或禁用主题和动态加载主题

    1.为单个页面指定主题可以将@Page指令的Theme或StyleSheetTheme属性设置为要使用的主题名称, 代码如下: <%@ Page Theme ="MyTheme&quo ...

  8. treeGrid树形数据表格的json数据格式说明

    在使用easyUI 的treeGrid的时候,很多时候我们从数据库取出来的数据treeGrid却不能读取显示成一个树:如下 { menuCode: "a00", menuName: ...

  9. javascript 利用匿名函数对象给你异步回调方法传参数

    先来创建一个匿名函数对象: /*** * 匿名函数 */ var callChangeBtn=new function(bugBtn){ this.chage=function(json){ bugB ...

  10. mybatisGenerator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap--转

    转自:http://blog.csdn.net/tan3739/article/details/7555665 Exception in thread "main" Java.la ...