Mybatis plus插件入门实践

注解:
@TableName(""):默认实体类的类名对应于数据库的表名,驼峰类名会自动映射成数据库以下划线的表名;如果不能直接使用这种方式映射,需该注解来指定表名;
@TableId:默认实体类的主键字段名称为id,如果不是此名称,需在类字段上使用该注解,表明该字段对应于数据库表的主键字段;
@TableFiled(""):默认实体类的字段名对应于数据库表的字段名是驼峰映射成下划线,如果不是则需要该注解去指定对应的数据库表字段名称;
@TableFiled(exist=false):默认实体类的字段与数据库表的字段是一一对应的,在某些需要实体类冗余字段时,可在实体类的字段上使用该注解,则操作数据库时忽略该实体类字段;
@TableField(condition=SqlCondition.LIKE):将该注解加载实体类的字段上,表示该字段使用Wrapper进行查询时使用的时模糊查询;(也可以对condition进行表达式赋值,具体可参照SqlCondition源代码文件)

条件查询
使用QueryWrapper
示例:
需求:查询名字是王姓或者年龄大于等于25,相同时按年龄降序,以主键id为升序的列表

public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	queryWrapper.likeRight("name", "王").or().ge("age", 25).orderByDesc("age").orderByAsc("id");
	List userList = userMapper.selectList( queryWrapper );

需求:查询名字时王姓并且(年龄小于40或邮箱不为空)
sql语法:name like '王%‘ and ( age < 40 or email is not null )

1.
public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	queryWrapper.likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
	List userList = userMapper.selectList( queryWrapper );

2. // Lambda表达式
	public void fun() {
		LambdaQueryWrapper lambdaQuery = Wrappers. lambdaQuery();
		lambdaQuery.likeRight(User::getName, "王").and( e -> e.lt(User::getAge, 40).or().isNotNull(User::getEmail));
		List userList = userMapper.selectList(lambdaQuery);

需求:查询时只返回某些数据的字段

1.
	public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	queryWrapper.select("name", "age").likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
	List userList = userMapper.selectList( queryWrapper );

2.
	public void fun() {
	QueryWrapper queryWrapper = ew QueryWrapper();
	queryWrapper.select(User.class, info -> !info.getColumn().equals("create_time") && !info.getColumn().equals("extra")).likeRight("name", "王").and(wq -> wq.lt("age", 40).or().isNotNull("email"));
	List userList = userMapper.selectList( queryWrapper );

需求:条件查询实体类中非空的字段

1.
public void fun() {
	QueryWrapper queryWrapper = ew QueryWrapper();
	queryWrapper.like(!StringUtils.isEmpty(name), "name", name).like(!StringUtils.isEmpty(dept), "dept", dept);
	List userList = userMapper.selectList( queryWrapper );

2. // 这种方式时完全匹配的实体类数据字段值
	// 如果需要在实体类上的某些字段进行模糊查询时,可以在实体类上加注解@TableFiled( condition = SqlCondition.LIKE )
	// 如果在SqlCondition中未找到想要的类型,可以直接对condition进行表达式复制,例如:@TableField( condition = "%s<#{%s}") 这是SqlCondition中没有的小于表达式语法
public void fun() {
	User searchUser = new User();
	searchUser.setName("王");
	QueryWrapper queryWrapper = ew QueryWrapper(searchUser).like("name", name);
	List userList = userMapper.selectList( queryWrapper );

需求:自定义Map(String, Object)去匹配数据

SQL语法: ... WHERE name = '王五' AND age IS NULL
public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	Map params = new HashMap();
	params.put("name", "王五");
	params.put("age", null);
	queryWrapper.allEq( params );
	List userList = userMapper.selectList( queryWrapper );

SQL语法: ... WHERE name = '王五' 
public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	Map params = new HashMap();
	params.put("name", "王五");
	params.put("age", null);
	queryWrapper.allEq( params, false );
	List userList = userMapper.selectList( queryWrapper );

SQL语法: ... WHERE name = '王五' AND age IS NULL
public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	Map params = new HashMap();
	params.put("name", "王五");
	params.put("age", null);
	queryWrapper.allEq( params );
	List userList = userMapper.selectList( queryWrapper );

需求:根据某字段分组,查询每组的平均年龄、最大年龄、最小年龄,且每组年龄总和小于300

SQL语法:SELECT AVG(age) avgAge, MAX(age) maxAge, MIN(age) minAge FROM tbl_name GROUP BY row_id HAVING SUM(age) < 300;
public void fun() {
	QueryWrapper queryWrapper = new QueryWrapper();
	queryWrapper.select("AVG(age) avgAge", "MAX(age) maxAge", "MIN(age) minAge").groupBy("row_id").having("SUM(age) < {0}", 300);
	List userList = userMapper.selectList( queryWrapper );

需求:分页查询

1.
	public void fun() {
		QueryWrapper queryWrapper = new QueryWrapper();
		Page page = new Page(1, 2);
		IPage iPageRes = userMapper.selectPage(page, queryWrapper);
		// iPageRes.getPages 总页数
		// iPageRes.getTotal 	总记录数
		List userList = iPageRes.getRecords();

2.
	public void fun() {
		QueryWrapper queryWrapper = new QueryWrapper();
		Page page = new Page(1, 2);
		IPage> iPageRes = userMapper.selectMapsPage(page, queryWrapper);
		// iPageRes.getPages 总页数
		// iPageRes.getTotal 	总记录数
		List> userList = iPageRes.getRecords();

更新

1.
	public void updateById() {
		User user = new User();
		user.setId("111");
		user.setName("testName");
		int rows = userMapper.updateById(user);	// 返回的整型数据代表影响的数据库记录数
	}

2. 
	public void updateByCondition() {
		UpdageWrapper updateWrapper = new UpdateWrapper();
		updateWrapper.eq("age", 25);
		User user = new User();
		user.setId(111);
		user.setName("testName");
		int rows = userMapper.update(user, updateWrapper);
	}

3. // 不创建类实例,直接设置值
	public void updateByCondition2() {
		UpdageWrapper updateWrapper = new UpdateWrapper();
		updateWrapper.eq("id", 111).set("name", "testName");
		int rows = userMapper.update(null, updateWrapper);
	}

4. // lambda表达式更新操作
	public void updateByCondition2() {
		LambdaUpdageWrapper lambdaUpdate = new LambdaUpdateWrapper();
		lambdaUpdateWrapper.eq(User::getId, 111).set(User::getName, "testName");
		int rows = userMapper.update(null, lambdaUpdate);
	}

5. // lambda链式表达式更新操作
	public void updateByCondition2() {
		boolean updateFlag = new LambdaUpdateChainWrapper(userMapper).eq(User::getId, 111).set(User::getName, "testName").update();
	}

删除

1.
	public void deleteById() {
		int rows = userMapper.deleteById(111);
		}

2.
	public void deleteByMap() {
		Map searchMap = new HashMap<>();
		searchMap.put("name", "tesName")
		int rows = userMapper.deleteByMap(searchMap);
		}

3. //	批量删除
	public void deleteByIds() {
		int rows = userMapper.deleteBatchIds(Arrays.asList(111, 222));
		}

4. // 带删除条件的操作
	public void deleteByWrapper() {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.eq("id", 111);
		int rows = userMapper.delete(queryWrapper);
		}

5. // 带删除条件的lambda操作
	public void deleteByWrapper2() {
		LambdaQueryWrapper lambdaWrapper = new LambdaQueryWrapper();
		// or LambdaQueryWrapper lambdaWrapper = Wrappers.lambdaQuery();
		lambdaWrapper .eq(User::getId, 111);
		int rows = userMapper.delete(lambdaWrapper );
		}

插入
因为插入就一个函数很简单,在此就不做赘述了。

注:本文内容参照引用了视频教程[MyBatis-Plus],大家可以看看(https://www.imooc.com/video/19490)

你可能感兴趣的:(Spring,boot,数据库)