JavaWeb前后端代码一键生成
生成包括controller层/Rest类、service层、实体类dao层(可不用), 实体类。
先说下,用编码配置方式:
public class GenBeanSimple {
public static void main(String[] args) {
try{
String dbName=HoneyConfig.getHoneyConfig().getDbName();
// driverName,url,username,password config in bee.properties.
GenConfig config = new GenConfig();
config.setDbName(dbName);
config.setGenToString(true);//生成toString方法
config.setGenSerializable(true); //生成序列化
config.setGenComment(true); //可生成注释
config.setCommentPlace(1); //注释的位置
// 更改成本地的具体路径 change to your real path
config.setBaseDir("D:\\xxx\\yyy\\bee-exam\\src\\main\\java\\");
config.setPackagePath("org.teasoft.exam.bee.osql.entity2");
GenBean genBean = new GenBean(config);
config.setGenFieldFile(true); //可以使用{实体}_F来引用字段名
//设置相对Entity的文件夹; 空表示与Entity同一个文件夹
// config.setFieldFileRelativeFolder("field"); //默认
config.setOverride(true); //是否覆盖原来的文件
genBean.genSomeBeanFile("orders");
} catch (BeeException e) {
e.printStackTrace();
}
}
}
根据Javabean创建表
Ddl.createTable(new Orders(), false); //后一个参数为是否先删除已有表
详细可参考: CreateTableWithJavabean
生成Controller, Service等
Controller类一般用于控制访问, 而在rest风格编程中(也是前后端分离),可以直接返回jason数据给前端解析.因此,此处用Rest作为类的后缀.
生成实例:
GenFilesExam
package org.teasoft.exam.bee.osql.autogen;
import java.util.HashMap;
import java.util.Map;
import org.teasoft.honey.osql.autogen.GenFiles;
/**
* @author Kingstar
* @since 1.7.2
*/
public class GenFilesExam {
public static void main(String[] args) {
test();
}
public static void test() {
BeeInitPreLoadService.init(); //V1.11 要使用,低于1.11可注释掉
Map map = new HashMap<>();
// map.put("entityName1", "Orderhistory"); //使用#{entityName?up1}可以首字母大写
map.put("entityName", "orderhistory");
map.put("packageName", "com.automvc.enet.order.rest");
// 更改成本地的具体路径 change to your real path
String basePath = "D:\\workspace\\bee-exam\\src\\main\\java\\org\\teasoft\\exam\\bee\\osql\\autogen\\";
String templatePath = basePath + "OrderhistoryRest.java.template";
String targetFilePath = basePath + "OrderhistoryRest.java";
GenFiles.genFile(templatePath, map, targetFilePath);
System.out.println("finished!");
}
}
/*
* Copyright 2016-2020 the original author.All rights reserved.
* Kingstar([email protected])
* The license,see the LICENSE file.
*/
package com.automvc.enet.order.rest;
import java.util.List;
import org.teasoft.bee.osql.BeeSQLException;
import org.teasoft.bee.osql.FunctionType;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.automvc.enet.order.entity.Orderhistory;
import com.automvc.common.jquery.Result;
/**
* @author AiTeaSoft.com
* @since 1.0
* Create on 2019-04-16 11:48:24
*/
@RestController
@RequestMapping("/orderhistory")
public class OrderhistoryRest {
@Autowired
ObjSQLService objSQLService;
@Autowired
ObjSQLRichService objSQLRichService;
@RequestMapping("/list")
public Result list(Orderhistory orderhistory,
@RequestParam(value = "page", defaultValue = "1", required = false) int page,
@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {
Result result =new Result();
try{
int total = objSQLRichService.count(orderhistory);
List list=objSQLRichService.select(orderhistory, (page-1)*rows, rows);
result.setRows(list);
result.setTotal(total);
} catch (BeeSQLException e) {
result.setErrorMsg(e.getMessage());
}
return result;
}
@RequestMapping("/add")
public Result insert(Orderhistory orderhistory){
Result result =new Result();
try{
int num=objSQLService.insert(orderhistory);
result.setTotal(num);
if(num<=0) result.setErrorMsg("insert failed!");
} catch (BeeSQLException e) {
result.setErrorMsg(e.getMessage());
}
return result;
}
@RequestMapping("/edit")
public Result update(Orderhistory orderhistory){
Result result =new Result();
try{
int num=objSQLService.update(orderhistory);
result.setTotal(num);
if(num<=0) result.setErrorMsg("update failed!");
} catch (BeeSQLException e) {
result.setErrorMsg(e.getMessage());
}
return result;
}
@RequestMapping("/del")
public Result delete(String ids) {
Result result = new Result();
try {
int num=objSQLRichService.deleteById(Orderhistory.class, ids);
result.setTotal(num);
if (num <= 0) result.setErrorMsg("delete failed!");
} catch (BeeSQLException e) {
result.setErrorMsg(e.getMessage());
}
return result;
}
}
编程方式还是略显麻烦。
还可以用一键生成式, 将整个工程都生成, 前后端的代码都可以生成,连搭建环境也不需要我们自己做.
浏览快码加编-代码生成神器
官网: http://www.kmcoding.com
可以点击演示查看下效果.
然后再到 生成器[简易] 菜单, 输入自己的工程名,包名等信息, 下载整个工程.
还可以针对不同的数据表,生成增删改查的前后端代码.
控制哪些字段是否显示,哪些字段用于查询条件等,可使用定制菜单,更加高级功能等着你。