关于MyBatis的Example类详解

本文详细介绍了MyBatis Generator如何自动生成Example类,用于构建动态SQL。Example类提供了一系列方法,如等于、不等于、大于、小于等条件,支持and、or组合查询。同时展示了增、删、改、查的各种操作示例,帮助理解其在实际开发中的应用。

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

Example类的定义?
mybatis-generator会为每个字段产生Criterion,为底层的mapper.xml创建动态sql。如果表的字段比较多,产生的example类会十分庞大。理论上通过example类可以构造你想到的任何筛选条件。在mybatis-generator中加以配置,配置数据表的生成操作就可以自动生成example了。

mybatis 的mapper接⼝提供了增、删、改、查的⽅法。避免过多使⽤xml来直接写sql。
实体类 User:

private Integer id;
private String name;
private String age;
private Integer sex;

Example类的使用:

Example examle = new Example(User.class);
example.setOrderByClause("字段名 asc,字段名 desc");
//去除重复,boolean 型,true 为选择不重复的记录。
example.setDistinct(false);
 Criteria criteria = new Example().createCriteria();
 is null;is not null;  
 equal to(value);not equal to(value);  
 GreaterThan(value);GreaterThanOrEqualTo(value);  
 LessThan(value); LessThanOrEqualTo(value);  
 in(item,item,item,...);not in(item,item,item,...);  
 like("%"+value+"%");not like("%"+value+"%");  
 Between(value1,value2);not between(value1,value2);

Mybatis的实例函数:

返回值类型方法功能说明
int countByExample(UserExample example) throws SQLException按条件计数
int deleteByPrimaryKey(Integer id) throws SQLException按主键删除
int deleteByExample(Example example) throws SQLException按条件删除
String / Integer insert (User user) throws SQLException 插⼊数据(返回值为ID)
   User selectByPrimaryKey(Integer id) thorws SQLException按主键查询
   List<?>selectByExample(UserExample example) thorws SQLException按条件查询
   List<?>selectByExampleWithBLOGs(UserExample example) thorws SQLException按条件查询(包括BLOB字段),只有当数据表中的字段类型有为⼆进制的才会产⽣。
   int updateByPrimaryKey(User record) thorws SQLException按主键更新,(注意会把数据库中⾮空的字段更新为null)
   int updateByPrimaryKeySelective(User record) thorws SQLException按主键更新值不为null的字段
    int updateByExample(User record, UserExample example) thorws SQLException按条件更新
    int updateByExampleSelective(User record, UserExample example) thorws  SQLException按条件更新值不为null的字段

实例方法详解:

1.按条件统计:

Example example = new Example(User.class);
//Criteria criteria = example.createCriteria();

example.createCriteria().andEqualTo("id", "1001" )

UserMapper.countByExample(example);
//等同于:select count(*) from user where id='1001'

2.查询:

(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("name", "小杨");
User user = UserMapper.selectByExample(example);

//等同于:select * from user where id = "1001" and name = '小杨'

        selectByExample (or条件)

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

//等同于:select * from user where id = "1001" or name = '小杨'

        selectByExample (and+or多条件查询)

Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", "1001" )
                .andEqualTo("name", "小杨");
        example.and().orEqualTo("age","18")
                .orEqualTo("sex","1");
        List<User> user = UserMapper.selectByExample(example);

//等同于:
SELECT id,user_id,user_name,pass_word FROM user WHERE ( id = "1001" and name= "小杨" ) and ( age= "18" or sex= "1" )

3.插入:

User user = new User();
user.setId("1002");
user.setAge("18");
user.setName("小王");
user.setSex("0")
UserMapper.insert(user);
//等同于:
insert into user(id,age,name,sex) values ('1002','18','小王','0');

4.更新:
(1)updateByPrimaryKeyimaryKey 按主键更新(会把没有设置的值设置为空)

User user = new User();
user.setId("1002");
user.setName("小王");
user.setAge("18");
user.setSex("0")
UserMapper.updateByPrimaryKey(user);
//等同于:update user set sex= "0" name='小王', age='18' where id='1002'

(2)updateByExampleSelective 按主键更新(不会把null 更新)

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

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

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

UserMapper.updateByExampleSelective(user,example);

//等同于:update user set Name='小王' where id='1002'

5.删除:
(1)deleteByPrimaryKey 按主键删除

UserMapper.deleteByPrimaryKey("1002");  
//相当于:delete from user where id="1002"

(2)deleteByExample 按条件删除

Example example = new Example(User.class);
example.createCriteria().andEqualTo("name","小王");
UserMapper.deleteByExample(example);
//等同于:delete from user where name ='小王'

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值