mybatis自动生成entity层和dao层中Mapper接口中的各个方法的意义及example实体类的用法

以下是自动生成的Mapper接口中的方法所代表的意义,网上这篇文章有更详细的讲解,推荐给大家MyBatis的Mapper接口以及Example的实例函数及详解

package cn.lichenyang.emall.dao;

import cn.lichenyang.emall.entity.TbContent;
import cn.lichenyang.emall.entity.TbContentExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;

public interface TbContentMapper {
	
    //根据条件计算总数,example用来设置当前对象的条件
    int countByExample(TbContentExample example);
    
    //根据条件删除,example用来设置当前对象的条件
    int deleteByExample(TbContentExample example);

    //根据主键删除,example用来设置当前对象的条件
    int deleteByPrimaryKey(Long id);

    //插入数据(如果当前对象没有值,也会被插入到数据库中)
    int insert(TbContent record);
    
    //插入数据,如果当前对象的属性为null,则当前属性不会插入数据库,只向数据库插入有值的属性
    int insertSelective(TbContent record);

    //按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    List<TbContent> selectByExampleWithBLOBs(TbContentExample example);

    //根据条件查询,example就是TbContent的条件
    List<TbContent> selectByExample(TbContentExample example);

    //根据主键查询
    TbContent selectByPrimaryKey(Long id);
    
    //按条件更新值不为null的字段 			解释:前台传入那个值就修改那个值,example用于添加条件,相当于是where后面的内容			
    int updateByExampleSelective(@Param("record") TbContent record, @Param("example") TbContentExample example);
    
    //按条件进行跟新		 (包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    int updateByExampleWithBLOBs(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根据条件进行更新,所有带有example的修改,左边只用设置从界面传过来的值,右边设置的是传过来的对象的条件
    int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

    //根据主键更新当前对象的属性的值不为null的属性,如果为null,就不进行修改
    int updateByPrimaryKeySelective(TbContent record);

    //根据主键进行更新,(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生。
    int updateByPrimaryKeyWithBLOBs(TbContent record);

    //根据主键进行修改
    int updateByPrimaryKey(TbContent record);
}

xxxexample的用法:

xxxExample example = new xxxExample();  
Criteria criteria = example.createCriteria();  
方法说明:  
// 1.添加升序排列条件,DESC为降序  
example.setOrderByClause("字段名ASC")  
// 2.去除重复,boolean类型,true为选择不重复的记录  
example.setDistinct(false)  
// 3.添加字段xxx为null的条件  
criteria.andXxxIsNull  
// 4.添加字段xxx不为null的条件  
criteria.andXxxIsNotNull  
// 5.添加xxx字段等于value条件  
criteria.andXxxEqualTo(value)  
// 6.添加xxx字段不等于value条件  
criteria.andXxxNotEqualTo(value)  
// 7.添加xxx字段大于value条件  
criteria.andXxxGreaterThan(value)  
// 8.添加xxx字段大于等于value条件  
criteria.andXxxGreaterThanOrEqualTo(value)  
// 9.添加xxx字段小于value条件  
criteria.andXxxLessThan(value)  
// 10.添加xxx字段小于等于value条件  
criteria.andXxxLessThanOrEqualTo(value)  
// 11.添加xxx字段值在List  
criteria.andXxxIn(List)  
// 12.不添加xxx字段值在List  
criteria.andXxxNotIn(List)  
// 13.添加xxx字段值在之间  
criteria.andXxxBetween(value1,value2)  
// 14.添加xxx字段值不在之间  
criteria.andXxxNotBetween(value1,value2)

例:执行一下方法

int updateByExample(@Param("record") TbContent record, @Param("example") TbContentExample example);

recore是从界面传过来的对象(需要修改的属性封装成的对象),example就是上面设置好值之后的example

执行查询的时候:xxxmapper.updateByExample(record,example)


举更详细的例子

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'


猜你喜欢

转载自blog.csdn.net/qq_37385585/article/details/79903280