通过map可以避免创建实体


并不是说不要创建实体,而是有时并没必要创建

例如:从磁盘上读取文件在页面上显示,磁盘上得文件名字是201101.xls,那么在页面上显示时,需要显示2011年1月


我们做法是:先读取出文件保存在list中,但是文件名转换如何做呢?

通常可以创建个file实体,里面两个字段,name_file,name_show


但是我们还可以通过map来实现


原理,采用json的数据格式 [name_file:201101.xls,name_show:2011年1月]

class BusinessTypeSumReportAction{

public static List getFiles(String dirname) throws Exception {
        List file_names = null;
        
        File dir = new File(dirname);
        if (dir.exists()) {
            file_names = new ArrayList();
            File[] files = dir.listFiles();
            // 排序
            Arrays.sort(files,
                    new BusinessTypeSumReportAction.CompratorByLastModified());
            for (int i = 0; i < files.length; i++) {

                String filename = files[i].getName();
                if (filename.matches("^.+\\.xls$")&&filename.matches("2[0-9]{5}+\\.xls$")) {
                    Map<String, String> map = new HashMap<String, String>();
                    String year = filename.substring(0, 4)+"年";
                    String month = filename.substring(4,6)+"月";
                    if(month.startsWith("0"))month = month.substring(1,3);
                    map.put("name_file", files[i].getName());
                    map.put("name_show", year+month);
                    file_names.add(map);
                }
            }

        } else {
            System.out.println("该目录没有任何文件信息!");
        }
        return file_names;

    }


public static List<Map<String, List>> getFiles2(String dirname)
            throws Exception {
        List file_names = null;
        List<Map<String, List>> listMap = new ArrayList<Map<String, List>>();
        Map<String, List> map = new HashMap<String, List>();
        File dir = new File(dirname);
        String tempyear = "";
        String year = "";
        if (dir.exists()) {
            file_names = new ArrayList();
            File[] files = dir.listFiles();
            // 排序
            Arrays.sort(files,
                    new BusinessTypeSumReportAction.CompratorByLastModified());
            for (int i = 0; i < files.length; i++) {

                // 取出文件名年份,同一年份的封装到一个list中,数据结构为json类似
                String filename = files[i].getName();
                if (filename.matches("^.+\\.xls$")) {

                    year = filename.substring(0, 5);

                    file_names.add(files[i].getName());
                    if (!tempyear.equals(year)) {
                        map.put(year, file_names);
                    }
                    tempyear = year;
                }
            }
        } else {
            System.out.println("该目录没有任何文件信息!");
        }
        return file_names;
    }

    private static String format(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    private static class CompratorByLastModified implements Comparator<File> {
        public int compare(File f1, File f2) {
            long diff = f1.lastModified() - f2.lastModified();
            if (diff > 0)
                return 1;
            else if (diff == 0)
                return 0;
            else
                return -1;
        }

        public boolean equals(Object obj) {
            return true;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapStruct是一个用于Java中实体与模型间属性转换的优秀工具。当我们在开发中,经常会遇到实体类与模型类之间的属性不匹配的情况。对于这种情况,我们可以使用MapStruct来简化转换过程。 首先,我们需要在项目中添加MapStruct的依赖。然后,在实体类和模型类中使用注解来标识需要进行属性转换的字段。例如,我们可以在实体类的字段上加上`@Mapping`注解,指定需要转换的目标字段,以及在模型类的字段上使用`@Mapping(target = "sourceField")`注解来指定需要从哪个源字段进行转换。 接下来,我们需要创建一个转换器的接口,并使用`@Mapper(componentModel = "spring")`注解来标识该接口为MapStruct的映射器。然后,我们可以在接口中定义转换方法,并使用`@Mapping`注解来指定具体的属性转换规则。 在使用MapStruct进行属性转换时,只需通过调用映射器接口中的方法即可完成转换。MapStruct会根据注解配置自动执行转换过程,将实体类中不匹配的属性值转换到模型类中相应的字段上。 另外,为了提高转换效率,我们可以在编译过程中使用MapStruct的编译器插件。该插件可以生成对应的转换器实现类,避免了通过反射等方式进行转换,大大提高了性能。 总的来说,使用MapStruct可以方便快捷地处理Java中实体与模型间属性不匹配的转换问题。通过注解配置和自动生成的转换器实现类,可以避免手动编写繁琐的属性赋值代码,提高开发效率同时保证转换过程的准确性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值