TKmybatis的框架介紹及使用方法


最近項目使用了SpringBoot+TKMytis框架,期間遇到一些問題,順便記一下。

一、框架配置

配置的話非常簡單,我用的是SpringBoot,直接引入:


   
   
  
  
          
  1. <dependency>
  2. <groupId>tk.mybatis </groupId>
  3. <artifactId>mapper-spring-boot-starter </artifactId>
  4. <version>2.0.3-beta1 </version>
  5. </dependency>
  6. <dependency>
  7. <groupId>tk.mybatis </groupId>
  8. <artifactId>mapper </artifactId>
  9. <version>4.0.0 </version>
  10. </dependency>

Mybatis的以及分頁插件等就不寫了。

創建一個BaseMapper


   
   
  
  
          
  1. public interface BaseMapper<T> extends tk.mybatis.mapper.common.BaseMapper<T>, MySqlMapper<T>, IdsMapper<T>, ConditionMapper<T>,ExampleMapper<T> {
  2. }

這5個Mapper待會我會詳細講解。

創建BaseService<T>繼承自BaseMapper<T>


   
   
  
  
          
  1. public interface BaseService<T> extends BaseMapper<T> {
  2. }

以及BaseService的實現類BaseServiceImpl<T> implements BaseService<T>


   
   
  
  
          
  1. public abstract class BaseServiceImpl<T> implements BaseService<T> {
  2. }

Service里需實現部分方法,詳細代碼在后邊。

這樣我們就基本完成了配置。

二、類配置方法

1、實體類

創建一個實體類與數據庫進行映射,此時我們使用JPA的注解:


   
   
  
  
          
  1. package com .capol .entity;
  2. import java .sql .Timestamp;
  3. import javax .persistence .Column;
  4. import javax .persistence .Id;
  5. import javax .persistence .Table;
  6. import javax .persistence .Transient;
  7. import com .capol .base .BaseEntity;
  8. import lombok .Data;
  9. import lombok .EqualsAndHashCode;
  10. /**
  11. * @author lizihao
  12. * @version 2018年07月31日
  13. * 用戶角色
  14. */
  15. @ Data
  16. @EqualsAndHashCode(callSuper=false)
  17. @Table(name= "t_sys_user_role")//設置數據庫中表名字
  18. public class UserRole extends BaseEntity{
  19. /**
  20. * 主鍵
  21. */
  22. @ Column( name = "f_id")
  23. @Id
  24. private String fId;
  25. /**
  26. * 用戶ID
  27. */
  28. @ Column( name = "f_user_id")
  29. private String fUserId;
  30. /**
  31. * 用戶名
  32. */
  33. @ Transient
  34. private String fUserName;
  35. }

其中@Table即數據表表名,@Column即列名,@Id作為主鍵,需要注意,@Id注解不可有多個,@Transient即冗余字段,不與數據庫任何字段對應。

分享一個小技巧,實際項目中我們可能存在多數據源的情況,如果使用的是sqlserver,且多個數據庫均在同一台服務器下且配置的賬號均擁有權限,

則@Table注解中可以寫成“{數據庫名}.{架構名}.{表名}”,如:@Table(name="db.dbo.tableName")

而不需要再額外配置數據源

2、Service類

這里主要是實現了上邊BaseMapper中繼承的5個Mapper的方法,

tk.mybatis.mapper.common.BaseMapper<T>中有較多方法,均需要繼承實現:


   
   
  
  
          
  1. /**
  2. * 保存一個實體,null屬性也會保存
  3. *
  4. * @param record
  5. * @return
  6. */
  7. int insert(T record);
  8. /**
  9. * 保存一個實體,null屬性不會保存
  10. *
  11. * @param record
  12. * @return
  13. */
  14. int insertSelective(T record);
  15. /**
  16. * 根據實體屬性作為條件進行刪除,查詢條件使用等號
  17. */
  18. int delete(T record);
  19. /**
  20. * 根據主鍵更新屬性不為null的值
  21. */
  22. int updateByPrimaryKeySelective(T record);
  23. /**
  24. * 根據實體中的屬性值進行查詢,查詢條件使用等號
  25. */
  26. List<T> select(T record);
  27. /**
  28. * 查詢全部結果,select(null)方法能達到同樣的效果
  29. */
  30. List<T> selectAll();
  31. /**
  32. * 根據實體中的屬性進行查詢,只能有一個返回值,有多個結果是拋出異常,查詢條件使用等號
  33. */
  34. T selectOne(T record);
  35. /**
  36. * 根據實體中的屬性查詢總數,查詢條件使用等號
  37. */
  38. int selectCount(T record);

以上所有方法的查詢條件均為實體類record中的非空屬性。

MySqlMapper<T>中的方法如下:


   
   
  
  
          
  1. /**
  2. * 批量插入,支持批量插入的數據庫可以使用,例如MySQL,H2等,另外該接口限制實體包含`id`屬性並且必須為自增列
  3. */
  4. public int insertList(List<T> recordList);
  5. /**
  6. * 插入數據,限制為實體包含`id`屬性並且必須為自增列,實體配置的主鍵策略無效
  7. */
  8. public int insertUseGeneratedKeys(T record);

這兩個方法就比較坑了,限制了主鍵必須為自增列,如果是自己生成主鍵則不能使用該方法。

IdsMapper<T>中的方法如下:


   
   
  
  
          
  1. /**
  2. * 根據主鍵@Id進行查詢,多個Id以逗號,分割
  3. * @param id
  4. * @return
  5. */
  6. List<T> selectByIds(String ids);
  7. /**
  8. * 根據主鍵@Id進行刪除,多個Id以逗號,分割
  9. * @param id
  10. * @return
  11. */
  12. int deleteByIds(String ids);

這兩個方法就很好理解了,不再解釋。

ConditionMapper<T>中的方法如下:


   
   
  
  
          
  1. /**
  2. * 根據Condition條件進行查詢
  3. */
  4. public List<T> selectByCondition(Object condition);
  5. /**
  6. * 根據Condition條件進行查詢
  7. */
  8. public int selectCountByCondition(Object condition);
  9. /**
  10. * 根據Condition條件刪除數據,返回刪除的條數
  11. */
  12. public int deleteByCondition(Object condition);
  13. /**
  14. * 根據Condition條件更新實體`record`包含的全部屬性,null值會被更新,返回更新的條數
  15. */
  16. public int updateByCondition(T record, Object condition);
  17. /**
  18. * 根據Condition條件更新實體`record`包含的全部屬性,null值會被更新,返回更新的條數
  19. */
  20. public int updateByConditionSelective(T record, Object condition);

傳入的Object condition應為tk.mybatis.mapper.entity.Condition,具體使用方法后續會說明。

ExampleMapper<T>中的方法如下:


   
   
  
  
          
  1. /**
  2. * 根據Example條件進行查詢
  3. */
  4. public List<T> selectByExample(Object example);
  5. /**
  6. * 根據Example條件進行查詢,若有多條數據則拋出異常
  7. */
  8. public T selectOneByExample(Object example);
  9. /**
  10. * 根據Example條件進行查詢總數
  11. */
  12. public int selectCountByExample(Object example);
  13. /**
  14. * 根據Example條件刪除數據,返回刪除的條數
  15. */
  16. public int deleteByExample(Object example);
  17. /**
  18. * 根據Example條件更新實體`record`包含的全部屬性,null值會被更新,返回更新的條數
  19. */
  20. public int updateByExample(T record, Object example);
  21. /**
  22. * 根據Example條件更新實體`record`包含的不是null的屬性值,返回更新的條數
  23. */
  24. public int updateByExampleSelective(T record, Object example);

同上,傳入的Object example應為tk.mybatis.mapper.entity.Example,具體使用方法后續會說明。

3、實現類

各個方法的實現大同小異,此處以一個為例:


   
   
  
  
          
  1. public abstract class BaseServiceImpl<T> implements BaseService<T> {
  2. protected abstract BaseMapper<T> getMapper();
  3. @Override
  4. public int insert(T record) {
  5. return getMapper().insert(record);
  6. }
  7. }

getMapper()方法需要在具體的業務代碼中實現,其余不再贅述。

三、使用方法

1、tk.mybatis.mapper.common.BaseMapper<T>, IdsMapper<T>, MySqlMapper<T>內方法使用說明:

從接口中我們可以看到傳入的方法基本均為T record,即實體類,查詢時會根據實體類中的屬性值進行where語句構建,查詢條件為等號,這里沒有什么特殊的。

不過需要注意,若傳入實例化的實體類,且其中包含int屬性,則構建sql語句中會將該屬性包含進去,如下代碼:


   
   
  
  
          
  1. @Data
  2. @EqualsAndHashCode(callSuper= false)
  3. @Table(name= "t_sys_user_role") //設置數據庫中表名字
  4. public class UserRole extends BaseEntity{
  5. /**
  6. * 主鍵
  7. */
  8. @Column(name = "f_id")
  9. @Id
  10. private String fId;
  11. /**
  12. * 類型(1.系統管理員)
  13. */
  14. @Column(name = "f_type")
  15. private int fType;
  16. }
  17. @RunWith(SpringJUnit4ClassRunner.class)
  18. @SpringBootTest(classes=StartApp.class)
  19. @WebAppConfiguration
  20. public class TestService {
  21. @Autowired
  22. private IUserRoleService userRoleService;
  23. @Test
  24. public void testUserRole() throws Exception{
  25. UserRole userRole = new UserRole();
  26. List<UserRole> userRoleList = userRoleService.select(userRole);
  27. System.out.println(userRoleList);
  28. }
  29. }

從日志中我們可以看到:


   
   
  
  
          
  1. 2018 -08 -12 17: 31: 10.768 DEBUG 12172 --- [ main] com.capol.mapper.UserRoleMapper. select : ==> Preparing: SELECT f_id,f_user_id,f_type,f_status,f_description,f_creator_id,f_create_time,f_last_updator_id,f_last_update_time FROM t_sys_user_role WHERE f_type = ?
  2. 2018 -08 -12 17: 31: 10.776 DEBUG 12172 --- [ main] com.capol.mapper.UserRoleMapper. select : ==> Parameters: 0( Integer)
  3. 2018 -08 -12 17: 31: 10.787 DEBUG 12172 --- [ main] com.capol.mapper.UserRoleMapper. select : <== Total: 0

很明顯,這不是我們要的結果。將int類型改為Integer類型即可,或使用Condition、Example方法進行查詢。

2、ExampleMapper<T>內方法使用說明:

所有方法均需要傳入tk.mybatis.mapper.entity.Example,

首先進行實例化:


   
   
  
  
          
  1. Example example = new Example(UserRole.class); //實例化
  2. Example.Criteria criteria = example.createCriteria();

 Criteria是Example中的一個內部類,在最終sql構建時以括號呈現,Criteria里帶了較多構建查詢條件的方法,如

andEqualTo(String property, Object value),

orEqualTo(String property, Object value),

andGreaterThan(String property, Object value),

orGreaterThan(String property, Object value)

傳入的property為實體類中的屬性名,非數據度字段名。

舉例說明,如orEqualTo(String property, Object value),代碼如下:


   
   
  
  
          
  1. Example example = new Example(UserRole.class); //實例化
  2. Example.Criteria criteria = example.createCriteria();
  3. criteria.orEqualTo( "fUserId", "15693a6e509ee4819fcf0884ea4a7c9b");
  4. criteria.orEqualTo( "fUserId", "15ccaf3e89376f7b109eec94d10b7988");
  5. List<UserRole> userRoleList = userRoleService.selectByExample(example);

最終的where語句則為:

( f_user_id = "15693a6e509ee4819fcf0884ea4a7c9b" or f_user_id = "15ccaf3e89376f7b109eec94d10b7988" )

其余方法同理。 

其中andCondition(String condition)方法支持手寫條件,傳入的字符串為最終的查詢條件,如:length(f_user_id)<5

以及likeTo()的方法是不帶百分號%的,需要自己對傳入參數進行構建(加左like或者右like等)。

其余方法自行見源碼,不再贅述。

3、ConditionMapper<T>內方法使用說明:

所有方法均需要傳入tk.mybatis.mapper.entity.Condition,Condition實際上繼承自tk.mybatis.mapper.entity.Example,

源碼中只有三個方法:


   
   
  
  
          
  1. public Condition(Class<?> entityClass) {
  2. super(entityClass);
  3. }
  4. public Condition(Class<?> entityClass, boolean exists) {
  5. super(entityClass, exists);
  6. }
  7. public Condition(Class<?> entityClass, boolean exists, boolean notNull) {
  8. super(entityClass, exists, notNull);
  9. }

說實話我也不知道這樣做有什么意義,望哪位大神指教一下。

boolean exists, boolean notNull這兩個參數的含義為:

若exists為true時,如果字段不存在就拋出異常,false時,如果不存在就不使用該字段的條件,

若notNull為true時,如果值為空,就會拋出異常,false時,如果為空就不使用該字段的條件

其使用方法與Example類似:


   
   
  
  
          
  1. Condition condition = new Condition(UserRole. class);
  2. Criteria criteria = condition.createCriteria();
  3. criteria.orEqualTo( "fUserId", "15693a6e509ee4819fcf0884ea4a7c9b");
  4. criteria.orEqualTo( "fUserId", "15ccaf3e89376f7b109eec94d10b7988");
  5. List<UserRole> userRoleList = userRoleService.selectByCondition(condition);

畢竟是繼承自Example。

4、Example.and()/or()和Condition.and()/or()方法說明:

兩個都一樣,我就挑一個說吧。

實例化方法跟上邊略有不同:


   
   
  
  
          
  1. Condition condition = new Condition(UserRole. class);
  2. //Criteria criteria1 = condition.createCriteria();
  3. Criteria criteria1 = condition. and();

上邊說了,每個Criteria在最終結果中以括號形式展現,此時and()方法則表示 and (Criteria中的條件),or()方法則表示 or (Criteria中的條件),默認createCriteria()等同於and(),測試結果如下:

2018-08-12 18:23:11.805 DEBUG 13760 --- [           main] c.c.m.UserRoleMapper.selectByCondition   : ==>  Preparing: SELECT f_id,f_user_id,f_type,f_status,f_description,f_creator_id,f_create_time,f_last_updator_id,f_last_update_time FROM t_sys_user_role WHERE ( f_user_id = ? and f_user_id = ? ) or ( f_description = ? or f_description = ? )
  
  
 
 
         

原文地址:https://blog.csdn.net/q564495021/article/details/81607515


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM