开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 306

wangzg/codeutil

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
codeutil
/
src
/
main
/
java
/
xml
/
DatabaseXml.java
codeutil
/
src
/
main
/
java
/
xml
/
DatabaseXml.java
DatabaseXml.java 6.32 KB
一键复制 编辑 原始数据 按行查看 历史
package xml;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import util.Unicode;
import entity.Column;
import entity.Table;
import templet.Utils;
/**
* 数据库转XML类
*/
public class DatabaseXml {
/**
* 写配置文件
* @param util
* @param outPath
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void writeDatabaseXml(database.Util util,Map<String,String> propertyMap, String outPath) throws ClassNotFoundException, SQLException
{
Document doc=DocumentHelper.createDocument();//
doc.setXMLEncoding("utf-8");
Element root= doc.addElement("db");
root.addAttribute("name", util.getDbName());//
root.addAttribute("driverName", util.getDriverName());//
root.addAttribute("userName", util.getUserName());//
root.addAttribute("passWord", util.getPassWord());//
root.addAttribute("url", util.getUrl());//
for(String key:propertyMap.keySet())
{
Element element = root.addElement("property");
element.addAttribute("name", key);
element.setText(propertyMap.get(key));
}
List<Table> tableList= util.getDbInfo();
for(Table table : tableList)
{
int keycount=0;//主键数量
String keyType="";
for(Column column : table.getColumns()){
if(column.getColumnKey().equals("PRI")){
keycount++;
keyType=column.getColumnType();
}
}
if(keycount==1){//如果是只有一个主键
//System.out.println("读取表:"+table.getName());
Element tableElement=root.addElement("table");//
tableElement.addAttribute("name", table.getName() );//表名称
tableElement.addAttribute("name2", Utils.getTableName2(table.getName()) ); //处理后的表名称(去掉前缀)
tableElement.addAttribute("comment", Unicode.toUnicodeString(table.getComment()) );
tableElement.addAttribute("key", table.getKey() );
tableElement.addAttribute("key2", Utils.getColumnName2( table.getKey()) );//转驼峰格式
tableElement.addAttribute("Key2", Utils.getClassName(Utils.getColumnName2( table.getKey())) );//转驼峰格式,首字母大写
tableElement.addAttribute("keyType", keyType );//主键类型
for(Column column : table.getColumns())
{
//System.out.println("读取字段:"+column.getColumnName());
Element columnElement= tableElement.addElement("column");
columnElement.addAttribute("name", column.getColumnName());//字段名称
columnElement.addAttribute("name2", Utils.getColumnName2(column.getColumnName()) );////转驼峰格式
columnElement.addAttribute("type", column.getColumnType());//类型
columnElement.addAttribute("dbtype", column.getColumnDbType());
columnElement.addAttribute("comment", Unicode.toUnicodeString( column.getColumnComment()));
columnElement.addAttribute("key", column.getColumnKey());
columnElement.addAttribute("decimal_digits", String.valueOf(column.getDecimal_digits()));
columnElement.addAttribute("colums_size", String.valueOf(column.getColums_size()));
//主键数量
if(column.getColumnKey().equals("PRI")){
keycount++;
}
}
}
}
writeXml(outPath, doc);
}
/**
* 返回表名称
* @return
*/
public static List<Table> readDatabaseXml(String xmlPath)
{
List<Table> list=new ArrayList();
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlPath+ File.separatorChar + "db.xml");
Element dbe= doc.getRootElement();
List<Element> elist= dbe.elements();
for(Element e:elist)
{
if(e.getName().equals("table")){
Table table=new Table();
table.setName(e.attributeValue("name"));
table.setName2(e.attributeValue("name2"));
table.setKeyType(e.attributeValue("keyType"));
table.setComment( Unicode.decodeUnicode( e.attributeValue("comment")));
table.setKey(e.attributeValue("key"));
table.setKey2(e.attributeValue("key2"));
table.setKey2Upper(e.attributeValue("Key2"));//大写主键名
List<Column> columns=new ArrayList();
List<Element> elist2= e.elements(); //字段列表
for(Element e2:elist2)
{
Column column=new Column();
column.setColumnName(e2.attributeValue("name"));
column.setColumnName2(e2.attributeValue("name2"));
column.setColumnType(e2.attributeValue("type"));
column.setColumnDbType(e2.attributeValue("dbtype"));
column.setColumnComment(Unicode.decodeUnicode( e2.attributeValue("comment")));
column.setColumnKey(e2.attributeValue("key"));
columns.add(column);
}
table.setColumns(columns);
list.add(table);
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 读取属性
* @return
*/
public static Map<String,String> readProperty(String xmlPath)
{
Map<String,String> map=new HashMap<String, String>();
try {
SAXReader reader = new SAXReader();
Document doc = reader.read(xmlPath+ File.separatorChar +"db.xml");
Element dbe= doc.getRootElement();
List<Element> elist= dbe.elements();
for(Element e:elist)
{
if(e.getName().equals("property")){
map.put(e.attributeValue("name"), e.getText());
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
/**
*
* @param outPath
* @param doc
*/
private static void writeXml(String outPath,Document doc)
{
try {
String xmlFileName="db.xml";
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");//
XMLWriter writer = null;//
File file = new File(outPath + File.separatorChar +xmlFileName);
if(!file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
writer = new XMLWriter(new FileWriter(file), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

《黑马架构师》一款由传智播客教育集团研究院开发的基于自主研发的模板引擎的"代码生成神器"。即便是一个工程几百个表,也可以瞬间完成基础代码的构建!用户只需建立数据库表结构,简单的几步操作就可以快速生成可以运行的一整套代码,可以极大地缩短开发周期,降低人力成本。《黑马架构师》内置了当前java和前端主流的架构模板,如SSM+dubbo 、springBooot+springCloud+springData 、前后端分离的vue+elementUI 模板、swagger API模板、数据库文档模板等。 用户通过自己开发模板也可以实现生成php、python、C# 、c++、数据库存储过程等其它编程语言的代码。
暂无标签
Apache-2.0
使用 Apache-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/sinBao/codeutil.git
git@gitee.com:sinBao/codeutil.git
sinBao
codeutil
codeutil
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /