springBoot-MyBatis-XXExample类.Criteria
1:自动生成逆向工程文件 entity,mapper,xml文件
entity (testZ,testZExample)
public class testZ {
private Integer id;
private String name;
private String age;
public class testZExample {
protected String orderByClause;
protected boolean distinct;
mapper 及 对应 xml
@Mapper
public interface testZMapper {
long countByExample(testZExample example);
int deleteByExample(testZExample example);
int deleteByPrimaryKey(Integer id);
int insert(testZ record);
int insertSelective(testZ record);
List<testZ> selectByExample(testZExample example);
testZ selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") testZ record, @Param("example") testZExample example);
int updateByExample(@Param("record") testZ record, @Param("example") testZExample example);
int updateByPrimaryKeySelective(testZ record);
int updateByPrimaryKey(testZ record);
}
insert:
@Test
public void insert() {
/**
* 插入只有两个方法,方法传入的参数都是POJO,
* 返回值都是int类型的受影响的行数。
* 不同之处在于
* insert:会插入所有的信息,如果传入的对象某一属性为空,则插入空,如果数据库中设置了默认值,默认值就失效了。
* insertSelective:不同,他只会插入含有数据的属性,对于为空的属性,不予以处理,这样的话如果数据库中设置有默认值,就不会被空值覆盖了
*/
testZ t = new testZ();
t.setAge("25");
t.setName("zhangsan");
System.out.println("insert:"+ mapper.insert(t));
testZ t1 = new testZ();
t1.setName("wangwu");
mapper.insertSelective(t1);
System.out.println("insertSelective:"+ mapper.insertSelective(t1));
}
update
@Test
public void update(){
/**
* 第一组Example:根据特定限制条件进行更新,
* updateByExample(封装设置了新值的对象,封装where筛选条件对象):更新所有列,没有设置值的为默认值 null 或者0
* updateByExampleSelective(封装设置了新值的对象,封装where筛选条件对象):只更新设置值的数据,没有设置值的不更新
* updateByExampleWithBLOBs(封装设置了新值的对象,封装where筛选条件对象):根据特定的限制条件进行更新所有列(包含text类型大文本)
* 第二组ByPrimaryKey:根据封装的对象的主键ID更新
* updateByPrimaryKey(封装设置了新值的对象):通过ID更新除了text大文本(数据库)的所有列,没有设置值的为默认值 null 或者0。
* updateByPrimaryKeySelective(封装设置了新值的对象):通过ID,只更新设置值的数据,没有设置值的不更新
* updateByPrimaryKeyWithBLOBs(封装设置了新值的对象)通过ID更新所有列,没有设置值的为默认值 null 或者0。
*/
testZExample testZExample = new testZExample();
com.mybatis.mybatisdemo.entity.testZExample.Criteria criteria = testZExample.createCriteria();
criteria.andAgeIsNull();
criteria.andIdEqualTo(100);
testZ t = new testZ();
t.setId(100);
t.setName("之前是1");
/**
* 只更新设置值的数据,没有设置值的不更新
*/
//mapper.updateByPrimaryKeySelective(t);
/**
* 更新所有数据,没有设置值的为默认值 null 或者0
*/
//mapper.updateByPrimaryKey(t);
/**
* 第一个参数为需要更改的列数据
* 第二个参数为筛选条件
* 根据testZExample特定的限制条件更新t所有设置了值的列。
*/
//int i = mapper.updateByExampleSelective(t, testZExample);
/**
* 第一个参数为需要更改的列数据
* 第二个参数为筛选条件
* 根据testZExample特定的限制条件更新t所有列没有设置值的为默认值null活0。
*/
//mapper.updateByExample(t,testZExample);
}
delete
@Test
public void delect(){
/**
*删除只有两个方法
* deleteByExample(封装where筛选条件对象):根据封装对象的条件筛选删除数据。
* deleteByPrimaryKey(id):根据主键ID删除数据
*返回值都是int类型的受影响的行数。
*/
testZExample testZExample = new testZExample();
com.mybatis.mybatisdemo.entity.testZExample.Criteria criteria = testZExample.createCriteria();
criteria.andAgeIsNull();
// mapper.deleteByExample(testZExample);
mapper.deleteByPrimaryKey(2);
}
select
@Test
public void select(){
/**
* 查询两个方法
* selectByExample(封装查询条件参数对象):根据封装对象的条件筛选插叙数据返回List。
* selectByPrimaryKey(id):根据主键ID查询,返回单个对象。
*/
testZExample testZExample = new testZExample();
testZExample.setStart(0);
testZExample.setLimit(2);
com.mybatis.mybatisdemo.entity.testZExample.Criteria criteria = testZExample.createCriteria();
criteria.andAgeIsNotNull();
List<testZ> testZS = mapper.selectByExample(testZExample);
System.out.println(testZS);
//testZ testZ = mapper.selectByPrimaryKey(5);
//System.out.println(testZ);
}
排序(多重排序)
@Test
public void sort(){
/**
* testZExample.setOrderByClause 设置排序字段及规则
* 多条件按等级顺序“,”隔开
*/
testZExample testZExample = new testZExample();
testZExample.setOrderByClause("age desc ,id desc");
List<testZ> testZS = mapper.selectByExample(testZExample);
System.out.println(testZS);
}
分页(Example类中添加分页参数),修改xml查询sql(limit #{start},#{limit})
public class testZExample {
protected String orderByClause;
protected boolean distinct;
private int start;
private int limit;
<select id="selectByExample" parameterType="com.mybatis.mybatisdemo.entity.testZExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
'true' as QUERYID,
<include refid="Base_Column_List" />
from test
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
<if test="start !=0 or limit!=0">
limit #{start},#{limit}
</if>
@Test
public void select(){
testZExample testZExample = new testZExample();
testZExample.setStart(0);
testZExample.setLimit(2);
List<testZ> testZS = mapper.selectByExample(testZExample);
System.out.println(testZS);
System.out.println(testZ);
}
Example说明:
Mybatis逆向工程会生成实例及实例对应的example(用于添加条件,相当于where后的部分)
xxxExample example = new xxxExample();
Criteria criteria = example.createCriteria();
example.setOrderByClause("username asc"); //asc升序,desc降序排列
example.setDistinct(false); //去除重复,true是选择不重复记录,false反之
example.Criteria criteria = userExample.createCriteria(); //构造自定义查询条件
criteria方法说明:
Mybatis逆向工程会通过criteria构造查询条件
// 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)