springboot整合tkmybatis以及使用

springboot整合tkmybatis以及使用经常用 mybatis 的的都知道 使用 mybatisorm 框架存在一个非常不友善的问题就是 就是每操作一个单表就需要自己手写一个 xml 文件 虽然说可以用工具生成 xml 和实体类可以解决这个问题 但是二次开发的时候对某个表字段进行修改的时候 生成 xml 文件就不现实啦 最近发现 tkmybatis 就非常好的解决了这个问题 在这里和大家分享一下 框架配置这里需要引用到的包 mybat

经常用mybatis的的都知道,使用mybatis orm框架存在一个非常不友善的问题就是,就是每操作一个单表就需要自己手写一个xml文件,虽然说可以用工具生成xml和实体类可以解决这个问题,但是二次开发的时候对某个表字段进行修改的时候,生成xml文件就不现实啦。最近发现tkmybatis就非常好的解决了这个问题。在这里和大家分享一下。

  • 框架配置

这里需要引用到的包

 <!--mybatis操作数据库有关--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--连接mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--使用阿里巴巴的druid数据源,有利于监控sql的执行情况--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--通用mapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--添加lombok支持,可以省掉写get和set方法--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> <optional>true</optional> </dependency> <!--使用fastjson来操作json数据--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.31</version> </dependency> <!--spring boot web方面的支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.4.RELEASE</version> </dependency> 

数据源配置

spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver 

表结构语句

DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `user_phone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `user_name_index`(`user_name`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES (1, 'evan', '26'); INSERT INTO `user` VALUES (2, 'evan11', '26'); 
  • 类配置方法
    实体类方法
    注意这里是引用了lombok框架,可以省略写get,set之类的方法,这里需要idea里面添加lombok插件,如果有报错的话。(如果有强迫症的话)其实还是代码还是可以跑起来的。

@Data @Table(name="user") public class UserModel { @Id @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC") private Integer id; @Column private String userName; @Column private String userPhone; } 

在这里插入图片描述

  • Service类
    这里主要是实现了上边BaseMapper中继承的5个Mapper的方法。
    tk.mybatis.mapper.common.BaseMapper中有较多方法,均需要继承实现:

 / * 保存一个实体,null属性也会保存 * * @param record * @return */ int insert(T record); / * 保存一个实体,null属性不会保存 * * @param record * @return */ int insertSelective(T record); / * 根据实体属性作为条件进行删除,查询条件使用等号 */ int delete(T record); / * 根据主键更新属性不为null的值 */ int updateByPrimaryKeySelective(T record); / * 根据实体中的属性值进行查询,查询条件使用等号 */ List<T> select(T record); / * 查询全部结果,select(null)方法能达到同样的效果 */ List<T> selectAll(); / * 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号 */ T selectOne(T record); / * 根据实体中的属性查询总数,查询条件使用等号 */ int selectCount(T record); 

MySqlMapper中的方法如下:

/ * 批量插入,支持批量插入的数据库可以使用,例如MySQL,H2等,另外该接口限制实体包含`id`属性并且必须为自增列 */ public int insertList(List<T> recordList); / * 插入数据,限制为实体包含`id`属性并且必须为自增列,实体配置的主键策略无效 */ public int insertUseGeneratedKeys(T record); 

IdsMapper中的方法如下:

 / * 根据主键@Id进行查询,多个Id以逗号,分割 * @param id * @return */ List<T> selectByIds(String ids); / * 根据主键@Id进行删除,多个Id以逗号,分割 * @param id * @return */ int deleteByIds(String ids); 

ConditionMapper中的方法如下:

/ * 根据Condition条件进行查询 */ public List<T> selectByCondition(Object condition); / * 根据Condition条件进行查询 */ public int selectCountByCondition(Object condition); / * 根据Condition条件删除数据,返回删除的条数 */ public int deleteByCondition(Object condition); / * 根据Condition条件更新实体`record`包含的全部属性,null值会被更新,返回更新的条数 */ public int updateByCondition(T record, Object condition); / * 根据Condition条件更新实体`record`包含的全部属性,null值会被更新,返回更新的条数 */ public int updateByConditionSelective(T record, Object condition); 

ExampleMapper中的方法如下:

/ * 根据Example条件进行查询 */ public List<T> selectByExample(Object example); / * 根据Example条件进行查询,若有多条数据则抛出异常 */ public T selectOneByExample(Object example); / * 根据Example条件进行查询总数 */ public int selectCountByExample(Object example); / * 根据Example条件删除数据,返回删除的条数 */ public int deleteByExample(Object example); / * 根据Example条件更新实体`record`包含的全部属性,null值会被更新,返回更新的条数 */ public int updateByExample(T record, Object example); / * 根据Example条件更新实体`record`包含的不是null的属性值,返回更新的条数 */ public int updateByExampleSelective(T record, Object example); 

使用方法

ExampleMapper内方法使用说明:

 Example example = new Example(UserModel.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("id","1"); criteria.orEqualTo("userName","evan11"); 
Example example = new Example(UserRole.class);//实例化 Example.Criteria criteria = example.createCriteria(); 
 Example example = new Example(UserModel.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("id","1"); criteria.orEqualTo("userName","evan11"); List<UserModel> userModels = userDao.selectByExample(example); 

最总输出的sql语句是

Preparing: SELECT id,user_name,user_phone FROM user WHERE ( ( id = ? and user_name = ? ) ) Parameters: 1(String), evan11(String) 

其中andCondition(String condition)方法支持手写条件,传入的字符串为最终的查询条件,如:length(f_user_id)<5

以及likeTo()的方法是不带百分号%的,需要自己对传入参数进行构建(加左like或者右like等)。

其余方法自行见源码,不再赘述。

ConditionMapper内方法使用说明:
所有方法均需要传入tk.mybatis.mapper.entity.Condition,Condition实际上继承自tk.mybatis.mapper.entity.Example,
源码中有三个方法:

 public Condition(Class<?> entityClass) { super(entityClass); } public Condition(Class<?> entityClass, boolean exists) { super(entityClass, exists); } public Condition(Class<?> entityClass, boolean exists, boolean notNull) { super(entityClass, exists, notNull); } 

其使用方法与Example类似

 Condition condition = new Condition(UserRole.class); Criteria criteria = condition.createCriteria(); criteria.andEqualTo("id","1"); criteria.orEqualTo("userName","evan11"); List<UserModel> userModels = userDao.selectByExample(example); 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233863.html原文链接:https://javaforall.net

(0)
上一篇 2025年6月15日 下午4:01
下一篇 2025年6月15日 下午4:22


相关推荐

  • Java面试笔试题大汇总(最全+详细答案)

    Java面试笔试题大汇总(最全+详细答案)声明 有人说 有些面试题很变态 个人认为其实是因为我们基础不扎实或者没有深入 本篇文章来自一位很资深的前辈对于最近 java 面试题目所做的总结归纳 有 170 道题目 知识面很广 而且这位前辈对于每个题都自己测试给出了答案 如果你对某个题有疑问或者不明白 可以电脑端登录把题目复制下来然后发表评论 大家一起探讨 也可以电脑端登录后关注我给我发私信 我们一起进步 以下内容来自这位前辈 2013 年年底的

    2026年3月17日
    2
  • js页面跳转无效(js登录成功后跳转页面)

    或者window.open无效,setTimeout(function(){window.location.href=””;},100);就行了

    2022年4月13日
    103
  • webssh的安装与使用

    webssh的安装与使用最近研究了一下在 web 端实现一个远程连接终端操作的类似网页版 xshell 的实现 在网上搜索了一下发现已经有类似的操作在这里主要介绍以下两种 https github com huashengdun webssh https github com billchurch WebSSH2 我用的是虚拟机 centos7 系统 别的 linux 系统应该类似 1 首先是第一种 使用 python 和 j

    2026年3月18日
    0
  • SQL的单表查询

    SQL的单表查询

    2021年7月20日
    64
  • ROS中Remap标签详解,举例说明其两种用法

    ROS中Remap标签详解,举例说明其两种用法1 lt remap gt 标签 lt remap gt 标签 允许你以更结构化的方式将名称重新映射参数传递给 ROS 节点 而不是直接设置 lt 节点 gt 的参数属性 2 作用 2 1 重命名一个已经存在的主题 在自己的 lanuch 文件中 修改自己的发布的主题名字为别人要订阅的主题的名字 针对自己发布的主题 改变自己发布主题的名字 from original nam

    2026年3月19日
    3
  • spring cloud面试题_javaspring面试题

    spring cloud面试题_javaspring面试题Tags:JavaEE,Spring,面试题发表时间:2014-11-2915:03:53原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。比如:转自:Su的技术博客 原文地址:https://blog.verysu.com/article/119 这些Spring面试题是从尚硅谷http://www.atguigu.c…

    2022年10月14日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号