MyBatis-Plus初体验--痛并快乐

感觉还行,确实能省不少时间。就是有点丑,有待进一步美化。对于一个抵触Lambda的码农来说,看在能省时间的份上,就捏着鼻子先用着吧。哈哈。
附上2个具体场景:

1:多对多关连查询最终解决方案:

#用于查询用户时,设置部门id条件的sql片段
user.department.userInDept=id in (select user_id from sys_user_department where dept_id={0})
#用于查询用户时,设置岗位id条件的sql片段
user.position.userInPosition=id in (select user_id from sys_user_position where position_id={0})

//读属性

@Component
@PropertySource("mapper/apply-where.properties")
public class SqlSegmentProperties {

	public static String userInDept;

	public static String userInPosition;

	@Value("${user.department.userInDept}")
	public void setUserIdIn(String userIdIn) {
		SqlSegmentProperties.userInDept = userIdIn;
	}

	@Value("${user.position.userInPosition}")
	public void setUserInPosition(String userInPosition) {
		SqlSegmentProperties.userInPosition = userInPosition;
	}
}

查询时用query.apply设置多对多条件

	public Wrapper<User> buildQuery() {
		QueryWrapper<User> query = Wrappers.<User>query();
		query.setEntity(this);
		// 根据机构查询
		query.apply(null != departmentId, userInDept, departmentId);
		// 根据岗位查询
		query.apply(null != positionId, userInPosition, positionId);
		// 排序
		query.orderByDesc("gmt_create");
		return query;
	}

//对Wrapper这玩意有强烈的抵制心理,代码太丑了有木有。还是尽可能抽抽吧- ,-

	/** 用户 **/
	static LambdaQueryWrapper<User> userQuery() {
		return Wrappers.<User>lambdaQuery();
	}

	/** 部门 **/
	static LambdaQueryWrapper<Department> departmentQuery() {
		return Wrappers.<Department>lambdaQuery();
	}

	/** 用户-部门 **/
	static LambdaQueryWrapper<UserDepartment> userDepartmentQuery() {
		return Wrappers.<UserDepartment>lambdaQuery();
	}

	static LambdaQueryWrapper<UserDepartment> userDepartmentQuery(Long userId) {
		return userDepartment().eq(UserDepartment::getUserId, userId);
	}

相当不爽的代码–>Wrappers.lambdaUpdate().set(ReportOutTempEntity::getStatus, “3”).eq(ReportOutTempEntity::getId, paramvo.getReportOutTempId()).点到昏天暗地…

你可能感兴趣的:(mybatis,java)