mybatis利用example文件进行异表字段模糊查询

21 篇文章 0 订阅

1.首先建立一个po表


po表中 添加不是本表的数据

例如

public class UserInfoPo extends UserInfo {


    private String deptId;//部门id


    private String uid;//用户id


    private String dept;//部门


    private String position;//部门的职位


public String getDeptId() {
return deptId;
}

}


在example文件中添加 模糊查询用到的字段


public class UserInfoExample {

private String position;//部门的职位

}


在xml文件中

映射字段

<resultMap id="BaseResultMap1" type="com.integral.po.UserInfoPo" extends="BaseResultMap">
 
  <result column="uid" property="uid" jdbcType="VARCHAR" />
  <result column="dept" property="dept" jdbcType="VARCHAR" />
  <result column="position" property="position" jdbcType="VARCHAR"/>
 
  </resultMap>


并且写出sql


<select id="selectUserByExample" resultMap="BaseResultMap1" parameterType="com.integral.entity.UserInfoExample" >
    select u.*,d.* from t_userinfo u,t_user_dept d
    <!-- <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if> -->
    where u.id = d.uid 
    <if test="position != null" > <!--此处的position 与 example和po中的字段一致--> 
        and d.position like #{position,jdbcType=VARCHAR}

<!--d.position  是字段名字 -->
    </if>    
   
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>


在mapper文件中


写出方法:

/**
     * 根据部门的模糊查询获取所有用户
    * @description:     
    * @author:xuzn   
    * @date:2017-2-13 上午9:41:52      
    * @modify:
    * @param example
    * @return   
    * @version:    
    *
     */
    List<UserInfoPo> selectUserByExample(UserInfoExample example);



在serviceImpl中  方法如下:


/**
* 模糊查询获取用户
*/
@Override
public List<UserInfoPo> selectUserByExample(UserInfoPo po) {
UserInfoExample example = new UserInfoExample();
Criteria criteria = example.createCriteria();

//判断 字段是否为空    由po获取传过来的数据
if(StringUtils.hasText(po.getPosition())){

//example文件中 将 position赋值
example.setPosition("%"+po.getPosition()+"%");
}

List<UserInfoPo> list = userInfoMapper.selectUserByExample(example);
if (list.size()>0) {
return list;
}else {
return null;
}
}



controller层 方法 如下:


@RequestMapping("selectUserByExample")
@ResponseBody
public LBResult selectUserByExample(UserInfoPo po,
HttpServletRequest request,
HttpServletResponse response) {

List<UserInfoPo> list = userInfoService.selectUserByExample(po);
try {
if (list.size()>0) {
return LBResult.build(400, "成功", list);
}else {
return LBResult.build(404, "失败");
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
return LBResult.build(500, ExceptionUtil.getStackTrace(e));
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的Terry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值