java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis使用foreach踩坑

mybatis使用foreach踩坑及解决

作者:大佬喝可乐丶

这篇文章主要介绍了mybatis使用foreach踩坑及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

mybatis使用foreach踩坑记录

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

dao层

单参数List和多参数的list

    /*通过表名(日期)查询*/
    List<WagesInfoVo> selectWagesBySheet(List<String> sheetNameList);

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

xml文件中foreach属性的选择

 <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="(" separator="," 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
        <foreach 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){
                return ResponseVO.appFail("查询失败");
            }
            return ResponseVO.success(wagesRequestVo);
        }catch (Exception e){
            return ResponseVO.serviceFail(e.getMessage());
        }
    }

postman截图

总结

 以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文