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的字段
实例方法详解:
- countByExample 按条件统计
Example example = new Example(User.class);
Criteria criteria = example.createCriteria();
criteria.andEqualTo("name",name);
criteria.andEqualTo("sex",sex);
Mapper.countByExample(example);
- deleteByPrimaryKey 按主键删除
Mapper.deleteByPrimaryKey(userId);
- deleteByExample 按条件删除
Example example = new Example(user.class);
Criteria criteria = example.createCriteria();
criteria.andEqualTo("name",name);
Mapper.deleteByExample(example);
- insert 插入操作
User user = new User();
user.setName("小红");
Mapper.insert(user);
- selectByPrimaryKey 按主键查询
User user = ##Mapper.selectByPrimaryKey(100);
相当于select * from user where id=100;
- 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;
- 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';
- updateByPrimaryKeySelective 按主键更新(不会把null 更新)
User user = new User();
user.setId("1234");
user.Name("小明");
Mapper.updateByPrimaryKeySelective (user );
相当于 update t_user set name ='小明' where id=1234;
- deleteByPrimaryKey 按主键删除
User user = new User();
user.setId("1234");
Mapper.deleteByPrimaryKey(user); 相当于 delete from t_user where id='1234';
- deleteByExample 按条件删除
User user = new User();
user.setName("小名");
Mapper.deleteByExample(user);
相当于 delete from t_user where name='小名'