【SSM 7】Mybatis底层封装思路
来源:互联网 发布:cfca 网络身份认证平台 编辑:程序博客网 时间:2024/06/05 15:10
一、基本概述
在前面的博客中介绍到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.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="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的配置和测试,将在下一篇博客中介绍!
0 0
- 【SSM 7】Mybatis底层封装思路
- 汇总:基于SSM封装的一个迅速开发项目的底层框架集成项目
- 关于URL+method、通用参数封装的设计思路(java web,SSM框架)
- 处理问题的思路,基于SSM项目(spring,springmvc,mybatis)
- android底层学习思路
- ssm框架整合思路
- SSM整合思路
- SSM整合思路
- 整合ssm思路
- SSM框架mybatis 详解二 No.7
- 论底层封装--Memcached
- 底层方法封装
- jdbc底层封装
- hibernate底层dao封装
- 【SSM-MyBatis框架】MyBatis入门
- MyBatis 底层原理
- 【SSM】Mybatis之SqlSession
- SSM——MyBatis
- 使用itoa函数输出二进制格式
- python decode encode问题
- Linux文件编程之虚拟文件系统(VFS)
- 55. Jump Game
- oracle基础教程(第三天)Java操作数据库
- 【SSM 7】Mybatis底层封装思路
- .NET异步编程总结----四种实现模式
- adb的简单用法
- hdu5920 Ugly Problem【贪心+高精度减法】
- HDU 5919 Sequence II(主席树)
- Common.js
- 面向对象程序设计上机练习八(对象数组)
- C#之XML基础 节点.innertext,为标签添加内容
- python中self和cls