实现了一个简单的代码构建工具
通过实体类Bean生成对应的Action、ServiceInter、ServiceImpl类。
工具类BuildCode.java
package com.sh.tools;
import java.io.File;
import java.io.FileWriter;
/**
* 代码生成工具
* ClassName: BuildCode
* @Description: TODO
* @author micoMo
* @date 2017-9-16
*/
@SuppressWarnings("unchecked")
public class BuildCode {
//首行缩进BEGIN
private static final String B1 =" ";
private static final String B2 =" ";
private static final String B3 =" ";
//换行END
private static final String E1 = "\r\n";
private static final String E2 = "\r\n\r\n";
//对应类的路径
private static final String ACTION_PATH = "com/sh/action";
private static final String SERVICE_INTER_PATH = "com/sh/service/inter";
private static final String SERVICE_IMPL_PATH = "com/sh/service/impl";
/**
* 创建三个类的方法
* @Description: TODO
* @param @param c
* @param @throws Exception
* @return void
* @throws
* @author micoMo
* @date 2017-9-16
*/
public static void createCodeTool(Class c) throws Exception{
createBeanServiceInter(c);
createBeanServiceImpl(c);
createBeanAction(c);
}
/**
* 创建beanAction
* @Description: TODO
* @param @param c
* @param @throws Exception
* @return void
* @throws
* @author micoMo
* @date 2017-9-16
*/
private static void createBeanAction(Class c) throws Exception{
String cName = c.getSimpleName();//获取类名 User
String cname = getLowercaseChar(cName);//首字母转为小写 user
String fileName = System.getProperty("user.dir") + "/src/" + ACTION_PATH
+ "/" + cName + "Action.java";//文件完整路径
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
//拼接代码段
String code =
"package com.sh.action;"+E2+
"import java.util.List;"+E1+
"import javax.annotation.Resource;"+E1+
"import javax.servlet.http.HttpServletRequest;"+E1+
"import net.sf.json.JSONArray;"+E1+
"import net.sf.json.JSONObject;"+E1+
"import org.apache.struts2.ServletActionContext;"+E1+
"import org.apache.struts2.interceptor.ServletRequestAware;"+E1+
"import org.springframework.context.annotation.Scope;"+E1+
"import org.springframework.stereotype.Controller;"+E1+
"import com.sh.entity."+cName+";"+E1+
"import com.sh.entity.PageBean;"+E1+
"import com.sh.service.inter."+cName+"ServiceInter;"+E1+
"import com.sh.tools.ResponseTool;"+E1+
"import com.sh.tools.StringTool;"+E1+
"import com.opensymphony.xwork2.ActionSupport;"+E2+
"@Controller(\""+cname+"Action\")"+E1+
"@Scope(value=\"prototype\")"+E1+
"public class "+cName+"Action extends ActionSupport implements ServletRequestAware {"+E2+
B1+"@Resource(name=\""+cname+"Service\")"+E1+
B1+"private "+cName+"ServiceInter "+cname+"Service;"+E2+
B1+"JSONObject json = new JSONObject();"+E1+
B1+"String resultCode = \"1\";// 返回结果 1成功,0失败"+E1+
B1+"String errorMessage = \"\";// 返回结果错误信息"+E2+
B1+"private HttpServletRequest request;"+E1+
B1+"@Override"+E1+
B1+"public void setServletRequest(HttpServletRequest request) {"+E1+
B2+"this.request = request;"+E1+
B1+"}"+E2+
B1+"public String get"+cName+"() throws Exception{"+E2+
B2+"String id = request.getParameter(\"id\");"+E2+
B2+"if(StringTool.isEmpty(id)){"+E1+
B3+"errorMessage += \"id不能为空\";"+E1+
B3+"resultCode = \"0\";"+E1+
B2+"}"+E2+
B2+"if(\"\".equals(errorMessage)){"+E1+
B3+""+cName+" n = "+cname+"Service.get"+cName+"(Integer.parseInt(id));"+E1+
B3+"json.put(\""+cname+"\", JSONObject.fromObject(n));"+E1+
B2+"}"+E2+
B2+"json.put(\"errorMessage\", errorMessage);"+E1+
B2+"json.put(\"resultCode\", resultCode);"+E1+
B2+"ResponseTool.write(ServletActionContext.getResponse(), json);"+E1+
B2+"return null;"+E1+
B1+"}"+E2+
B1+"public String get"+cName+"List() throws Exception{"+E2+
B2+"String page = request.getParameter(\"page\");"+E1+
B2+"String rows = request.getParameter(\"rows\");"+E2+
B2+"String []param={page,rows};"+E1+
B2+"String []pname={\"page\",\"rows\"};"+E1+
B2+"if(!\"\".equals(StringTool.isEmpty(param, pname))){"+E1+
B3+"errorMessage += StringTool.isEmpty(param, pname);"+E1+
B3+"resultCode = \"0\";"+E1+
B2+"}"+E2+
B2+"if (\"\" == errorMessage) {"+E1+
B3+""+cName+" "+cname+" = new "+cName+"();"+E1+
B3+"PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));"+E1+
B3+"List<"+cName+"> "+cname+"List = "+cname+"Service.get"+cName+"List("+cname+", pageBean);"+E1+
B3+"long total = "+cname+"Service.get"+cName+"Count("+cname+");"+E1+
B3+"JSONArray list = JSONArray.fromObject("+cname+"List);"+E1+
B3+"json.put(\"list\", list);"+E1+
B3+"json.put(\"total\", total);"+E1+
B2+"}"+E2+
B2+"json.put(\"errorMessage\", errorMessage);"+E1+
B2+"json.put(\"resultCode\", resultCode);"+E1+
B2+"ResponseTool.write(ServletActionContext.getResponse(), json);"+E1+
B2+"return null;"+E2+
B1+"}"+E2+
"}";
//将代码段写入文件中
fw.write(code);
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 创建beanServiceInter
* @Description: TODO
* @param @param c
* @param @throws Exception
* @return void
* @throws
* @author micoMo
* @date 2017-9-16
*/
private static void createBeanServiceInter(Class c) throws Exception{
String cName = c.getSimpleName();
String cname = getLowercaseChar(cName);
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_INTER_PATH
+ "/" + cName + "ServiceInter.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
String code =
"package com.sh.service.inter;"+E2+
"import java.util.List;"+E1+
"import com.sh.entity.PageBean;"+E1+
"import com.sh.entity."+cName+";"+E2+
"public interface "+cName+"ServiceInter {"+E2+
B1+"List<"+cName+"> get"+cName+"List("+cName+" "+cname+", PageBean pageBean);"+E2+
B1+"Long get"+cName+"Count("+cName+" "+cname+");"+E2+
B1+"void save"+cName+"("+cName+" "+cname+");"+E2+
B1+"void delete"+cName+"("+cName+" "+cname+");"+E2+
B1+"void update"+cName+"("+cName+" "+cname+");"+E2+
B1+""+cName+" get"+cName+"(int id);"+E2+
"}";
fw.write(code);
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 创建beanServiceImpl
* @Description: TODO
* @param @param c
* @param @throws Exception
* @return void
* @throws
* @author micoMo
* @date 2017-9-16
*/
private static void createBeanServiceImpl(Class c) throws Exception{
String cName = c.getSimpleName();
String cname = getLowercaseChar(cName);
String fileName = System.getProperty("user.dir") + "/src/" + SERVICE_IMPL_PATH
+ "/" + cName +"ServiceImpl.java";
File f = new File(fileName);
FileWriter fw = new FileWriter(f);
String code =
"package com.sh.service.impl;"+E2+
"import java.util.ArrayList;"+E1+
"import java.util.List;"+E1+
"import javax.annotation.Resource;"+E1+
"import org.springframework.stereotype.Service;"+E1+
"import com.sh.dao.inter.BaseDao;"+E1+
"import com.sh.entity."+cName+";"+E1+
"import com.sh.entity.PageBean;"+E1+
"import com.sh.service.inter."+cName+"ServiceInter;"+E2+
"@Service(\""+cname+"Service\")"+E1+
"public class "+cName+"ServiceImpl implements "+cName+"ServiceInter {"+E2+
B1+"@Resource(name=\"baseDao\")"+E1+
B1+"private BaseDao<"+cName+"> baseDao;"+E2+
B1+"@Override"+E1+
B1+"public List<"+cName+"> get"+cName+"List("+cName+" "+cname+", PageBean pageBean){"+E2+
B2+"StringBuffer sbHQL = new StringBuffer(\"FROM "+cName+" \");"+E1+
B2+"List<Object> param = new ArrayList<>();"+E1+
B2+"if ("+cname+"!=null) {"+E2+
B2+"}"+E1+
B2+"String hql = sbHQL.toString().replace(\"AND\", \"WHERE\");"+E1+
B2+"if(pageBean != null){"+E1+
B3+"return baseDao.find(hql,param, pageBean);"+E1+
B2+"} else{"+E1+
B3+"return baseDao.find(hql,param);"+E1+
B2+"}"+E2+
B1+"}"+E2+
B1+"@Override"+E1+
B1+"public Long get"+cName+"Count("+cName+" "+cname+"){"+E2+
B2+"StringBuffer sbHQL = new StringBuffer(\"SELECT COUNT(*) FROM "+cName+" \");"+E1+
B2+"List<Object> param = new ArrayList<>();"+E1+
B2+"if ("+cname+"!=null) {"+E2+
B2+"}"+E1+
B2+"String hql = sbHQL.toString().replace(\"AND\", \"WHERE\");"+E1+
B2+"return baseDao.count(hql,param);"+E2+
B1+"}"+E2+
B1+"@Override"+E1+
B1+"public void save"+cName+"("+cName+" "+cname+"){"+E1+
B2+"baseDao.save("+cname+");"+E1+
B1+"}"+E2+
B1+"@Override"+E1+
B1+"public "+cName+" get"+cName+"(int id){"+E1+
B2+"return baseDao.get("+cName+".class, id);"+E1+
B1+"}"+E2+
B1+"@Override"+E1+
B1+"public void update"+cName+"("+cName+" "+cname+"){"+E1+
B2+"baseDao.update("+cname+");"+E1+
B1+"}"+E2+
B1+"@Override"+E1+
B1+"public void delete"+cName+"("+cName+" "+cname+"){"+E1+
B2+"baseDao.delete("+cname+");"+E1+
B1+"}"+E2+
"}";
fw.write(code);
fw.flush();
fw.close();
showInfo(fileName);
}
/**
* 第一个字母变为小写<br>
* @Description: TODO
* @param @param str
* @param @return
* @return String
* @throws
* @author micoMo
* @date 2017-9-16
*/
private static String getLowercaseChar(String str){
return str.substring(0,1).toLowerCase()+str.substring(1);
}
/**
* 显示信息
* @Description: TODO
* @param @param info
* @return void
* @throws
* @author micoMo
* @date 2017-9-16
*/
private static void showInfo(String info){
System.out.println("创建文件:"+ info+ "成功!");
}
}
调用示例BuildCodeTest.java (运行即可生成对应的类)
package com.sh.tools;
import com.sh.entity.Customer;
import com.sh.entity.OrderItem;
import com.sh.entity.Product;
import com.sh.entity.Sender;
import com.sh.entity.Supplier;
import com.sh.entity.SupplierUser;
import com.sh.entity.Order;
import com.sh.entity.Type;
public class BuildCodeTest {
public static void main(String[] args) throws Exception{
//右键运行该class可自动生成对应实体类的action、serviceInter、serviceImpl类。
BuildCode.createCodeTool(Customer.class);
BuildCode.createCodeTool(OrderItem.class);
BuildCode.createCodeTool(Product.class);
BuildCode.createCodeTool(Sender.class);
BuildCode.createCodeTool(Supplier.class);
BuildCode.createCodeTool(SupplierUser.class);
BuildCode.createCodeTool(Order.class);
BuildCode.createCodeTool(Type.class);
}
}
图为自动生成的java文件
生成后的Action类示例
package com.sh.action;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.sh.entity.Product;
import com.sh.entity.PageBean;
import com.sh.service.inter.ProductServiceInter;
import com.sh.tools.ResponseTool;
import com.sh.tools.StringTool;
import com.opensymphony.xwork2.ActionSupport;
@Controller("productAction")
@Scope(value="prototype")
public class ProductAction extends ActionSupport implements ServletRequestAware {
@Resource(name="productService")
private ProductServiceInter productService;
JSONObject json = new JSONObject();
String resultCode = "1";// 返回结果 1成功,0失败
String errorMessage = "";// 返回结果错误信息
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String getProduct() throws Exception{
String id = request.getParameter("id");
if(StringTool.isEmpty(id)){
errorMessage += "id不能为空";
resultCode = "0";
}
if("".equals(errorMessage)){
Product n = productService.getProduct(Integer.parseInt(id));
json.put("product", JSONObject.fromObject(n));
}
json.put("errorMessage", errorMessage);
json.put("resultCode", resultCode);
ResponseTool.write(ServletActionContext.getResponse(), json);
return null;
}
public String getProductList() throws Exception{
String page = request.getParameter("page");
String rows = request.getParameter("rows");
String []param={page,rows};
String []pname={"page","rows"};
if(!"".equals(StringTool.isEmpty(param, pname))){
errorMessage += StringTool.isEmpty(param, pname);
resultCode = "0";
}
if ("" == errorMessage) {
Product product = new Product();
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
List<Product> productList = productService.getProductList(product, pageBean);
long total = productService.getProductCount(product);
JSONArray list = JSONArray.fromObject(productList);
json.put("list", list);
json.put("total", total);
}
json.put("errorMessage", errorMessage);
json.put("resultCode", resultCode);
ResponseTool.write(ServletActionContext.getResponse(), json);
return null;
}
}
生成后的ServiceInter类示例
package com.sh.service.inter;
import java.util.List;
import com.sh.entity.PageBean;
import com.sh.entity.Product;
public interface ProductServiceInter {
List<Product> getProductList(Product product, PageBean pageBean);
Long getProductCount(Product product);
void saveProduct(Product product);
void deleteProduct(Product product);
void updateProduct(Product product);
Product getProduct(int id);
}
生成后的ServiceImpl类示例
package com.sh.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.sh.dao.inter.BaseDao;
import com.sh.entity.Product;
import com.sh.entity.PageBean;
import com.sh.service.inter.ProductServiceInter;
@Service("productService")
public class ProductServiceImpl implements ProductServiceInter {
@Resource(name="baseDao")
private BaseDao<Product> baseDao;
@Override
public List<Product> getProductList(Product product, PageBean pageBean){
StringBuffer sbHQL = new StringBuffer("FROM Product ");
List<Object> param = new ArrayList<>();
if (product!=null) {
}
String hql = sbHQL.toString().replace("AND", "WHERE");
if(pageBean != null){
return baseDao.find(hql,param, pageBean);
} else{
return baseDao.find(hql,param);
}
}
@Override
public Long getProductCount(Product product){
StringBuffer sbHQL = new StringBuffer("SELECT COUNT(*) FROM Product ");
List<Object> param = new ArrayList<>();
if (product!=null) {
}
String hql = sbHQL.toString().replace("AND", "WHERE");
return baseDao.count(hql,param);
}
@Override
public void saveProduct(Product product){
baseDao.save(product);
}
@Override
public Product getProduct(int id){
return baseDao.get(Product.class, id);
}
@Override
public void updateProduct(Product product){
baseDao.update(product);
}
@Override
public void deleteProduct(Product product){
baseDao.delete(product);
}
}
项目中引用的其他类就不一一贴代码了,下面是完整的SSH项目
百度网盘:http://pan.baidu.com/s/1pKS2bIN 密码:bi5d