zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Unix/Linux 软件安装
    Cocos2d-x 脚本语言Lua基本数据结构-表(table)
    HTML5----CSS3图片滤镜(filter)特效
    Cacti监控Redis实现过程
    为何被主流抛弃-江西IDC机房价格为何居高不下缺少竞争力-2014年5月江西IDC排行榜
    HDU3367 Pseudoforest 【并查集】+【贪心】
    cocos2d-x 2.2.3 建project
    从零開始学android&lt;数据存储(1)SharedPreferences属性文件.三十五.&gt;
    SQL Server连接Oracle详细步骤
    SQL Server与Oracle对比学习:权限管理(一)
  • 原文地址:https://www.cnblogs.com/jpfss/p/11363367.html
Copyright © 2011-2022 走看看