zoukankan      html  css  js  c++  java
  • Mybatis中的自带Mapper方法


    1.各方法介绍

    mybatis逆向工程生成的mapper源码:

    import com.itheima.springmvc.pojo.Items;
    import com.itheima.springmvc.pojo.ItemsExample;
    import java.util.List;
    import org.apache.ibatis.annotations.Param;
     
    public interface ItemsMapper {
    	
        //按条件计数	
        int countByExample(ItemsExample example);
     
        //按条件删除
        int deleteByExample(ItemsExample example);
     
        //按主键删除
        int deleteByPrimaryKey(Integer id);
     
        //插入数据(返回值为ID):如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段改为null;
        int insert(Items record);
     
        //插入数据(返回值为ID):如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段不动;
        int insertSelective(Items record);
     
        //按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
        List<Items> selectByExampleWithBLOBs(ItemsExample example);
     
        //按条件查询
        List<Items> selectByExample(ItemsExample example);
     
        //按主键查询
        Items selectByPrimaryKey(Integer id);
     
        //按条件更新值不为null的字段:如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段不动;
        int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
     
        //和updateByExample相比此方法可以修改大字段类型,其余性质和updateByExample相同
        int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
     
        //按条件更新:如果example定义了两个字段,数据库共4个字段,则修改数据库的两个字段,其余两个字段改为null;
        int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
     
        //按主键更新值不为null的字段:如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段不动;
        int updateByPrimaryKeySelective(Items record);
     
        //和updateByPrimaryKey相比此方法可以修改大字段类型,其余性质和updateByPrimaryKey相同
        int updateByPrimaryKeyWithBLOBs(Items record);
     
        //按主键更新:如果record定义了两个字段,其中有一个字段是主键,数据库共4个字段,则根据主键修改数据库的两个字段,其余两个字段改为null;
        int updateByPrimaryKey(Items record);
    }
    

    2.方具体使用

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

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

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

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

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

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

    4、insert===>插入数据

    int insert(Account record); 
    //下面是完整的案例
    User user = new User(); 
    user.setId(10001); 
    user.setUsername("mrHan"); 
    user.setPassword("123456") 
    user.setEmail("786***195@qq.com"); 
    userDAO.insert(user); 
    相当于:insert into user(ID,username,password,email) values(10001,'mrHan','123456','786***195@qq.com');

    5、insertSelective===>插入数据

    int insertSelective(Accountrecord);

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

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

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

    AccountselectByPrimaryKey(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("joe"); 
    User user = new User(); 
    user.setPassword("123456"); 
    userDAO.updateByPrimaryKeySelective(user,example); 
    相当于:update user set password='123456' where username='joe';

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

    int updateByExample(@Param("record")Accountrecord,@Param("example")AccountExampleexample);

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

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

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

    int updateByPrimaryKey(Accountrecord); 
    //下面是一个完整的案例
    Useruser=newUser();
    user.setId(10001);
    user.setUsername("mrHan");
    user.setPassword("123456");
    user.setEmail("786***195@qq.com");
    userDAO.updateByPrimaryKey(user);
    相当于:update user set username='mrHan',password='123456',email='786**195@qq.com'where id=10001;
    int updateByPrimaryKeySelective(Accountrecord); 
    //下面是一个完整的案例 
    Useruser=newUser();
    user.setId(10001);
    user.setPassword("123456");
    userDAO.updateByPrimaryKeySelective(user);
    相当于:update user set password='123456'where id=10001;
    补充 
    Mybatis自动生成的查询selectByExample(TExample example) 中like需要自己写通配符
    
    TExample example = new TExample(); 
    TExample.Criteria criteria = example.createCriteria(); 
    if(StringUtils.isNotBlank(userName)){ 
    userName = "%" + userName + "%"; 
    } 
    if(StringUtils.isNotBlank(userName)){ 
      criteria.andBuyerNameLike(userName); 
    } 
    dao.countByExample(example)
    
  • 相关阅读:
    Java——字符串操作
    算法——Java实现队列
    算法——Java实现栈
    算法——线性表之链式存储结构
    算法——线性表之顺序存储结构
    Java——单双引号的区别
    Hystrix源码解析
    Eureka源码探索(一)-客户端服务端的启动和负载均衡
    dubbo源码研究(一)
    dubbo-springboot入门级demo
  • 原文地址:https://www.cnblogs.com/aaaazzzz/p/13173054.html
Copyright © 2011-2022 走看看