今天主要做了测试用户模块的基础数据的管理,遇到了一个比较头大的问题,用户属性存的都是id,但是返给前端需要转成相应的string类型,然后还有各种非必传参数的验证,一下子来个这么复杂的SQL
,特别在是在mybatis
的XML
文件中配置,着实让我学习了一阵子。分享一下学习成果。
需求图如下,按照几种筛选条件获取当前用户创建的测试用户列表:
接口文档如下:
里面都是必传参数,string类型默认 "",number默认0表示全部。
下面是我的XML文件内容:
<sql id="table">
qa_test_user
</sql>
<sql id="user_status">
qa_user_status_name
</sql>
<sql id="env">
family_base_env
</sql>
<sql id="user_role">
qa_role_name
</sql>
<select id="findUsers" parameterType="com.okay.family.common.bean.testuser.SearchUserBean"
resultType="com.okay.family.common.bean.testuser.TestUserBean">
select tu.id,tu.descc,tu.user,tu.phone,tu.password,tu.create_time,us.name status,env.name env,ur.name role from
<include refid="table"/>
tu
left join
<include refid="user_status"/>
us on tu.status = us.status
left join
<include refid="env"/>
env on tu.envId = env.id
left join
<include refid="user_role"/>
ur on tu.roleId = ur.roleId
where tu.uid = #{uid}
<if test="status != 0">
and tu.status = #{status}
</if>
<if test="envId != 0">
and tu.envId = #{envId}
</if>
<if test="roleId != 0">
and tu.roleId = #{roleId}
</if>
<choose>
<when test="type == 0 and query != null and query != ''">
and tu.user = #{query}
</when>
<when test="type == 1 and query != null and query != ''">
and tu.phone = #{query}
</when>
<when test="type == 2 and query != null and query != ''">
and tu.id = #{query}
</when>
</choose>
</select>
下面是Java bean的内容:
package com.okay.family.common.bean.testuser
import com.okay.family.common.basedata.OkayConstant
import com.okay.family.fun.base.bean.AbstractBean
import org.hibernate.validator.constraints.Range
import javax.validation.constraints.Min
class SearchUserBean extends AbstractBean {
private static final long serialVersionUID = 894894891651651L;
int uid
@Range(min = 0L, max = OkayConstant.ENV, message = "环境参数错误")
int envId
@Min(value = 1L)
int pageNum
@Range(min = 5L, max = 20L, message = "每页显示数量设置错误")
int pageSize
@Range(min = 0L, max = 10L, message = "用户身份参数错误")
int roleId
@Range(min = 0L, max = OkayConstant.USER_STATUS, message = "用户状态参数错误")
int status
String query
@Range(min = 0l, max = 2L, message = "搜索类型出错!0账号1手机号2用户id")
int type
}
总算是解决了这个难题,剩下的继续搬砖,终于体会到开发的难处了:牵一发而动全身,改个属性名,乱七八糟的地方修改不下五处,但愿提测时候BUG能少点。
Fhaohaizi@163.com
。