@Configuration @MapperScan({ "com.example.demo.mapper" }) @EnableTransactionManagement public class DB {
/** * 分页插件 */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } }
6.开始使用
6.1 自带crud操作
6.2 条件构造器
介绍几种常用的
1. eq 同 =
1 2 3
QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>(); queryWrapper.eq("name", "zjx").eq("TS", "2019-02-22"); Form form = this.getOne(queryWrapper);
此处sql等同于
select * from Form where name = “zjx” and ts = “2019-02-22”;
2. ne 同 <>
3. ge gt le lt
ge 同 >=
gt 同 >
le 同 <=
lt 同 <
4. LIKE 同 ‘%值%’
1 2 3
QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>(); queryWrapper.like("name", "zjx"); Form form = this.getOne(queryWrapper);
此处sql等同于
select * from Form where name like “%zjx%” ;
5. in 同 in
1 2 3
QueryWrapper<Form> queryWrapper = new QueryWrapper<Form>(); queryWrapper.in("id", 1,2,3); Form form = this.getOne(queryWrapper);
此处sql等同于
select * from Form where id in (1,2,3);
6. orderByAsc.orderByDesc,groupBy
orderByAsc(“id”, “name”) —> order by id ASC,name ASC orderByDesc(“id”, “name”) —> order by id DESC,name DESC groupBy(“id”, “name”) —> group by id,name
6.3 分页
mapper中使用注解形式
1 2 3
//强调 sql语句千万不要加分号; @Select("select * from form where name = #{name}") public List<Form> getList(String name, IPage<Form> page )
serviceImpl中
1 2 3 4 5 6 7
@Override public IPage<Form> getList(String name, int pageNum, int pageSize) { IPage<Form> page = new Page<Form>(pageNum, pageSize); List<Form> iList = FormMapper.getList(name, page); page.setRecords(iList); return page; }