Mybatis中Mapper内置方法细解

来源:互联网 发布:go语言 python 编辑:程序博客网 时间:2024/06/10 15:18

1、countByExample ===>根据条件查询数量

int countByExample(UserExample example);//下面是一个完整的案列UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("taibai"); int count = userDAO.countByExample(example); 相当于:select count(*) from user where username='taibai'

2、deleteByExample ===>根据条件删除多条

int deleteByExample(AccountExample example);//下面是一个完整的案例UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("taibai"); userDAO.deleteByExample(example); 相当于:delete from user where username='taibai'

3、deleteByPrimaryKey===>根据条件删除单条

 int deleteByPrimaryKey(Integer id);userDAO.deleteByPrimaryKey(110);  相当于:delete from user where id=110

4、insert===>插入数据

int insert(Account record);//下面是完整的案例User user = new User(); //user.setId(101); user.setUsername("test"); user.setPassword("123456") user.setEmail("674531003@qq.com"); userDAO.insert(user); 相当于:insert into user(ID,username,password,email) values(110,'root','123456','12345678@qq.com');

5、insertSelective===>插入数据

int insertSelective(Account record);

6、selectByExample===>根据条件查询数据

 List<Account> selectByExample(AccountExample example);//下面是一个完整的案例UserExample example = new UserExample();Criteria criteria = example.createCriteria();criteria.andUsernameEqualTo("taibai");criteria.andUsernameIsNull();example.setOrderByClause("username asc,email desc");List<?>list = userDAO.selectByExample(example);相当于:select * from user where username = 'taibai' and username is null order by username asc,email desc//注:在iBator 生成的文件UserExample.java中包含一个static 的内部类 Criteria ,在Criteria中有很多方法,主要是定义SQL 语句where后的查询条件。

7、selectByPrimaryKey===>根据主键查询数据

Account selectByPrimaryKey(Integer id);//相当于select * from user where id = 变量id

8、updateByExampleSelective===>按条件更新值不为null的字段

int updateByExampleSelective(@Param("record") Account record, @Param("example") AccountExample example);//下面是一个完整的案列UserExample example = new UserExample(); Criteria criteria = example.createCriteria(); criteria.andUsernameEqualTo("taibai"); User user = new User(); user.setPassword("123"); userDAO.updateByPrimaryKeySelective(user,example); 相当于:update user set password='123' where username='taibai'

9、updateByExampleSelective===>按条件更新

int updateByExample(@Param("record") Account record, @Param("example") AccountExample example);

10、updateByPrimaryKeySelective===>按条件更新

int updateByPrimaryKeySelective(Account record);//下面是一个完整的案例 User user = new User();user.setId(110);user.setPassword("taibai");userDAO.updateByPrimaryKeySelective(user);相当于:update user set password='taibai' where id=110

11、updateByPrimaryKey===>按主键更新

int updateByPrimaryKey(Account record);

//下面是一个完整的案例
User user =new User();
user.setId(110);
user.setUsername(“root”);
user.setPassword(“123456”);
user.setEmail(“123456@qq.com”);
userDAO.updateByPrimaryKey(user);
相当于:update user set username=’root’,password=’123456’,email=’123456@qq.com’ where id=110

原创粉丝点击