• jpa将数据库表数据生成新增语句,查询字段,查询注释,查询表字段


    复制代码
    @Override
    public void signForm(HttpServletResponse response, Map<String, Object> map) {
    try{
    String substring = url.substring(0, url.indexOf("?"));
    String substring1 ="'"+ substring.substring(substring.lastIndexOf("/")+1)+"'";
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment;filename="+new String((System.currentTimeMillis()+".sql").getBytes("utf-8"),"ISO-8859-1"));
    OutputStream out = response.getOutputStream();
    String name=map.get("name").toString();
    String sql0="SHOW CREATE TABLE "+name;
    Query query0 = entityManager.createNativeQuery(sql0);
    List resultList2 = query0.getResultList();
    String s1 = JSON.parseArray(JSON.toJSONString(resultList2.get(0)), String.class).get(1)+";";
    out.write((s1+System.getProperty("line.separator")).getBytes());
    String sql="SELECT table_name, COLUMN_NAME dbName, DATA_TYPE dbType, COLUMN_COMMENT label FROM INFORMATION_SCHEMA.COLUMNS where table_name=" + "'"+name+"'" + "AND table_schema="+substring1;
    Query query = entityManager.createNativeQuery(sql);
    List<Object> resultList = query.getResultList();
    String insetSql="insert into "+name+" (";
    String sql2="SELECT ";
    for (Object map1:resultList){
    List<String> strings = JSON.parseArray(JSON.toJSONString(map1), String.class);
    String s = strings.get(2);
    String sd = "`" + strings.get(1) + "`,";
    if(s.contains("int")){
    sql2+="(`" + strings.get(1) + "`+0) "+strings.get(1)+",";
    }else{
    sql2+=sd;
    }
    insetSql+=sd;
    }
    insetSql=insetSql.substring(0,insetSql.length()-1);
    sql2=sql2.substring(0,sql2.length()-1);
    insetSql+=" )values";
    sql2+=" FROM "+name;
    Query query2 = entityManager.createNativeQuery(sql2);
    query2.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    List<Map<String,Object>> resultList1 = query2.getResultList();
    for (Map<String,Object> map1 :resultList1){
    String insetSql2=insetSql;
    insetSql2+=" ( ";
    for (Object map2:resultList){
    String s = JSON.parseArray(JSON.toJSONString(map2), String.class).get(1);
    String type = JSON.parseArray(JSON.toJSONString(map2), String.class).get(2);
    if(type.contains("int")||type.equals("Integer")||map1.get(s)==null){
    insetSql2+=map1.get(s)+",";
    }else{
    insetSql2+="'"+map1.get(s)+"',";
    }
    }
    insetSql2=insetSql2.substring(0,insetSql2.length()-1)+");";
    out.write((insetSql2+System.getProperty("line.separator")).getBytes());
    }
    out.flush();
    out.close();
    }catch (Exception e){
    }
    }
    复制代码
  • 相关阅读:
    mysql存储过程
    cms初步构想
    ubuntu 搭建Mercurial 服务(nginx)
    ubuntu 搭建简易的https网站
    【分布式】ZooKeeper权限控制之ACL(Access Control List)访问控制列表
    【分布式】Zookeeper的Leader选举-选举过程介绍(经典的Paxos算法解析)
    【Java 泛型】之 <? super T> 和<? extends T> 中 super ,extends如何理解?有何异同?
    CSS3 圆角(border-radius)
    css去除链接 input 虚框
    关于背景色半透明的实现
  • 原文地址:https://www.cnblogs.com/qq376324789/p/16004506.html
Copyright © 2020-2023  润新知