mybatisqueryWrapper、updateWrapper⽤法
/**
* <p>
* 根据根据 entity 条件,删除记录,QueryWrapper实体对象封装操作类(可以为 null)
* 下⽅获取到queryWrapper后删除的查询条件为name字段为null的and年龄⼤于等于12的and email字段不为null的
* 同理写法条件添加的⽅式就不做过多介绍了。
* </p>
*/
@Test
public void delete() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper
.isNull("name")
.ge("age", 12)
.isNotNull("email");
int delete = mapper.delete(queryWrapper);
System.out.println("delete return count = " + delete);
System.out.println("delete return count = " + delete);
}
/**
* <p>
* 根据 entity 条件,查询⼀条记录,
* 这⾥和上⽅删除构造条件⼀样,只是seletOne返回的是⼀条实体记录,当出现多条时会报错 * </p>
*/
@Test
public void selectOne() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "lqf");
User user = mapper.selectOne(queryWrapper);
System.out.println(user);
}
/**
* <p>
* 根据 Wrapper 条件,查询总记录数
* </p>
*
* @param queryWrapper 实体对象
*/
@Test
public void selectCount() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", "lqf");
Integer count = mapper.selectCount(queryWrapper);
System.out.println(count);
}
/
**
* <p>
* 根据 entity 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)为null查询全部
*/
@Test
public void selectList() {
List<User> list = mapper.selectList(null);
System.out.println(list);
}
/**
* <p>
* 根据 Wrapper 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
@Test
public void selectMaps() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNotNull("name");
List<Map<String, Object>> maps = mapper.selectMaps(queryWrapper);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
}
/**
* 打印结果
* {name=lqf, id=1046282328366391406, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391407, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391408, age=12, email=lqf@163, status=false} * {name=lqf, id=10462823
28366391409, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391410, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391411, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391412, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391413, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391414, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391415, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391416, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391417, age=12, email=lqf@163, status=false} * {name=lqf, id=1046282328366391418, age=12, email=lqf@163, status=false} * json类型的键值对模式
*/
/**
* <p>
* 根据 entity 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
@Test
public void selectPage() {
Page<User> page = new Page<>(1, 5);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<User> userIPage = mapper.selectPage(page, queryWrapper);
System.out.println(userIPage);
}
/
**
* 打印结果
* ==> Preparing: SELECT COUNT(1) FROM user
* ==> Parameters:
* <== Columns: COUNT(1)
* <== Row: 100
* ==> Preparing: SELECT id,name,age,email,status FROM user LIMIT 0,5
* ==> Parameters:
* <== Columns: id, name, age, email, status
* <== Row: 1046282328366391319, lqf, 12, lqf@163, 0
* <== Row: 1046282328366391320, lqf, 12, lqf@163, 0
* <== Row: 1046282328366391321, lqf, 12, lqf@163, 0
* <== Row: 1046282328366391322, lqf, 12, lqf@163, 0
* <== Row: 1046282328366391323, lqf, 12, lqf@163, 0
* <== Total: 5
*
*
* 这⾥需要在项⽬中加⼊分页插件
* @Bean
* public PaginationInterceptor paginationInterceptor() {
* return new PaginationInterceptor();
* }
*/
/**
* <p>
* 根据 Wrapper 条件,查询全部记录(并翻页)
* 根据 Wrapper 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
@Test
public void selectMapsPage() {
Page<User> page = new Page<>(1, 5);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
IPage<Map<String, Object>> mapIPage = mapper.selectMapsPage(page, queryWrapper);
System.out.println(mapIPage);
}
/**
* 和上个分页同理只是返回类型不同
*/
/**
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param entity 实体对象 (set 条件值,不能为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,⾥⾯的 entity ⽤于⽣成 where 语句)
*/
@Test
public void update() {
//修改值
User user = new User();
user.setStatus(true);
user.setName("zhangsan");
//修改条件s
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name", "lqf");
int update = mapper.update(user, userUpdateWrapper);
System.out.println(update);
}
/**
* 打印结果
* ==> Preparing: UPDATE user SET name=?, status=? WHERE name = ?isnull的用法
* ==> Parameters: zhangsan(String), true(Boolean), lqf(String)
* <== Updates: 100
* Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@56a4f272]
* 100
* 2018-10-02 15:08:03.928 INFO 7972 --- [ Thread-2] o.s.GenericWebApplicationContext : Closing org.t.support.Gene * 2018-10-02 15:08:03.937 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 -
* 2018-10-02 15:08:04.053 INFO 7972 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
*
* Process finished with exit code 0
*/
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论