MyBatis的Example总结

本文介绍mybatis中的Example类,该类能简化常见的CRUD操作,如查询、插入、更新和删除等,尤其适用于字段较多的情况,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在工作中遇到的Example类直接可以当mybatisplus的basemapper来用还是很好的

Example类

  • 什么是Example类呢?
    。其实Example类是mybatis的内容,官方解释是:mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。在mybatis-generator中加以配置,配置数据表的生成操作就可以自动生成example了。(学过mybatispuls了话就很好理解,跟那mapper一样的)
上图:

在这里插入图片描述

解释

这个是Example类创建的方法API:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

1.查询:

(1)selectByPrimaryKey

User user = UserMapper.selectByPrimaryKey("1001");
等同于:select * from user where id = "1001" 

(2)selectByExample (and条件)

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
	.andEqualTo("userName", "小李");
User user = UserMapper.selectByExample(example);

等同于:select * from user where id = "1001" and username = '小李'

selectByExample (or条件)

Example example = new Example(User.class);
example.or.andEqualTo("id", "1001" )
example.or.andEqualTo("userName", "小李");
User user = UserMapper.selectByExample(example);

等同于:select * from user where id = "1001" or username = '小李'

selectByExample (and+or多条件查询)

Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", "1" )
                .andEqualTo("userId", "1001");
        example.and().orEqualTo("userName","小李")
                .orEqualTo("passWord","123");
        List<User> user = UserMapper.selectByExample(example);

等同于:
SELECT id,user_id,user_name,pass_word FROM user WHERE ( id = "1" and user_id = "1001" ) and ( user_name = "小李" or pass_word = "123" )
2.插入:
User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("小王");
user.setPassWord("666")
UserMapper.insert(user);
等同于:insert into user(id,user_id ,user_name,pass_word) values ('2','1002','小王','666');
3.更新:

(1)updateByPrimaryKey

User user = new User();
user.setId("2");
user.setUserId("1002");
user.setUserName("小王");
user.setPassWord("666")
UserMapper.updateByPrimaryKey(user);
等同于:update user set user_id = "1002" user_name='小王', pass_word='666' where id='2'

(2)updateByExampleSelective

注意:updateByExample()更新所有的字段,包括字段为null的,建议使用 updateByExampleSelective()更新需要更新的字段

Example example = new Example(User.class);
example.createCriteria().andEqualTo("id","2");

User user = new User();
user.setUserName("小王");

UserMapper.updateByExampleSelective(user,example);

等同于:update user set user_name='小王' where id='2'

4.删除:

(1)deleteByPrimaryKey

UserMapper.deleteByPrimaryKey("1");  //相当于:delete from user where id="1"
``
(2)deleteByExample
```java
Example example = new Example(User.class);
example.createCriteria().andEqualTo("userId","1002");
UserMapper.deleteByExample(example);
等同于:delete from user where userId='1002'
5.查询数量:
Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", "1001" )
UserMapper.countByExample(example);
等同于:select count(*) from user where id='1001'

总结:

Mybatis逆向工程会生成实例及实例对应的example(用于添加条件,相当于where后的部分)
xxxExample example = new xxxExample();
Criteria
criteria = example.createCriteria()后面可以一直.条件。
这个其他也是属于mybatis的内容,就是别人帮你封装了,你可以不用去写一些单表的crud了,直接去实现自己的业务逻辑就行。
这也大大节省了开发时间,跟mybtaisplus的用法大同小异。

在这里插入图片描述

### 关于 `tk.mybatis` 的使用示例与教程 #### 基本概念 `tk.mybatis` 是基于 MyBatis 的增强工具包,提供了许多便捷的功能来简化开发流程。它通过封装常见的 CRUD 操作以及提供灵活的动态 SQL 功能,极大地提高了开发效率。 --- #### 批量插入数据 在实际项目中,可能会遇到批量插入的需求。然而,在某些情况下会出现错误提示。这通常是因为配置或者方法调用方式存在问题[^2]。以下是解决该问题的一个简单示例: ```java // 配置 Mapper 接口继承 BaseMapper<T> public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {} // 调用批量插入方法 List<User> userList = new ArrayList<>(); userList.add(new User("Alice", 20)); userList.add(new User("Bob", 25)); int result = userMapper.insertList(userList); if (result > 0) { System.out.println("批量插入成功!"); } ``` 上述代码展示了如何利用 `insertList` 方法实现批量插入功能。 --- #### 条件查询 `tk.mybatis` 提供了强大的条件查询支持,主要依赖于 `Example` 类构建复杂的查询逻辑。以下是一个多字段排序的例子[^3]: ```java Example example = new Example(User.class); example.createCriteria().andEqualTo("status", 1); // 查询状态为1的数据 example.setOrderByClause("create_time desc, remark desc"); List<User> users = userMapper.selectByExample(example); for (User user : users) { System.out.println(user.getName()); } ``` 此代码片段演示了如何设置多个字段排序并执行条件查询的操作。 --- #### 更新操作 对于更新场景,`tk.mybatis` 提供了两种核心方法:`updateByExample` 和 `updateByExampleSelective`。两者的区别如下[^4]: - **`updateByExample`**: 将传入对象中的所有字段值写入数据库,无论这些字段是否为空。 - **`updateByExampleSelective`**: 只有当字段值不为空时才会被更新到数据库中。 具体应用实例: ```java // 创建更新对象 User record = new User(); record.setId(1L); record.setName(null); // 使用 updateByExample 进行更新 Example example = new Example(User.class); example.createCriteria().andEqualTo("id", 1L); int rowsAffected = userMapper.updateByExample(record, example); System.out.println(rowsAffected + " 行受影响"); ``` 如果希望仅更新非空字段,则应改用 `updateByExampleSelective` 方法。 --- #### 总结 以上内容涵盖了 `tk.mybatis` 在日常开发中的几个典型应用场景,包括但不限于批量插入、复杂条件查询和不同类型的更新操作。开发者可以根据业务需求选择合适的方式完成相应的任务。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值