接上java
上一篇解決了建表繁瑣的問題node
接上web
上一篇解決了建表繁瑣的問題sql
這一篇咱們將要解決的問題是:根據bean,自動生成mybatis結構,實現原子的增刪改查操做數據庫
繼續回顧mybatis的結構apache
一個接口對應一個xml,xml裏寫sql語句。那麼咱們將要完成的需求就是:根據bean生成相應的xml和inter瀏覽器
新建共享數據類commonsInter.javamybatis
package plug.gg.commons; public class commonsInter { private static commonsInter va=new commonsInter(); public static commonsInter getObj(){ return va; } /*全部的inter*/ private String[] inter; /*全部的xml*/ private String[] xml; public String[] getInter() { return inter; } public void setInter(String[] inter) { this.inter = inter; } public String[] getXml() { return xml; } public void setXml(String[] xml) { this.xml = xml; } }
生成interapp
package plug.gg.commons; import org.apache.ibatis.annotations.Param; import org.dom4j.DocumentException; import plug.gg.ggScan; import plug.gg.ggThreadInter; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class ggCommonsInter { public List<String> beanList=new LinkedList<String>(); public Map node=new HashMap(); public ggCommonsInter() throws DocumentException{ node=new ggScan().getXml(ggThreadInter.getVa().getContextEvent()); for(String name:ggThreadInter.getVa().getBeanTableName()){ //System.out.println(node.get("page")+"."+name); /*組裝bean路徑*/ beanList.add(node.get("page")+"."+name); } } public void create(){ /*page*/ String packages="package "+node.get("print-inter")+";"; String imports[]=new String[beanList.size()]; /*import*/ String impBatis="import org.apache.ibatis.annotations.Param;import java.util.List;"; for(int i=0;i<beanList.size();i++) { imports[i] = "import " + beanList.get(i) + ";"+impBatis; } /*接口名*/ String function[]=new String[beanList.size()]; String interName[]=new String[beanList.size()]; for(int i=0;i<beanList.size();i++){ function[i]=packages+imports[i]; function[i]=function[i]+"public interface "+ggThreadInter.getVa().getBeanTableName().get(i)+"Dao{\n"; /*數據庫插入的原子方法(即不可再分解的方法)*/ function[i]=function[i]+"int add(@Param(\"name0\")String name,@Param(\"value0\")String value);\n"; /*數據庫刪除的原子方法(即不可再分解的方法)*/ function[i]=function[i]+"int delete(@Param(\"name1\")String name,@Param(\"value1\")String value);\n"; /*數據庫更新的原子方法(即不可再分解的方法)*/ function[i]=function[i]+"int update(@Param(\"name2\")String name,@Param(\"value2\")String value);\n"; /*數據庫查詢整表(即不可再分解的方法)*/ function[i]=function[i]+"List<"+ggThreadInter.getVa().getBeanTableName().get(i)+"> allSelect();\n"; /*數據庫查詢的原子方法(即不可再分解的方法)*/ function[i]=function[i]+""+ggThreadInter.getVa().getBeanTableName().get(i)+" isSelect(@Param(\"name3\")String name,@Param(\"value3\")String value);\n}"; } /*存儲變量*/ commonsInter.getObj().setInter(function); } }
同上生成xmldom
package plug.gg.commons; import org.dom4j.DocumentException; import plug.gg.ggScan; import plug.gg.ggThreadInter; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class ggCommonsXml { public Map node=new HashMap(); public List<String> beanList=new LinkedList<String>(); public ggCommonsXml() throws DocumentException{ node=new ggScan().getXml(ggThreadInter.getVa().getContextEvent()); for(String name:ggThreadInter.getVa().getBeanTableName()){ //System.out.println(node.get("page")+"."+name); beanList.add(node.get("page")+"."+name); } } public void create(){ /*頭部*/ String header="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'>"; String[] fun=new String[beanList.size()]; for(int i=0;i<commonsInter.getObj().getInter().length;i++){ /*namespace*/ fun[i]=header+"<mapper namespace='"+node.get("print-inter")+"."+ggThreadInter.getVa().getBeanTableName().get(i)+"Dao'>\n"; /*和inter對應的sql*/ fun[i]=fun[i]+"<insert id='add'>insert into "+ggThreadInter.getVa().getBeanTableName().get(i)+" (${name0}) values ('${value0}')</insert>\n"; fun[i]=fun[i]+"<delete id='delete'>delete from "+ggThreadInter.getVa().getBeanTableName().get(i)+" where ${name1}='${value1}'</delete>\n"; fun[i]=fun[i]+"<update id='update'>update "+ggThreadInter.getVa().getBeanTableName().get(i)+" set ${name2}='${value2}'</update>\n"; fun[i]=fun[i]+"<select id='allSelect' resultType='"+node.get("page")+'.'+ggThreadInter.getVa().getBeanTableName().get(i)+"'>select * from "+ggThreadInter.getVa().getBeanTableName().get(i)+"</select>\n"; fun[i]=fun[i]+"<select id='isSelect' resultType='"+node.get("page")+'.'+ggThreadInter.getVa().getBeanTableName().get(i)+"'>select * from where ${name3}='${value3}'</select>\n"; fun[i]=fun[i]+"</mapper>"; } /*存儲變量*/ commonsInter.getObj().setXml(fun); } }
一樣的須要一些配置gg.xml
完成以後須要存儲輸出爲.java和.xml
新建工具類printFile.java
package plug.gg.commons; import javax.servlet.ServletContext; import java.io.*; public class printFile { public String newfile(String name,String path,String value) throws FileNotFoundException { OutputStream in=new FileOutputStream(new File(path+"/"+name)); try { OutputStreamWriter out=new OutputStreamWriter(in, "utf-8"); PrintWriter pw=new PrintWriter(out); StringBuilder tes=new StringBuilder(value); pw.println(tes.toString()); pw.flush(); pw.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block System.out.println("error"); } return path+"/"+name; } }
轉存類ggCommonsPrint.java
package plug.gg.commons; import org.dom4j.DocumentException; import plug.gg.ggScan; import plug.gg.ggThreadInter; import sun.applet.Main; import javax.servlet.ServletContextEvent; import java.io.FileNotFoundException; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class ggCommonsPrint { private String printInter; private String printXml; public Map node=new HashMap(); public List<String> beanList=new LinkedList<String>(); public ggCommonsPrint(){ try { node=new ggScan().getXml(ggThreadInter.getVa().getContextEvent()); for(String name:ggThreadInter.getVa().getBeanTableName()){ beanList.add(node.get("page")+"."+name); } } catch (DocumentException e) { e.printStackTrace(); } } public void printInter(){ ServletContextEvent contextEvent=ggThreadInter.getVa().getContextEvent(); String prints=node.get("print-inter").toString().replace(".","/"); String path=contextEvent.getServletContext().getRealPath("WEB-INF/classes/"+prints+""); /*源文件位置*/ String disPath=contextEvent.getServletContext().getRealPath(""); disPath=disPath.substring(0,disPath.indexOf(node.get("web-content").toString())+node.get("web-content").toString().length()); prints=prints.replace("/","\\"); disPath=disPath+"\\src\\main\\java\\"+prints; for(int i=0;i<commonsInter.getObj().getInter().length;i++){ String name=ggThreadInter.getVa().getBeanTableName().get(i)+"Dao.java"; String value=commonsInter.getObj().getInter()[i]; try { /*String javaFile=new printFile().newfile(name,path,value); new compiler().tall(javaFile);*/ new printFile().newfile(name,disPath,value); } catch (FileNotFoundException e) { e.printStackTrace(); } } } public void printXml(){ ServletContextEvent contextEvent=ggThreadInter.getVa().getContextEvent(); String prints=node.get("print-xml").toString().replace(".","/"); String path=contextEvent.getServletContext().getRealPath("WEB-INF/classes/"+prints+""); String disPath=contextEvent.getServletContext().getRealPath(""); disPath=disPath.substring(0,disPath.indexOf(node.get("web-content").toString())+node.get("web-content").toString().length()); prints=prints.replace("/","\\"); /*由於是maven結構因此是src/main/java*/ disPath=disPath+"\\src\\main\\java\\"+prints; for(int i=0;i<commonsInter.getObj().getInter().length;i++){ String name=ggThreadInter.getVa().getBeanTableName().get(i)+"Mapper.xml"; String value=commonsInter.getObj().getXml()[i]; try { /*String xmlFile=new printFile().newfile(name,path,value);*/ new printFile().newfile(name,disPath,value); } catch (FileNotFoundException e) { e.printStackTrace(); } } } }
在ggLoderListener.java裏調用
package plug.gg; import org.dom4j.DocumentException; import plug.gg.commons.ggCommonsInter; import plug.gg.commons.ggCommonsPrint; import plug.gg.commons.ggCommonsXml; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class ggLoaderListener extends ggThreadInter implements ServletContextListener{ public ggLoaderListener() { } @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent arg0) { new ggThreadBean().run(arg0); ggCreateTableSql.getObj().init(ggThreadInter.getVa().getSqlTable()); ggThreadInter.getVa().setContextEvent(arg0); try { ggCommonsInter commonsInter=new ggCommonsInter(); commonsInter.create(); ggCommonsXml commonsXml=new ggCommonsXml(); commonsXml.create(); new ggCommonsPrint().printInter(); new ggCommonsPrint().printXml(); } catch (DocumentException e) { e.printStackTrace(); } } }
重啓服務觀察
已生成inter和xml
調試是否生效
瀏覽器打開相應地址,觀察數據庫
成功
到如今已經初步將開發工做大大下降,可是仍有瑕疵,由於生成的sql是原子操做,好比咱們要插入6個字段時要寫一個add和5個update
下節將在此基礎上再次封裝,將原子操做進行合成。