generator生成对象和xml的时候,会生成一个example对象,看一下example在查询、修改当中的使用,本文在这里进行记载,方便以后使用:
一、常用的方法举例:
所需依赖:
<!-- 映射关系:自动生成实体 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>
*搭建过程略,后期会专门出一个搭建过程的文章。*
1、简单查询
DemoExample example = new DemoExample(); example.createCriteria().andIdEqualTo(1L); List<Demo> demos = demoMapper.selectByExample(example); System.out.println(demos);
2、模糊查询
DemoExample example = new DemoExample(); //注意条件 example.createCriteria().andUserNameLike("%" + "l" + "%"); List<Demo> demos = demoMapper.selectByExample(example); System.out.println(demos);
3、分页
DemoExample example = new DemoExample(); RowBounds rowBounds = new RowBounds(0, 10); List<Demo> demos = demoMapper.selectByExampleWithRowbounds(example,rowBounds); System.out.println(demos)
4、复杂的其它查询:
DemoExample example = new DemoExample(); //年龄在0~40之间 DemoExample.Criteria criteria = example.createCriteria(); criteria.andUserNameLike("%" + "l" + "%").andAgeBetween(0L,40L); //createAt不在【0~10】 DemoExample.Criteria criteria2 = example.createCriteria(); criteria2.andCreateAtNotBetween(0L,10L); example.or(criteria2); List<Demo> demos = demoMapper.selectByExample(example); System.out.println(demos);
二、方法列表
1、mapper文件:
2、example文件: