【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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 大石退出菊丸怎么办 word空白页面突然变大了怎么办 高速上车胎爆了怎么办 没有定速巡航跑长途怎么办 惠普笔记本驱动无法安装怎么办 狙击手遇到热追踪导弹怎么办 做完卷腹脖子疼怎么办 医疗设备销售遭遇瓶颈怎么办 给顾客加油加超了怎么办 卡密码输错两次怎么办 擤鼻涕耳朵会响怎么办 鼻子里有血丝是怎么办 怀孕8周上火了怎么办 鼻炎犯了鼻涕流不停怎么办 擤鼻涕眼睛肿了怎么办 感冒咳嗽鼻子不通气怎么办 宝宝感冒不会擤鼻涕怎么办 新生儿鼻腔里有鼻涕怎么办 宝宝鼻腔有鼻涕出不来怎么办 怀孕的人感冒了怎么办 孕37周感冒咳嗽怎么办 吹鼻涕耳朵堵了怎么办 怀孕的孔雀鱼生病了怎么办 生病了咳嗽一直不好怎么办 宝宝生病治疗后咳嗽怎么办 2个月宝宝老是生病怎么办 2个月的哈士奇生病怎么办 怀孕的猫生病了怎么办 宝宝生病咳嗽啥都不吃怎么办 怀孕了感冒了怎么办啊 2个月宝宝生病了怎么办 刚刚怀孕了就生病了怎么办 一岁多宝宝总是发烧咳嗽生病怎么办 7个月宝宝生病怎么办 4个月宝宝老是生病怎么办 孕早期嗓子有痰怎么办 鼻炎早晨起床鼻涕带血怎么办 鼻子破皮了结痂怎么办 擤鼻子耳朵好像堵住了怎么办 鼻子和脸上起皮怎么办 鼻子擦鼻涕擦红怎么办