Mybatis通用Mapper和分頁pagehelper
極其方便的使用Mybatis單表的增刪改查
優點?
不客氣的說,使用這個通用Mapper甚至能改變你對Mybatis單表基礎操作不方便的想法,使用它你能簡單的使用單表的增刪改查,包含動態的增刪改查.
程式使用攔截器實現具體的執行Sql,完全使用原生的Mybatis進行操作.
你還在因為資料庫表變動重新生成xml嗎?還是要手動修改自動生成的insert|update|delete的xml呢?趕緊使用通用Mapper,表的變動只需要實體類保持一致,不用管基礎的xml,你不止會擁有更多的時間陪老婆|孩子|女朋友|打DOTA,你也不用做哪些繁瑣無聊的事情,感興趣了嗎?繼續看如何使用吧!!相信這個通用的Mapper會讓你更方便的使用Mybatis,這是一個強大的Mapper!!!
不管你信不信,這個專案的測試程式碼中沒有一個Mapper的xml配置檔案,但是卻可以做到每個Mapper對應上百行xml才能完成的許多功能.沒有了這些基礎xml資訊的干擾,你將會擁有清晰乾淨的Mapper.xml.
發現BUG可以提Issue,可以給我發郵件,可以加我QQ,可以進Mybatis群討論.
作者QQ: 120807756
Mybatis工具群: 211286137 (Mybatis相關工具外掛等等)
如何使用?
下面是通用Mapper的配置方法,還會提到Spring中的配置方法.還有和PageHelper分頁外掛整合的配置方式.
1. 引入通用Mapper的程式碼
將本專案中的4個程式碼檔案(EntityHelper
,Mapper
,MapperHelper
,MapperInterceptor
)複製到你自己的專案中.
專案依賴於JPA的註解,需要引入persistence-api-1.0.jar
或者新增Maven依賴:
<dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependency>
2. 配置Mapper攔截器
在mybatis-config.xml
中新增如下配置:
<plugins> <plugin interceptor="com.github.abel533.mapper.MapperInterceptor"> <!--================================================--> <!--可配置引數說明(一般無需修改)--> <!--================================================--> <!--UUID生成策略--> <!--配置UUID生成策略需要使用OGNL表示式--> <!--預設值32位長度:@java.util.UUID@randomUUID().toString().replace("-", "")--> <!--<property name="UUID" value="@java.util.UUID@randomUUID().toString()"/>--> <!--主鍵自增回寫方法,預設值CALL IDENTITY(),適應於大多數資料庫--> <!--<property name="IDENTITY" value="CALL IDENTITY()"/>--> <!--主鍵自增回寫方法執行順序,預設AFTER,可選值為(BEFORE|AFTER)--> <!--<property name="ORDER" value="AFTER"/>--> </plugin> </plugins>
可配置引數一般情況下不需要修改,直接像下面這樣一行即可:
<plugin interceptor="com.github.abel533.mapper.MapperInterceptor"></plugin>
附:Spring配置相關
如果你使用Spring的方式來配置該攔截器,你可以像下面這樣:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> <property name="typeAliasesPackage" value="com.isea533.mybatis.model"/> <property name="plugins"> <array> <-- 主要看這裡 --> <bean class="com.isea533.mybatis.mapperhelper.MapperInterceptor"/> </array> </property> </bean>
只需要像上面這樣配置一個bean即可.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations"> <array> <value>classpath:mapper/*.xml</value> </array> </property> <property name="typeAliasesPackage" value="com.isea533.mybatis.model"/> <property name="plugins"> <array> <bean class="com.isea533.mybatis.pagehelper.PageHelper"> <property name="properties"> <value> dialect=hsqldb reasonable=true </value> </property> </bean> <bean class="com.isea533.mybatis.mapperhelper.MapperInterceptor"/> </array> </property> </bean>
一定要注意PageHelper
和MapperInterceptor
這兩者的順序不能顛倒.
如果你想配置MapperInterceptor
的引數,可以像PageHelper
中的properties
引數這樣配置
3. 繼承通用的Mapper<T>
,必須指定泛型<T>
例如下面的例子:
public interface UserInfoMapper extends Mapper<UserInfo> { //其他必須手寫的介面... }
一旦繼承了Mapper<T>
,繼承的Mapper
就擁有了以下通用的方法:
//根據實體類不為null的欄位進行查詢,條件全部使用=號and條件 List<T> select(T record); //根據實體類不為null的欄位查詢總數,條件全部使用=號and條件 int selectCount(T record); //根據主鍵進行查詢,必須保證結果唯一 //單個欄位做主鍵時,可以直接寫主鍵的值 //聯合主鍵時,key可以是實體類,也可以是Map T selectByPrimaryKey(Object key); //插入一條資料 //支援Oracle序列,UUID,類似Mysql的INDENTITY自動增長(自動回寫) //優先使用傳入的引數值,引數值空時,才會使用序列、UUID,自動增長 int insert(T record); //插入一條資料,只插入不為null的欄位,不會影響有預設值的欄位 //支援Oracle序列,UUID,類似Mysql的INDENTITY自動增長(自動回寫) //優先使用傳入的引數值,引數值空時,才會使用序列、UUID,自動增長 int insertSelective(T record); //根據實體類中欄位不為null的條件進行刪除,條件全部使用=號and條件 int delete(T key); //通過主鍵進行刪除,這裡最多隻會刪除一條資料 //單個欄位做主鍵時,可以直接寫主鍵的值 //聯合主鍵時,key可以是實體類,也可以是Map int deleteByPrimaryKey(Object key); //根據主鍵進行更新,這裡最多隻會更新一條資料 //引數為實體類 int updateByPrimaryKey(T record); //根據主鍵進行更新 //只會更新不是null的資料 int updateByPrimaryKeySelective(T record);
4. 泛型(實體類)<T>
的型別必須符合要求
實體類按照如下規則和資料庫表進行轉換,註解全部是JPA中的註解:
-
表名預設使用類名,駝峰轉下劃線,如
UserInfo
預設對應的表名為user_info
. -
表名可以使用
@Table(name = "tableName")
進行指定,對不符合第一條預設規則的可以通過這種方式指定表名. -
欄位預設和
@Column
一樣,都會作為表字段,表字段預設為Java物件的Field
名字駝峰轉下劃線形式. -
可以使用
@Column(name = "fieldName")
指定不符合第3條規則的欄位名 -
使用
@Transient
註解可以忽略欄位,新增該註解的欄位不會作為表字段使用. -
建議一定是有一個
@Id
註解作為主鍵的欄位,可以有多個@Id
註解的欄位作為聯合主鍵. -
預設情況下,實體類中如果不存在包含
@Id
註解的欄位,所有的欄位都會作為主鍵欄位進行使用(這種效率極低). -
實體類可以繼承使用,可以參考測試程式碼中的
com.github.abel533.model.UserLogin2
類. -
由於基本型別,如int作為實體類欄位時會有預設值0,而且無法消除,所以實體類中建議不要使用基本型別.
除了上面提到的這些,Mapper還提供了序列(支援Oracle)、UUID(任意資料庫,欄位長度32)、主鍵自增(類似Mysql,Hsqldb)三種方式,其中序列和UUID可以配置多個,主鍵自增只能配置一個。
這三種方式不能同時使用,同時存在時按照 序列>UUID>主鍵自增
的優先順序進行選擇.下面是具體配置方法:
-
使用序列可以新增如下的註解:
//可以用於數字型別,字串型別(需資料庫支援自動轉型)的欄位 @SequenceGenerator(name="Any",sequenceName="seq_userid") @Id private Integer id;
-
使用UUID時:
//可以用於任意字串型別長度超過32位的欄位 @GeneratedValue(generator = "UUID") private String countryname;
-
使用主鍵自增:
//不限於@Id註解的欄位,但是一個實體類中只能存在一個(繼承關係中也只能存在一個) @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id;
5. 將繼承的Mapper介面新增到Mybatis配置中
例如本專案測試中的配置:
<mappers> <mapper class="com.github.abel533.mapper.CountryMapper" /> <mapper class="com.github.abel533.mapper.Country2Mapper" /> <mapper class="com.github.abel533.mapper.CountryTMapper" /> <mapper class="com.github.abel533.mapper.CountryUMapper" /> <mapper class="com.github.abel533.mapper.CountryIMapper" /> <mapper class="com.github.abel533.mapper.UserInfoMapper" /> <mapper class="com.github.abel533.mapper.UserLoginMapper" /> <mapper class="com.github.abel533.mapper.UserLogin2Mapper" /> </mappers>
附:Spring配置相關
如果你在Spring中配置Mapper介面,不需要像上面這樣一個個配置,只需要有下面的這個掃描Mapper介面的這個配置即可:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.isea533.mybatis.mapper"/> </bean>
6. 程式碼中使用
例如下面這個簡單的例子:
SqlSession sqlSession = MybatisHelper.getSqlSession(); try { //獲取Mapper UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class); UserInfo userInfo = new UserInfo(); userInfo.setUsername("abel533"); userInfo.setPassword("123456"); userInfo.setUsertype("2"); userInfo.setEmail("[email protected]"); //新增一條資料 Assert.assertEquals(1, mapper.insert(userInfo)); //ID回寫,不為空 Assert.assertNotNull(userInfo.getId()); //6是當前的ID Assert.assertEquals(6, (int)userInfo.getId()); //通過主鍵刪除新增的資料 Assert.assertEquals(1,mapper.deleteByPrimaryKey(userInfo)); } finally { sqlSession.close(); }
另一個例子:
SqlSession sqlSession = MybatisHelper.getSqlSession(); try { //獲取Mapper CountryMapper mapper = sqlSession.getMapper(CountryMapper.class); //查詢總數 Assert.assertEquals(183, mapper.selectCount(new Country())); //查詢100 Country country = mapper.selectByPrimaryKey(100); //根據主鍵刪除 Assert.assertEquals(1, mapper.deleteByPrimaryKey(100)); //查詢總數 Assert.assertEquals(182, mapper.selectCount(new Country())); //插入 Assert.assertEquals(1, mapper.insert(country)); } finally { sqlSession.close(); }
附:Spring使用相關
直接在需要的地方注入Mapper繼承的介面即可,和一般情況下的使用沒有區別.
關於和Spring結合的例子,可以看下面的地址:
當前版本v0.1.0
這是一個新生的開源專案
首先感謝您能看到這裡!
這是一個新生的專案,一切都剛剛開始,雖然專案中包含大量的測試,但是仍然會有很多未知的bug存在,希望各位能夠在使用過程中發現問題時及時反饋,歡迎各位fork本專案進行參與!