zoukankan      html  css  js  c++  java
  • hibernate根据包名获取该包下实体类,数据库中不存在的库表名称


    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.supermap.realestate_bh.registration.util.StringUtil;
    import com.supermap.wisdombusiness.core.SuperSpringContext;
    import com.supermap.wisdombusiness_bh.framework.dao.impl.CommonDao;
    import org.hibernate.SessionFactory;
    import org.springframework.orm.hibernate4.SessionFactoryUtils;

    import javax.persistence.Table;
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.DatabaseMetaData;
    import java.sql.ResultSet;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Set;

    public static Set getTableNotExits(){
    try {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    String packagename = "com.supermap.realestate.registration.model";
    packagename = "";
    String packagePath = packagename.replaceAll("\.", "\/");
    URL url = loader.getResource(packagePath);
    String path = url.getPath();
    File file = new File(path);
    File[] files = file.listFiles();
    List list = new LinkedList<>();
    Set<String> stringSet = new HashSet<>();
    for (File file1 : files) {
    if(file1.isDirectory()) continue;
    String filename = file1.getName();
    String name = packagename + "." + filename.replace(".class","");
    System.out.println(name);
    Class cl = Class.forName(name);
    if(cl.isAnnotationPresent(Table.class)){
    Table table = (Table)cl.getAnnotation(Table.class);
              //指定数据库/用户
    if("bdcdck".equalsIgnoreCase(table.schema())) stringSet.add(table.schema() + "&" + table.name());
    }
    list.add(packagename + "." + file1.getName());

    }

    SessionFactory object = SuperSpringContext.getContext().getBean(CommonDao.class).getSessionFactory();
    Connection connection = SessionFactoryUtils.getDataSource(object).getConnection();
    DatabaseMetaData databaseMetaData = connection.getMetaData();
    Set<String> set = new HashSet<>();
    for (String s : stringSet) {
    if(StringUtil.isEmpty(s)) continue;
    String[] ss = s.split("&");
    ResultSet rs = databaseMetaData.getTables(null,ss[0].toUpperCase(),ss[1].toUpperCase(),new String[]{"TABLE"});
    if(!rs.next()){
    set.add(s);
    }
    }

    return set;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
  • 相关阅读:
    js递归优化
    音视频混流
    JS的发布订阅模式
    redhat7.4 docker run启动容器报错container_linux.go:449
    使用Docker部署vue的前端应用过程
    Mac下安装pip
    powerDesigner 把name项添加到注释
    SQL SERVER 2012 连接报错
    CentOS minimal 安装图形界面
    SQLServer2008卸载后重装问题
  • 原文地址:https://www.cnblogs.com/shihx/p/13410655.html
Copyright © 2011-2022 走看看