mybatis逆向工程的selective
简介
在mybatis逆向工程所生成的mapper文件中,存在着selective方法,分别存在于insert语句和update语句。
insertSelective和insert
以
两个方法名分别是
int insert(User record);
int insertSelective(User record);
两者区别在于:
insert
方法会直接将传入参数record的所有属性插入数据库,包括null。
insertSelective
方法会跳过值null的字段。
两者应用场景在于:
如果数据库字段有默认值,而插入时不希望改变默认值时使用insertSelective
希望将默认值致null时使用insert
。一般而言,使用insertSelective
。
updateByExample相关
updateByExample
方法
int updateByExample(@Param("record") SealApplication record, @Param("example") SealApplicationExample example);
对注入的字段全部更新,为null的字段更新为null。
updateByExampleSelective
int updateByExampleSelective(@Param("record") SealApplication record, @Param("example") SealApplicationExample example);
对字段进行判断再更新,如果更新的字段中有null,忽略更新该字段,该字段还是以前的值。
第一个参数 是要修改的部分值组成的对象,其中有些属性为null则表示该项不修改。
第二个参数 是一个对应的查询条件的类, 通过这个类可以实现 order by 和一部分的where 条件。
updateByPrimaryKey相关
updateByPrimaryKey
int updateByPrimaryKey(SealApplication record);
updateByPrimaryKey 对你注入的字段全部更新,将为空的字段在数据库中置为NULL
updateByPrimaryKeySelective
int updateByPrimaryKeySelective(SealApplication record);
updateByPrimaryKeySelective 会对字段进行判断再更新(如果为Null就忽略更新),如果你只想更新某一字段,可以用这个方法。
updateByPrimaryKey与updateByExample
两者区别在于,updateByExample会根据条件(即传入的第二个参数example)进行更新。updateByPrimaryKey会根据传入类的主键进行更新。