最新写了一个java web代码生成器,主要可以根据模板生成对应的controller、service、model。尤其是在做毕业设计时,很多code都是类似,这样就少去了ctrl+c/v的时间。
其核心是根据反射mysql数据表clum映射到实体中,然后遍历velocity自定义的模板文件,生成代码。
操作界面:
生成代码:
对应表实体:
/**
* 数据库信息获得操作类
*
* @ClassName: DatabaseInfoOp
* @author ardo
*/
public class DatabaseInfoOp {
Connection conn = null;
private String classDriver;
private String url;
private String username;
private String password;
private String schema;
/**
* 获得表数据
*
* @Title: getDbInfo
* @return
* @throws SQLException
*/
public Database getDbInfo(String tableNamePattern) throws SQLException {
Database databaseBean = new Database();
// 表队列
List<Table> tableList = new ArrayList<Table>();
// 初始化数据库
getConnectionByJDBC();
// 获取数据库信息
DatabaseMetaData dbmd = conn.getMetaData();
databaseBean.setDatabaseProductName(dbmd.getDatabaseProductName());
// 获得数据库表
ResultSet rs = dbmd.getTables(null, null, tableNamePattern, new String[] { "TABLE", "VIEW" });
// String tableName = "";
while (rs.next()) {
Table table = new Table();
table.setTableName(rs.getString("TABLE_NAME"));
table.setTableComment(rs.getString("REMARKS"));
table.setTableSchem(rs.getString(1));
// 设置列信息
ResultSet rscol = dbmd.getColumns(null, null, table.getTableName(), null);
Column tempColumn;
while (rscol.next()) {
tempColumn = new Column();
tempColumn.setColumnName(rscol.getString("COLUMN_NAME"));
tempColumn.setColumnType(Integer.parseInt(rscol.getString("DATA_TYPE")));
String remarks = rscol.getString("REMARKS");
if (remarks.length() < 1)
remarks = "";
tempColumn.setColumnComment(remarks);
tempColumn.setAutoIncrement(rscol.getString("IS_AUTOINCREMENT").equals("YES"));
tempColumn.setNullAble(rscol.getString("IS_AUTOINCREMENT").equals("YES"));
// 添加列到表中
table.getColumnList().add(tempColumn);
}
// 设置主键列
ResultSet rsPrimary = dbmd.getPrimaryKeys(null, null, table.getTableName());
while (rsPrimary.next()) {
if (rsPrimary.getString("COLUMN_NAME") != null) {
for (int i = 0; i < table.getColumnList().size(); i++) {
Column coltemp = table.getColumnList().get(i);
if (coltemp.getColumnName().equals(rsPrimary.getString("COLUMN_NAME"))) {
coltemp.setPrimary(true);
}
}
}
}
// 设置外键列
ResultSet rsFPrimary = dbmd.getImportedKeys(null, null, table.getTableName());
while (rsFPrimary.next()) {
for (int i = 0; i < table.getColumnList().size(); i++) {
Column coltemp = table.getColumnList().get(i);
if (coltemp.getColumnName().equals(rsFPrimary.getString("FKCOLUMN_NAME"))) {
coltemp.setForeignKey(true);
}
}
}
tableList.add(table);
}
databaseBean.setTableList(tableList);
return databaseBean;
}
public Database getDbInfo() throws SQLException {
Database databaseBean = new Database();
// 表队列
List<Table> tableList = new ArrayList<Table>();
// 初始化数据库
getConnectionByJDBC();
// 获取数据库信息
DatabaseMetaData dbmd = conn.getMetaData();
databaseBean.setDatabaseProductName(dbmd.getDatabaseProductName());
// 获得数据库表
ResultSet rs = dbmd.getTables(null, this.schema, null, new String[] { "TABLE", "VIEW" });
// String tableName = "";
while (rs.next()) {
Table table = new Table();
table.setTableName(rs.getString("TABLE_NAME"));
table.setTableComment(rs.getString("REMARKS"));
table.setTableSchem(rs.getString(1));
// 设置列信息
ResultSet rscol = dbmd.getColumns(null, null, table.getTableName(), null);
Column tempColumn;
while (rscol.next()) {
tempColumn = new Column();
tempColumn.setColumnName(rscol.getString("COLUMN_NAME"));
tempColumn.setColumnType(Integer.parseInt(rscol.getString("DATA_TYPE")));
String remarks = rscol.getString("REMARKS");
if (remarks.length() < 1)
remarks = "";
tempColumn.setColumnComment(remarks);
tempColumn.setAutoIncrement(rscol.getString("IS_AUTOINCREMENT").equals("YES"));
tempColumn.setNullAble(rscol.getString("IS_AUTOINCREMENT").equals("YES"));
// 添加列到表中
table.getColumnList().add(tempColumn);
}
// 设置主键列
ResultSet rsPrimary = dbmd.getPrimaryKeys(null, null, table.getTableName());
while (rsPrimary.next()) {
if (rsPrimary.getString("COLUMN_NAME") != null) {
for (int i = 0; i < table.getColumnList().size(); i++) {
Column coltemp = table.getColumnList().get(i);
if (coltemp.getColumnName().equals(rsPrimary.getString("COLUMN_NAME"))) {
coltemp.setPrimary(true);
}
}
}
}
// 设置外键列
ResultSet rsFPrimary = dbmd.getImportedKeys(null, null, table.getTableName());
while (rsFPrimary.next()) {
for (int i = 0; i < table.getColumnList().size(); i++) {
Column coltemp = table.getColumnList().get(i);
if (coltemp.getColumnName().equals(rsFPrimary.getString("FKCOLUMN_NAME"))) {
//System.out.println("FKCOLUMN_NAME "+rsFPrimary.getString("FKCOLUMN_NAME"));
coltemp.setForeignKey(true);
}
}
}
tableList.add(table);
}
databaseBean.setTableList(tableList);
return databaseBean;
}
}
以下是生成service及持久层代码:
package com.exemple.service.impl;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.exemple.model.Product;
import com.exemple.service.ProductService;
import com.exemple.util.BeanUtil;
import org.unique.tools.StringUtils;
import org.unique.ioc.annotation.Autowired;
import org.unique.ioc.annotation.Service;
import org.unique.support.orm.dao.Page;
import org.unique.support.orm.dao.SqlBase;
import org.unique.support.orm.exception.UpdateException;
import org.unique.tools.CollectionUtil;
@Service
public class ProductServiceImpl implements ProductService{
@Override
public Product get(Integer id){
return Product.db.findByPK(id);
}
@Override
public Map<String, Object> getMap(Product product, Integer id){
Map<String, Object> resultMap = CollectionUtil.newHashMap();
if (null == product) {
product = this.get(id);
}
if (null != product) {
resultMap = BeanUtil.toMap(product);
}
return resultMap;
}
@Override
public boolean save( String productName, Integer classify, String pic, String describ, String price ){
int count = 0;
return count > 0;
}
@Override
public boolean update(Integer id, String productName, Integer classify, String pic, String describ, String price ){
int count = 0;
Product product = this.get(id);
if (null != product) {
try {
} catch (UpdateException e) {
count = 0;
}
}
return count > 0;
}
@Override
public boolean delete(Integer id){
int count = 0;
if (null != id) {
Product product = this.get(id);
if (null != product) {
try {
count = Product.db.delete("delete from product where id = ?", id);
} catch (UpdateException e) {
count = 0;
}
}
}
return count > 0;
}
@Override
public List<Map<String, Object>> getList(String productName,Integer classify,String pic,String describ,String price, String order){
SqlBase base = SqlBase.select("select t.* from product t");
base.eq("t.product_name", productName);
base.eq("t.classify", classify);
base.eq("t.pic", pic);
base.eq("t.describ", describ);
base.eq("t.price", price);
base.order(order);
List<Product> list = Product.db.findList(base.getSQL(), base.getParams());
return this.getProductMapList(list);
}
@Override
public Page<Product> getPageList(String productName,Integer classify,String pic,String describ,String price, Integer page, Integer pageSize, String order){
SqlBase base = SqlBase.select("select t.* from product t");
base.eq("t.product_name", productName);
base.eq("t.classify", classify);
base.eq("t.pic", pic);
base.eq("t.describ", describ);
base.eq("t.price", price);
base.order(order);
return Product.db.findListPage(page, pageSize, base.getSQL(), base.getParams());
}
@Override
public Page<Map<String, Object>> getPageMapList(Page<Product> pageEntity){
List<Product> productList = pageEntity.getResults();
Page<Map<String, Object>> pageMap = new Page<Map<String, Object>>(pageEntity.getTotalCount() , pageEntity.getPage(), pageEntity.getPageSize());
List<Map<String, Object>> listMap = this.getProductMapList(productList);
pageMap.setResults(listMap);
return pageMap;
}
private List<Map<String, Object>> getProductMapList(List<Product> productList){
List<Map<String, Object>> mapList = CollectionUtil.newArrayList();
for (int i = 0, len = productList.size(); i < len; i++) {
Product product = productList.get(i);
if (null != product) {
Map<String, Object> map = this.getMap(product, null);
mapList.add(map);
}
}
return mapList;
}
}
项目源码下载地址: http://download.youkuaiyun.com/detail/ardo_pass/9733984