Mybatis 的mapper接口方法介绍及使用详解

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

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

Examle 类的使用

Example examle = new Example(User.class);
example.setOrderByClause("字段名 asc,字段名 desc");
example.setDistinct(false);//去除重复,boolean 型,true 为选择不重复的记录。
 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 :按条件删除 ;
   Integer (User user) throws SQLException 插入;
   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. countByExample 按条件统计
	Example example = new Example(User.class);
	Criteria criteria = example.createCriteria();
 	criteria.andEqualTo("name",name);
 	criteria.andEqualTo("sex",sex);
 	Mapper.countByExample(example);
  1. deleteByPrimaryKey 按主键删除
	Mapper.deleteByPrimaryKey(userId);
  1. deleteByExample 按条件删除
Example example = new Example(user.class);
Criteria criteria = example.createCriteria();
criteria.andEqualTo("name",name); 
Mapper.deleteByExample(example); 
  1. insert 插入操作
User user = new User();
user.setName("小红");
Mapper.insert(user);
  1. selectByPrimaryKey 按主键查询
	User user = ##Mapper.selectByPrimaryKey(100); 
	相当于select * from user where id=100;
  1. selectByExample 按条件查询
  Example example = new Example(user.class);
  Criteria criteria = example.createCriteria();
  criteria.addEqualTo("name","小红");
  example.setOrderByClause("username asc,email desc");  
  Mapper.selectByExample (example);
  相当于 select  *  from user where name='小红' order by username asc, email desc;
  1. updateByPrimaryKey 按主键更新
    (会把没有设置的值设置为空)
User user = new User();
user.setId("111");
user.setUserId(123456);
user.setName("小红");
Mapper.updateByPrimaryKey (user)  相当于 update t_user set user_id=123456,name='小红',sex =null, where id='111'; 
  1. updateByPrimaryKeySelective 按主键更新(不会把null 更新)
User user = new User();
user.setId("1234");
user.Name("小明");
Mapper.updateByPrimaryKeySelective (user );
相当于 update    t_user   set  name ='小明' where id=1234;  
  1. deleteByPrimaryKey 按主键删除
User user =  new User();
user.setId("1234");
Mapper.deleteByPrimaryKey(user);  相当于 delete from t_user where id='1234';
  1. deleteByExample 按条件删除
User user  = new User();
user.setName("小名");
Mapper.deleteByExample(user);
相当于 delete from t_user where name='小名'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值