日期: 2024 年 10 月 31 日

  • mybatis中foreach使用

    一、foreach 属性使用

    <foreach collection="list" index="index" item="mchntCd" open="(" close=")" separator=",">
       #{mchntCd}
    </foreach>
    
    1. item:  集合中元素迭代时的别名,该参数为必选,通过别名进行取值
    2. index:在list和数组中,index是元素的序号,在map中,index是元素的key,非必填
    3. open: foreach代码的开始符号,一般是(和close=”)”合用。常用在in(),values()时。非必填
    4. separator:元素之间的分隔符,例如在in()的时候,separator=”,”会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。非必填
    5. close: foreach代码的关闭符号,一般是)和open=”(“合用。常用在in(),values()时。非必填
    6. collection: 要做foreach的对象,作为入参
      1. 传入是集合,也就是接口里面用的  List<String> nameList    那么 使用 collection = “list”
      2. 传入的是数组,接口里面 String[] namestrs ,那么 使用 collection = “array”
      3. 如果传入的是一个实体bean,实体bean里面有一个属性为 list<String> ids 那么collection = “ids ”,如果实体bean中对应ids是一个对象,ids 里面有一个属性为 List<Stirng> usernames,那么collection = “ids.usernames”

    二、代码使用

            1、实体类 list<String>  mchntCds
     mapper接口  —》 list方式
    int queryDiscDerateCount
    (List<String> mchntCds);
    mapper映射xml
    1. <select id=“queryDiscDerateCount” resultType=“Integer”>
    2. select count (*) from t_mchnt_disc_config where mchnt_cd in
    3. <foreach collection=“list” index=“index” item=“mchntCd” open=“(“ close=“)” separator=“,”>
    4. #{mchntCd}
    5. </foreach>
    6. </select>
     
       2、实体类 list<User>  userlist,实体类中有 list 属性
    实体类:
    1. @Data
    2. public class MchntDiscDerateDto {
    3. private String mchntCd = “”;
    4. }
    mapper接口
    
    List<TMchntDerateInfoDto> getDiscDerateList
    (List<MchntDiscDerateDto> discDto);
      mapper映射xml:
    1. <select id=“getDiscDerateList” parameterType=“MchntDiscDerateDto” resultType=“TMchntDerateInfoDto”>
    2. select
    3. t_mchnt_disc_derate_config
    4. where
    5. mchnt_cd in
    6. <foreach collection=“list” index=“index” item=“user” open=“(“ close=“)” separator=“,”>
    7. #{user.mchntCd}
    8. </foreach>
    9. </select>
     
    3、数组  String[] params      用 array
    mapper 接口  
    int queryDiscDerateCount
    (String[] mchntCds);
      mapper映射xml:
    1. <select id=“queryDiscDerateCount” resultType=“Integer”>
    2. select count (*) from t_mchnt_disc_derate_config where mchnt_cd in
    3. <foreach collection=“array” index=“index” item=“mchntCd” open=“(“ close=“)” separator=“,”>
    4. #{mchntCd}
    5. </foreach>
    6. </select>
     
    4、传入的参数是实体类,并且实体中包含数组和集合
    实体类:
    1. @Data
    2. public class UserVo {
    3. private Long id;
    4. private Long supplierId;
    5. private Long[] ids;
    6. private List<Long> clientIdList;
    7. }
    mapper接口
    List<UserVo> queryList
    (UserVo select);
    mapper映射文件xml
    1. <select id=“queryList” resultType=“UserVo” parameterType=“UserVo”>
    2. select *
    3. from bms_bills_memo
    4. <where>
    5. and id in
    6. <foreach collection=“ids” open=“(“ close=“)” item=“id” separator=“,”>
    7. #{id}
    8. </foreach>
    9. and
    10. client_id in
    11. <foreach collection=“clientIdList” separator=“,” item=“detail” open=“(“ close=“)” >
    12. #{detail}
    13. </foreach>
    14. </where>
    15. </select>
     
    5、传入多个list或者array,不使用实体进行封装。用注解@Params, collection使用到Param中定义的别名
    mapper接口
    List<UserVo> queryList
    (@Param("idArray") Long[] array, @Param("clientIdList") List<Long> list);
    mapper映射文件xml
    1. <select id=“queryList” resultType=“UserVo”>
    2. select *
    3. from t_user_inf
    4. <where>
    5. and id in
    6. <foreach collection=“idArray” open=“(“ close=“)” item=“id” separator=“,”>
    7. #{id}
    8. </foreach>
    9. and
    10. client_id in
    11. <foreach collection=“clientIdList” separator=“,” item=“detail” open=“(“ close=“)” >
    12. #{detail}
    13. </foreach>
    14. </where>
    15. </select>

     

    6、map参数
    当我们传入的参数为 Map<String,Objject>的时候,那么能不能正常使用 foreach呢,答案是肯定的,因为对象也是类似于map结构啊
    1. /**
    2. * map 获取数据
    3. * @param map
    4. * @return
    5. */
    6. List<SysUser> getUserByIds (Map<String,Object> map);
    1. // xml
    2. <select id=“getUserByIds” resultMap=“BaseResultMap”>
    3. select
    4. <include refid=“Base_Column_List” />
    5. from t_sys_user
    6. where status = #{status}
    7. and id in
    8. <foreach collection=“ids” item=“id” open=“(“ close=“)” separator=“,”>
    9. #{id}
    10. </foreach>
    11. </select>

    实际调用:

    1. @Test
    2. public void testMapUsers() {
    3. Map<String,Object> params = new HashMap<>();
    4. params.put(“status”, “1”);
    5. params.put(“ids”, Arrays.asList(1,2,3,4,5,6,7,8));
    6. List<SysUser> all = sysUserDao.getUserByIds(params);
    7. try {
    8. System.out.println(new JsonMapper().writeValueAsString(all));
    9. } catch (JsonProcessingException e) {
    10. e.printStackTrace();
    11. }
    12. }

    调用结果:

    三、总结

    1、mapper 接口中添加 @Param注解的场合,list,array将会失效;

    2、使用了 @Param注解 collection的内容需要使用Param中的别名来指定你需要用到的list或者array

  • 413 Request Entity Too Large

    最近我们的网站服务平台上传apk(应用程序)的时候 上传60M的app的时候 报错nginx

    413 Request Entity Too Large

    经过排查修改nginx配置

    这是最简单的一个做法,着报错原因是nginx不允许上传配置过大的文件,那么件把nginx的上传大小配置调高就好。

    1、打开nginx主配置文件nginx.conf, 找到http{}段并修改以下内容:

    http {
        include       mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 100m;
        limit_conn_zone $binary_remote_addr zone=one:32k;
        sendfile        on;
        tcp_nopush     on;
        keepalive_timeout  60;
        tcp_nodelay on;
        gzip  on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types       text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
        #include default.conf;
        include vhost/*.conf;
    }
    
    

    client_max_body_size 100m;

    当中的100修改成你需要的允许文件大小。

    2、修改后,测试nginx配置是否正确

    重启nginx

    我们的项目是以php运行的,这个大小client_max_body_size要和php.ini中的如下值的最大值差不多或者稍大,这样就不会因为提交数据大小不一致出现错误。

    post_max_size = 100M
    
    upload_max_filesize = 100M

    当中的100M修改成你需要的允许文件大小。把当中的100M修改成你第一步设置的大小。

    我们修改完php.ini文件后 重启一下php 然后我们再上传60M的APP时ok 没问题

  • IDEA中JS文件中文乱码

    521f7e8e3ee6fc0e8776946b15e1102f e33dce9c87ad4a54872f1ecdb9d4b35b

    开始我以为是导入的js文件没有设置编码的原因然后我加上charset=UTF-8,但是没有起作用,后我又以为是jsp文件编码的原因,于是我又加上了pageEncoding="utf-8",任然没有起作用。

    几次反思后我觉得可能问题出现在我用的开发工具IDEA上,网上查阅资料后找到问题了。

    一、设置全局和项目编码

    222b878f23f59693e1b35e09ab4e7765 83b8c1c582d3e083ec804ebd1a262967

    二、设置Tomcat启动项目用到的VM options

    f48c268e7f043df8b7c1c6259eafd5af 83c394bbefcc59f91102305b4e4e3654

    修改完后重启项目