java通过sql代码生成Dao和实体类

这篇博客介绍了如何在Java环境中,通过SQL代码来自动化生成Dao接口和对应的实体类,简化开发工作。只需调用特定方法即可完成生成,提高了开发效率。
摘要由CSDN通过智能技术生成
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import Element.sqlElem;


/**
 * 必须新建一个实体类 实体类名称:sqlElem 
 * 内设字段如下: 
 * private String name;//
 * 字段名 private String cls;//类型
 * @author mfkwfc
 */
public class CreateElementandDao {
/**
* 开始读取文本文件第一步验证
*/
public void start(String url) {
File file = new File(url);
if (file.exists()
&& url.substring(url.length() - 3, url.length()).equals("txt")) {
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
Map<String, List<sqlElem>> map = readtxt(buff);
if (map != null) {
String tempurl = url.substring(0, url.lastIndexOf("/") + 1);
readDao(map,tempurl);
readElement(map, tempurl);
}
} catch (FileNotFoundException e) {
System.out.println("读取失败了哦。");
e.printStackTrace();
}
} else {
System.out.println("该文件不存在哦或者后缀名错误");
}
}


/**
* 通过验证做读取动作
*/
private Map<String, List<sqlElem>> readtxt(BufferedReader buff) {
Map<String, List<sqlElem>> map = null;
try {
String tempSql = "";
while (buff.ready()) {
String text = buff.readLine();
tempSql += text;
}
buff.close();
String[] temps = tempSql.split(";");
for (String temp : temps) {
int len = temp.indexOf("table");
if (len == -1)// 可能会有创建数据库代码假如遇到则跳过
continue;
temp = temp.substring(len + 5, temp.length());// 剥第一层皮得到表名
int tablelen = temp.indexOf("(");
String tableName = temp.substring(0, tablelen).trim();// 获得表的名称
temp = temp.substring(tablelen + 1, temp.lastIndexOf(")"))
.trim();// 剥第二层皮得到里边的具体字段
String[] charat = temp.split(",");// 分割里边的具体字段
if (map == null)
map = new HashMap<String, List<sqlElem>>();
List<sqlElem> ls = null;
for (String ch : charat) {
if (ls == null)
ls = new ArrayList<sqlElem>();
ch = ch.trim();
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值