关于MyBatis的Example类详解

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 ='小王'

猜你喜欢

转载自blog.csdn.net/leaf__yang/article/details/125273086