delete基于java文件名的数据库表

s5a0g9ez  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(280)

我有以下代码 class IImporter 到现在为止还不错。我每天都会收到很多zip文件,其中包括 'EQUFULLFILE' 以及 'NONEQUFULLFILE' 在一个目录中,我试图从这个目录中读取文件并在数据库表中处理它 'EquData' 或者 'NonEquData' 相应地。在处理之前,我只是试图删除这个数据库表中的所有数据。
但我发现我的代码有问题。有时当我在目录中也没有收到任何文件时 'EQUFULLFILE' or 'NONEQUFULLFILE' 它只是从数据库表中删除数据。
我只需要修改我的代码一点,以适应逻辑,这样当我没有收到任何文件在目录中作为 'EQUFULLFILE' 则不应从数据库表中删除任何数据 'EquData' . 当我在目录中没有收到任何文件时 'NONEQUFULLFILE' 则不应从数据库表中删除任何数据 'NonEquData' .
有什么建议吗?

@Service
public class IImporter {

    private final static Logger log = LoggerFactory.getLogger(IImporter.class);
    private final static String EQU_FILE_TAG = "EQUFULLFILE";
    private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";

    private boolean isEquity;

    @Autowired
    private IFullreader IFullreader;

    @Autowired
    private ZipWalker zipWalker;

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void importDir(Path indir) throws IOException {
        log.info("Delete all table DATA");

    //here the logic should be changed and based on file name table should be deleted
        sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
        sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();

        log.info("Process directory" + indir.toString());

        Files.walk(indir, 1, FOLLOW_LINKS)
            .filter(Files::isRegularFile)
            .filter(f -> f.toString().endsWith(".zip"))
            .sorted()
            .forEach(f -> zipWalker.processZipFile(f, this::importFile));
    }

    private void importFile(Path path) {
        this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
        if (isEquity) {
        //code for reading data from file EQUFULLFILE
        }       
        else {
        //code for reading data from file NONEQUFULLFILE
        }           

        }
    }
}
htrmnn0y

htrmnn0y1#

您已经定义了两次equ文件标记。我怀疑那是个打字错误。
这个怎么样?

@Service
public class IImporter {

    private final static Logger log = LoggerFactory.getLogger(IImporter.class);
    private final static String EQU_FILE_TAG = "EQUFULLFILE";
    private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";

    private boolean isEquity;
    private boolean equFileFirstTime = true;
    private boolean nonequFileFirstTime = true;  

    @Autowired
    private IFullreader IFullreader;

    @Autowired
    private ZipWalker zipWalker;

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void importDir(Path indir) throws IOException {
        log.info("Process directory" + indir.toString());

        Files.walk(indir, 1, FOLLOW_LINKS)
            .filter(Files::isRegularFile)
            .filter(f -> f.toString().endsWith(".zip"))
            .sorted()
            .forEach(f -> zipWalker.processZipFile(f, this::importFile));
    }

    private void importFile(Path path) {
        this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
        if (isEquity) {
            if (equFileFirstTime) {
                log.info("Delete EQUFULLFILE table DATA");
                sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
                equFileFirstTime = false;
            }
            //code for reading data from file EQUFULLFILE
        }       
        else {
            if (nonequFileFirstTime) {
                log.info("Delete NONEQUFULLFILE table DATA");
                sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
                nonequFileFirstTime = false;
            }
            //code for reading data from file NONEQUFULLFILE
        }           

    }

}

相关问题