本文主要是介绍自定义一键创建controller,service,serviceImpl,mapper,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//在开发过程中比如创建controller(控制层)我们避免不了的重复创建接口捕获异常为接口注释,这些虽然在编译器可以生成,但是总是不那么自由,尤其不符合个人习惯,因此我自己定义了一套,可以指定地址,指定名称,可以创建单个,也可以一次都创建,完全是为了自己的开发需要:
这个要额外的一个jar包mysql-connector-java-5.1.34.jar,因为在创建pojo类时,我是直接通过访问数据的dll文件读出表的数据结构的。
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.*;/*** Created by ChaoZhang on 2019-03-14 10:55*/public class create {/*** @type* all* domain* service* serviceImpl* ctrl* mapper* mapper.xml*/public static String type = "all"; //创建类型public static final String path = "E:\\CreateDomainServiceMapper\\src\\"; //创建路径及内容public static final String fileName = "SpaceAvShare"; //创建的文件的名字public static final String owner = "ChaoZhang"; //注释作者public static final Date date = new Date(); //创建时间static class classBuffer{public File file;public StringBuffer buffer;classBuffer(File file, StringBuffer buffer){this.file = file;this.buffer = buffer;}}/*** 自定义创建作者和注释加入到缓冲并返回* example* created by ChaoZhang onThu Mar 14 14:05:05 CST 2019*/public static StringBuffer writeOwner(){StringBuffer buffer = new StringBuffer();buffer.append("/**\r\n");buffer.append(" *created by " + owner + " on " + date + "\r\n");buffer.append(" */\r\n");buffer.append("\r\n");return buffer;}/*** 自定义创建class或者interface加入到缓冲并返回* @return* @throws Exception*/public static StringBuffer writeClassOrInterface(StringBuffer buffer) throws Exception{if(type.equals(new String("domain"))){buffer.append("public class " + fileName + " {" + "\r\n");JdbcConnection jdbcConnection = new JdbcConnection();System.out.println("*** writeClassOrInterface " + JdbcConnection.result.toString());buffer.append(JdbcConnection.result.toString());}else if(type.equals(new String("service"))){buffer.append("public interface " + fileName + "Service" + " {");}else if(type.equals(new String("ctrl"))){buffer.append("@Controller\r\n");buffer.append("@RequestMapping(value = \"\")\n");buffer.append("public class " + fileName + "Ctrl" + " {");buffer.append("\r\n\r\n");buffer.append("/*JSONObject jsonObject = new JSONObject();\n" +" Integer state = Integer.valueOf(1000);\n" +" try{\n" +"\n" +" }catch (Exception e){\n" +" e.printStackTrace();\n" +" jsonObject.put(\"msg\", \"sql报错\");\n" +" jsonObject.put(\"erro\", e.getCause());\n" +" state = Integer.valueOf(2000);\n" +" }\n" +" jsonObject.put(\"state\", state);\n" +" return jsonObject;*/");}else if(type.equals(new String("serviceImpl"))){buffer.append("@Service\r\n");buffer.append("public class " + fileName + "ServiceImpl " + "implements " + fileName + "Service" + " {");}else if(type.equals(new String("mapper"))){buffer.append("public interface " + fileName + "Mapper" + " {");}if(type.equals(new String("xml"))){buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n");buffer.append("<!DOCTYPE mapper PUBLIC \"-//mybatis.org//DTD Mapper 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-mapper.dtd\" >\r\n");buffer.append("<mapper namespace=\"com.vv.space.mapper.DeviceUserMapper\" >\r\n");buffer.append("\r\n\r\n");buffer.append("</mapper>\r\n");return buffer;}buffer.append("\r\n");buffer.append("\r\n");buffer.append("}");return buffer;}/*** 创建pojo类* domain文件夹*/public static classBuffer createDomain(){File file = new File(path + fileName + ".java");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}/*** 创建业务service* service文件夹*/public static classBuffer createService(){File file = new File(path + fileName + "Service" +".java");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}/*** 创建Dao层服务* mapper文件夹*/public static classBuffer createMapper(){File file = new File(path + fileName + "Mapper" + ".java");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}/*** 创建业务实现类* impl文件夹*/public static classBuffer createserviceImpl(){File file = new File(path + fileName + "ServiceImpl" + ".java");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}/*** 创建控制层实现类* controller文件夹*/public static classBuffer createCtrl(){File file = new File(path + fileName + "Ctrl" + ".java");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}/*** 创建.xml文件* @throws Exception*/public static classBuffer createXml(){File file = new File(path + fileName + "Mapper" + ".xml");//写文件注释和创建者StringBuffer buffer = writeOwner();classBuffer classBuffer = new classBuffer(file, buffer);return classBuffer;}public static void create() throws Exception{classBuffer classBuffer = null;switch (type){case "domain":classBuffer = createDomain();break;case "service":classBuffer = createService();break;case "serviceImpl":classBuffer = createserviceImpl();break;case "ctrl":classBuffer = createCtrl();break;case "mapper":classBuffer = createMapper();break;case "xml":classBuffer = createXml();break;default:System.out.println("未知类型");}if(classBuffer.file.exists()){System.out.println("*** 该" + type + "文件已经存在 ***");return ;}else{classBuffer.file.createNewFile(); //创建文件//创建class or interfaceStringBuffer buffer1 = writeClassOrInterface(classBuffer.buffer);FileOutputStream fs = new FileOutputStream(classBuffer.file); //创建输出流输出到文件PrintWriter pw = new PrintWriter(fs); //创建打印对象将字符写入输出流pw.write(buffer1.toString()); //将字符写入输出流pw.flush(); //强制将缓冲全部输出到指定文件pw.close();}}//定义一个连接类static class JdbcConnection{//创建一个用于连接的对象Connection ct = null;//创建一个用于发送sql语句的对象PreparedStatement ps = null;//创建一个用于接收结果集的对象ResultSet rs = null;//属性public static StringBuffer result = new StringBuffer("");//默认构造函数public JdbcConnection(){try {//加载驱动Class.forName("org.gjt.mm.mysql.Driver");//得到连接ct = DriverManager.getConnection("jdbc:mysql://114.112.92.135:3307/space?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8","songod", "6Kx2n9usEP96K" );//查询String str = "SELECT COLUMN_NAME name , COLUMN_TYPE type, COLUMN_COMMENT comment FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'space' AND TABLE_NAME = 'space_av';";ps = ct.prepareStatement(str);//得到结果rs = ps.executeQuery();String s = null;while(rs.next()){s = "private " + rs.getString(2) + " " + rs.getString(1) + ";" + " //" + rs.getString(3);System.out.println("private " + rs.getString(2) + " " + rs.getString(1) + ";" + " //" + rs.getString(3));this.result.append("\r\n");this.result.append("\r\n");this.result.append(" ");this.result.append(s);this.result.append("\r\n");}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{try {//关闭资源if(rs != null){rs.close();}if(ps != null){ps.close();}if(ct != null){ct.close();}} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}}public static void main(String[] args) throws Exception{/*** 创建*/System.out.println("*** 开始创建 ***");List<String> list = new ArrayList<String>();if(type.equals("all")){list.add("domain");list.add("service");list.add("ctrl");list.add("mapper");list.add("serviceImpl");list.add("xml");}else{create();}for(String s:list){type = s;create();}System.out.println("*** 创建完成 ***");}}
比如我要创建的叫SpaceAvShare那么如下图:
从pojo类,ctrl,mapper,service都创建好了,这里要提一下创建pojo类需要连接数据库的连接,我将数据库的某个表的dll文件读出创建pojo类
很简单,完全符合我自己的习惯
这篇关于自定义一键创建controller,service,serviceImpl,mapper的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!