標籤:style http io ar color os 使用 sp java
在Spring4中使用通用Mapper
Spring4增加了對泛型注入的支援,這個特性對通用Mapper來說,非常的有用,可以說有了這個特性,可以直接在Service中寫Mapper<UserInfo> mapper,可以通過BaseService<T>來實現通用的Service。
這篇文檔主要講解通用Mapper在Spring4中的**最佳用法**。
一、在Spring4中配置通用Mapper
和其他裡面配置的區別就是在Spring4中可以配置通用Mapper這個類,我們可以把通用Mapper中提供的Mapper<T>配置到Spring中,如果你有自己實現的通用Mapper,也可以這麼配置:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper,com.isea533.mybatis.mapperhelper"/></bean>
這裡在配置basePackage的時候,將通用Mapper<T>所在的包com.isea533.mybatis.mapperhelper也配置上了。這樣就能在Spring4中直接注入Mapper<T>。
另外就是通用Mapper自身的配置:
<bean class="com.isea533.mybatis.mapperhelper.MapperHelper" depends-on="sqlSession" init-method="initMapper" scope="singleton" lazy-init="false"> <property name="mappers"> <array> <value>com.isea533.mybatis.mapperhelper.Mapper</value> </array> </property> <property name="sqlSessions" ref="sqlSession"/></bean>
這裡的配置和Spring3沒什麼區別,另外需要保證有sqlSession,可以按如下配置:
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory"/></bean>
其他的配置按照通常的方法進行配置即可,沒有特殊的地方,如果有人不明白完整的配置什麼樣,可以看下面的例子:
Mybatis-Spring4項目
applicationContext.xml
二、繼承
Mapper<T>實現自己的實體介面類
這裡以Country2Mapper為例:
public interface Country2Mapper extends Mapper<Country2> { //省略其他自己增加的方法}
如果你點進去上面的Country2Mapper查看,會發現裡面還有一些Example的方法,這些是代碼產生器產生的,產生的方法不包含通用的CRUD,只有Example的方法,還有一個對應的Country2Mapper.xml。
這個例子主要說明,除了通用Mapper的方法外,你可以添加自己的方法,和原來的沒有區別。
這裡的實體Country2代碼如下:
@Table(name="country")public class Country2 { @Id private Integer id; private String countryname; private String countrycode; //省略getter和setter方法}
這裡配置對應的表名為country。只有一個主鍵id。
三、在Service中使用
在Service中的使用方式有很多種。
第一種、直接注入上面定義的
Country2Mapper
@Servicepublic class DemoService { @Autowired private Country2Mapper mapper; public List<Country2> selectPage(int pageNum,int pageSize){ PageHelper.startPage(pageNum, pageSize); //Spring4支援泛型注入 return mapper.select(null); }}
這種方式太常見,太普通,這裡不多解釋。
第二種、泛型注入
這種方式用的就很少了,但是Spring4支援泛型注入,因此在第一種的基礎上,我們可以寫出如下的代碼:
@Servicepublic class DemoService { @Autowired private Mapper<Country2> mapper; public List<Country2> selectPage(int pageNum,int pageSize){ //這裡用到了分頁外掛程式PageHelper PageHelper.startPage(pageNum, pageSize); //Spring4支援泛型注入 return mapper.select(null); }}
對於不瞭解泛型注入的,可能會不習慣Mapper<Country2> mapper這種寫法,實際上這麼寫的優勢並不明顯。還不如第一種明確。
但是通過第二種,我們可以引出第三種,也可能會是很常用的通用Service。
第三種、通用Service
一般操作資料庫都在Service中進行,不可避免的就要寫出大量重複的CRUD方法,如果能有一個通用的Service,肯定也會減少很多工作量。
這裡通過簡單擴充來講,更複雜的封裝,各位可以根據自己的情況動手實踐。
如下簡單例子:
@Servicepublic abstract class BaseService<T> { @Autowired protected Mapper<T> mapper; public int save(T entity){ return mapper.insert(entity); } public int delete(T entity){ return mapper.deleteByPrimaryKey(entity); } /** * 單表分頁查詢 * * @param pageNum * @param pageSize * @return */ public List<T> selectPage(int pageNum,int pageSize){ PageHelper.startPage(pageNum, pageSize); //Spring4支援泛型注入 return mapper.select(null); }}
建立如上所示的抽象類別BaseService<T>,這裡封裝三個方法僅作為簡單的例子。需要更複雜邏輯的可以自行摸索。
然後修改剛才的DemoService例子:
@Servicepublic class DemoService extends BaseService<Country2>{}
由於BaseService<T>封裝了單表的分頁外掛程式,因此目前的DemoService中沒有任何代碼。
假如我們要增加一個包含校正的儲存方法。添加如下代碼:
@Servicepublic class DemoService extends BaseService<Country2>{ public int save(Country2 country2) { if (country2 == null) { throw new NullPointerException("儲存的對象不可為空!"); } if (country2.getCountrycode() == null || country2.getCountrycode().equals("")) { throw new RuntimeException("國家代碼不可為空!"); } if (country2.getCountryname() == null || country2.getCountryname().equals("")) { throw new RuntimeException("國家名稱不可為空!"); } return super.save(country2); }}
上面只是個例子,是否拋出異常各位不用計較。
從這個例子應該也能看到,當使用Spring4和通用Mapper的時候,是多麼的方便。
關於繼承
Mapper<T>
我一開始為什麼要設計為必須繼承Mapper<T>實現自己的Mapper呢?
主要考慮到兩個方面。
通過<T>可以方便的擷取泛型的類型,在通用的方法中就不需要傳遞實體類型。
通過繼承的Mapper,例如Country2Mapper,有獨立的Mapper就意味著有獨立的命名空間,可以緩衝結果,並且不需要攔截器就能實現。
現在有了Spring4後,又有了一個很重要的原因。
最後
如果之前說通用Mapper不如Mybatis-Generator自動產生好,我也只能說看個人喜好,不需要通用Mapper的可以不用,通用Mapper只是為了滿足一部分的人需要。
現在來看,**如果還有人說通用Mapper不如Mybatis-Generator自動產生好**,我會建議他看看這篇文檔
實際上,不需要說那個更好,適合自己的才好。
另外看完這篇文檔後,不需要再說**通用Mapper不如Mybatis-Generator自動產生好**,因為我和一些朋友正在翻譯**Mybatis-Generator**,最後還會提供**Mybatis-Generator和通用Mapper的整合外掛程式**,可以用**Mybatis-Generator**直接產生實體類、繼承通用Mapper的實體Mapper以及XML檔案。
Mybatis-Generator中文文檔地址:http://generator.sturgeon.mopaas.com/
Mybatis-Generator官方英文地址:http://mybatis.github.io/generator/index.html
這個文檔還沒有翻譯完,而且譯者水平有限,如果發現翻譯錯誤或者不合適的地方,可以在下面的地址提ISSUE
提交ISSUE
上面這個地址只是產生後的項目文檔地址,並不是我們直接用來翻譯的項目。
在Spring4中使用通用Mapper