zoukankan      html  css  js  c++  java
  • 【SSM 7】Mybatis底层封装思路

    一、基本概述

    在前面的博客中介绍到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的配置和测试,将在下一篇博客中介绍!



  • 相关阅读:
    ASP.NET应用界面开发新工具推荐,不要错过
    WinForm应用程序的交互界面可以创建哪些样式?
    快速创建一个.NET 5 WPF应用程序,这招一定要Get
    Blazor应用界面开发
    DevExpress v21.1帮助文档下载大全分享
    WinForms应用界面开发入门教程指南
    Kendo UI 透视网格
    WPF应用界面开发新风向
    Angular UI组件库Kendo UI for Angular扩展组件,这些功能值得了解
    响应式Web开发框架DevExtreme v21.1
  • 原文地址:https://www.cnblogs.com/hhx626/p/7534635.html
Copyright © 2011-2022 走看看