根据MySQL表自动生成接口_从数据库表自动生成java代码(接口层)

这个Java程序能根据MySQL数据库表自动生成对应的接口代码。它连接到数据库,获取表结构,然后创建Java接口文件,包含增删改查等基本操作。通过指定数据库连接参数、表名和作者信息,可以快速生成接口层代码,提高开发效率。
摘要由CSDN通过智能技术生成

package com.zero2ipo.cfj.common;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Date;

public class GenInterface {

private String packageOutPath = "com.zero2ipo.cfj.common";//指定实体生成所在包的路径

private String authorName = "zhengyunfei";//作者名字

private String tablename = "cfj_article_attr";//表名

private String[] colnames; // 列名数组

private String[] colTypes; //列名类型数组

private int[] colSizes; //列名大小数组

//数据库连接

private static final String URL ="jdbc:mysql://172.168.8.39:3306/caifujie";

private static final String NAME = "root";

private static final String PASS = "zhengyunfei";

private static final String DRIVER ="com.mysql.jdbc.Driver";

/*

* 构造函数

*/

public GenInterface(){

//创建连接

Connection con;

//查要生成实体类的表

String sql = "select * from " + tablename;

PreparedStatement pStemt = null;

try {

try {

Class.forName(DRIVER);

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

}

con = DriverManager.getConnection(URL,NAME,PASS);

pStemt = con.prepareStatement(sql);

ResultSetMetaData rsmd = pStemt.getMetaData();

int size = rsmd.getColumnCount();//统计列

colnames = new String[size];

colTypes = new String[size];

colSizes = new int[size];

for (int i = 0; i < size; i++) {

colnames[i] = rsmd.getColumnName(i + 1);

colTypes[i] = rsmd.getColumnTypeName(i + 1);

colSizes[i] = rsmd.getColumnDisplaySize(i + 1);

}

String content = parse(colnames,colTypes,colSizes);

try {

File directory = new File("");

String path=this.getClass().getResource("").getPath();

String outputPath = directory.getAbsolutePath()+ "/src/"+this.packageOutPath.replace(".", "/")+"/"+"I"+initcap(tablename) + ".java";

FileWriter fw = new FileWriter(outputPath);

PrintWriter pw = new PrintWriter(fw);

pw.println(content);

pw.flush();

pw.close();

} catch (IOException e) {

e.printStackTrace();

}

} catch (SQLException e) {

e.printStackTrace();

} finally{

//try {

//con.close();

//} catch (SQLException e) {

TODO Auto-generated catch block

//e.printStackTrace();

//}

}

}

/**

* 功能:生成实体类主体代码

* @param colnames

* @param colTypes

* @param colSizes

* @return

*/

private String parse(String[] colnames, String[] colTypes, int[] colSizes) {

StringBuffer sb = new StringBuffer();

sb.append("package " + this.packageOutPath + ";\r\n");

tablename=replaceUnderlineAndfirstToUpper(tablename.toLowerCase(),"_","");

importPackage(sb,this.packageOutPath+"."+initcap(tablename));

//注释部分

sb.append(" /**\r\n");

sb.append(" * "+tablename+" 实体类\r\n");

sb.append(" * "+new Date()+" "+this.authorName+"\r\n");

sb.append(" */ \r\n");

//实体部分

sb.append("\r\n\r\npublic interface I" + initcap(tablename) + "{\r\n");

add(sb,tablename);

update(sb,tablename);

delete(sb,tablename);

findById(sb,tablename);

findAllList(sb,tablename);

sb.append("}\r\n");

return sb.toString();

}

/**

* 导入包

*@title

*@date 2014-10-17

*@author ZhengYunfei

* @param sb

*/

public static void importPackage(StringBuffer sb,String tablename){

sb.append("import java.util.List;\r\n");

sb.append("import java.util.Map;\r\n");

sb.append("import "+tablename+"Bo"+";\r\n");

}

/**

* 页面跳转初始化

*@title

*@date 2014-10-17

*@author ZhengYunfei

* @param sb

* @param tablename

*/

public static void init(StringBuffer sb,String tablename){

infoMsg(sb,"页面跳转初始化");

sb.append("\t@Autowired @Qualifier(\"article\")\r\n");

sb.append("\rprivate I"+initcap(tablename)+" "+tablename+";\r\n");

sb.append("\t@RequestMapping(\"forInit\")\r\n");

sb.append("\tpublic ModelAndView forInit(){\t\n");

sb.append("\tModelAndView mv=new ModelAndView();\r\n");

sb.append("\treturn mv;\r\n");

sb.append("\t}\r\n");

}

public static void add(StringBuffer sb,String tablename){

infoMsg(sb,"新增");

sb.append("\tpublic String add("+tablename+"Bo bo);\r\n");

}

public static void update(StringBuffer sb,String tablename){

infoMsg(sb,"修改");

sb.append("\tpublic String update("+initcap(tablename)+" bo);\r\n");

}

public static void delete(StringBuffer sb,String tablename){

infoMsg(sb,"删除");

sb.append("\tpublic String delete(String id);\r\n");

}

/**

* 根据id查询

*@title

*@date 2014-10-17

*@author ZhengYunfei

* @param sb

* @param tablename

*/

public static void findById(StringBuffer sb,String tablename){

infoMsg(sb,"根据Id查询");

sb.append("\tpublic "+tablename+"Bo"+" findById(String id);\r\n");

}

/**

* 查询所有信息

*@title

*@date 2014-10-17

*@author ZhengYunfei

* @param sb

* @param tablename

*/

public static void findAllList(StringBuffer sb,String tablename){

infoMsg(sb,"查询所有信息");

sb.append("\tpublic String findAllList(Map queryMap,String curNo, String curSize);\r\n");

}

/**

* 自动生成注释

*@title

*@date 2014-10-17

*@author ZhengYunfei

*/

public static void infoMsg(StringBuffer sb,String msg){

sb.append("\t/**\r\n");

sb.append("\t*"+msg+"\r\n");

sb.append("\t*@author zhengYunFei\r\n");

sb.append("\t*@date "+new Date()+"\r\n");

sb.append("\t*/\r\n");

}

/**

* 首字母大写

*

* @param srcStr

* @return

*/

public static String firstCharacterToUpper(String srcStr) {

return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);

}

/**

* 替换字符串并让它的下一个字母为大写

* @param srcStr

* @param org

* @param ob

* @return

*/

public static String replaceUnderlineAndfirstToUpper(String srcStr,String org,String ob)

{

String newString = "";

int first=0;

while(srcStr.indexOf(org)!=-1)

{

first=srcStr.indexOf(org);

if(first!=srcStr.length())

{

newString=newString+srcStr.substring(0,first)+ob;

srcStr=srcStr.substring(first+org.length(),srcStr.length());

srcStr=firstCharacterToUpper(srcStr);

}

}

newString=newString+srcStr;

return newString;

}

/**

* 功能:将输入字符串的首字母改成大写

* @param str

* @return

*/

private static String initcap(String str) {

char[] ch = str.toCharArray();

if(ch[0] >= 'a' && ch[0] <= 'z'){

ch[0] = (char)(ch[0] - 32);

}

return new String(ch);

}

/**

* 出口

* TODO

* @param args

*/

public static void main(String[] args) {

new GenInterface();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值