开发者

mybatis使用foreach踩坑及解决

开发者 https://www.devze.com 2024-08-20 10:43 出处:网络 作者: 大佬喝可乐丶
目录myBATis使用foreach踩坑记录XML文件中foreach属性的选择controller层传参说明postman截图总结mybatis使用foreach踩坑记录
目录
  • myBATis使用foreach踩坑记录
    • XML文件中foreach属性的选择
    • controller层传参说明
    • postman截图
  • 总结

    mybatis使用foreach踩坑记录

    在使用fireach是最关键的是collection属性

    dao层

    单参数List和多参数的list

        /*通过表名(日期)查询*/
        List<WagesInfoVo> selectWagesBySheet(List<String> sheetNameList);
    
        /*通过用户名+表名(日期)查询*/
        List<WagesInfoVo> selectByPerson(@Param("personNumber") String personNumber,
                                         @Param(http://www.devze.com"sheetNameList") List<String> sheetNameList);

    xml文件中foreach属性的选择

    • 单参数的collection=“list”
    • 多参数的collection=“sheetNameList”,sheetNameList为传入的参数
     <select id="selectWagesBySheet" parameterType="Java.lang.String"
                resultType="com.zl.wagesmanage.vo.wages.WagesInfoVo">
            select person_number as personNumber,user_name as userName,
            account_number as accountNumber,wage as wage,other_money as otherMoney,reward as reward,
            should as should,old as old,medical as medical,lose_job as loseJob,trade_union as tradeUnion,
            housing_fund as housingFund,take_off as takeOff,total as total,sheet_name as sheetName
            from tt_excel_wages
            where sheet_name in
            <foreach collection="list" index="index" item="sheetNameList"
                     open="(" separat编程客栈or="," close=")">
                #{sheetNameList}
            </foreach>
        </select>
    
        <select id="selectByPerson" parameterType="java.lang.String"
                resultType="com.zl.wagesmanage.vo.wages.WagesInfoVo">
            select person_number as personNumber,user_name as userName,
            account_number as accountNumber,wage as wage,other_money as otherMoney,reward as reward,
            should as should,old as old,medical as medical,lose_job as loseJob,trade_union as tradeUnion,
            housing_fund as housingFund,take_off as takeOff,total as total,sheet_name as sheetName
            from tt_excel_wages
            where person_number=#{personNumber}
            and sheet_name in
            <foreaandroidch collection="sheetNameList" index="index" item="sheetNameList"
                     open="(" separator="," close=")">
                #{sheetNameList}
            </foreach>
        </select>

    controller层传参说明

    这里我都是使用@RequestParam进行传参,列举了get请求和post请求

        /*通过excel的表名进行查询*/
        @GetMapping(value = "getWages")
        @ResponseBody
        public ResponseVO getWages(@RequestParam("sheetNames")List<String> sheetNames){
            try {
                WagesRequestVo wagesRequestVo =new WagesRequestVo();
                //获取数据
                List<WagesInfoVo> wagesInfoVos = wagesServer.getWagesBySheet(sheetNames);
    
                wagesRequestVo.setWagesInfoVoList(wagesInfoVos);
                if (wagesInfoVos==null){
                    return ResponseVO.appFail("查询失败");
                }
                return ResponseVO.success(wagesRequestVo);
            }catch (Exception e){
                return ResponseVO.serviceFail(e.getMessage());
            }
        }
    
        /*通过用户名和表名进行查询*/
        @PostMapping("getWagesByNumber")
        @ResponseBody
        public  ResponseVO getWagesByNumber(@RequestParam("personNumber")String personNumber,
                                            @RequestParam("sheetName[]")List<String> sheetName){
            try {
                WagesRequestVo wagesRequestVo =new WagesRequestVo();
                //获取数据
                List<WagesInfoVo> wagesInfoVos = wagesServer.getWagesByPerson(personNumber,sheetName);
    
                wagesRequestVo.setWagesInfoVoList(wagesInfoVos);
                if (wagesInfoVos==null){
                    returandroidn ResponseVO.appFail("查询失败");
                }
                return ResponseVO.success(wagesRequestVo);
            }catch (Exception e){
                return ResponseVO.serviceFail(e.getMessage());
            }
        }

    postman截图

    • get请求传参方式:

    mybatis使用foreach踩坑及解决

    • post传参方式:

    mybatis使用foreach踩坑及解决

    总结

     以上为个人经验BtfFm,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消