🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 1. SQL中的前缀和后缀 `<trim>`标签 > 1. prefix: > 在trim标签内sql语句加上前缀。 > 2. suffix: > 在trim标签内sql语句加上后缀。 > 3. suffixOverrides: > 指定去除多余的后缀内容,如:suffixOverrides=",",去除trim标签内sql语句多余的后缀","。 > 4. prefixOverrides: > 指定去除多余的前缀内容 > ~~~ <select id="listMenu" resultType="net.aexit.inner.aexithome.common.model.SysMenu"> SELECT distinct m.* FROM sys_menu m LEFT JOIN sys_role_menu rm ON rm.menu_id = m.id // 加了前缀WHERE,去掉前后缀的AND或OR <trim prefix="WHERE" prefixOverrides="AND|OR" suffixOverrides="AND|OR"> <if test="roleId !=null and roleId != '' " > AND rm.role_id = #{roleId} </if> <if test="menuName !=null and menuName != '' " > AND name LIKE CONCAT('%',#{menuName},'%') </if> <if test="level !=null and level != '' " > AND level = #{level} </if> </trim> ORDER BY m.sort </select> ~~~ ## 2. 参数非空 查询用户的检车记录 * 首先默认查询该用户的所有的检车记录 ![](https://box.kancloud.cn/3f1be045b7d80fa31e4abcfddf598550_537x506.png) * 提供根据车牌模糊搜索自己的检车记录 ![](https://box.kancloud.cn/da631623284d86c7515c2aa579c09ced_670x457.png) * 检测记录表 某一辆车的检车记录(车牌号) ![](https://box.kancloud.cn/d3e2e44ea10c65285fe97bdb179d592b_816x445.png) * 用户的车辆信息(用户id和车牌号) ![](https://box.kancloud.cn/53aa0e6eb644b67229e19d297e2731ca_881x437.png) 1. controller ~~~ /** * 查询用户检测记录 * @param userId 用户编号 * @param vehicleNo 车牌号(可以为空,此时查询用户所有车辆) * @return */ @RequestMapping(value = "/get_recored_byuserid",method = RequestMethod.GET) @ResponseBody public List<EdsDetectionRecord> getRecordByUserId(@RequestParam String userId, @RequestParam(required = false)String vehicleNo){ return bizService.getByUserId(userId,vehicleNo); } ~~~ 2. service ~~~ /** * 获取检测记录列表 * @param userId 用户id * @return */ public List<EdsDetectionRecord> getByUserId(String userId,String vehicleNo){ return edsDetectionRecordMapper.selectByUserId(userId,vehicleNo); } ~~~ 3. mapper ~~~ List<EdsDetectionRecord> selectByUserId(@Param("userId")String userId,@Param("vehicleNo")String vehicleNo); ~~~ 4. xml ~~~ <select id="selectByUserId" parameterType="java.lang.String" resultMap="BaseResultMap"> SELECT r.* FROM eds_detection_record r INNER JOIN app_vehicle a ON r.vehicle_no = a.vehicle_no AND a.user_id=#{userId,jdbcType=VARCHAR} <if test="vehicleNo != null"> AND r.vehicle_no LIKE "%"#{vehicleNo,jdbcType=VARCHAR}"%" </if> ORDER BY r.detect_date DESC ; </select> ~~~ * 如果vehicleNo参数非空,则将下边的语句加入SQL ~~~ <if test="vehicleNo != null"> AND r.vehicle_no LIKE "%"#{vehicleNo,jdbcType=VARCHAR}"%" </if> ~~~ 如果vehicleNo参数非空,相当于 ~~~ SELECT r.* FROM eds_detection_record r INNER JOIN app_vehicle a ON r.vehicle_no = a.vehicle_no AND a.user_id=#{userId,jdbcType=VARCHAR} and r.vehicle_no LIKE ‘%vehicleNo%’ ~~~