一、基本概述
在前面的博客中介紹到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>
聲明模板之后,就可以調用模板里面的方法。它包含的方法有:
2.2,通用mapper插件
事實上,上面的方法其實還是需要開發的時候,做一些改動或者修改的。不過,還有一種比上面使用sqlsessiontemplate模板類更為簡單的方法,就是直接引入通用mapper插件,這個插件引入之后,通過使用具體的mapper去繼承,就擁有了很多符合我們開發習慣的方法。(PS:sqlsessiontemplate的參數類型,不是那么的符合日常開發習慣,至少是我個人,比如我要添加一條數據,那我希望的調用方式是Test.insert(Tb tb)型的)
這個通用mapper的配置和測試,將在下一篇博客中介紹!