逆向工程vgenerator(三)
前言
该项目的最后一篇博文,最终构建,写入文件,整体项目流程将在本片文章中写出。
jdbcType
/**
*@author vvxtoys
*mysql单位 -> jdbcType
*/
package cc.vvxtoys.bean;
public enum JdbcType {
DATE(",date,year,"),
LONGVARCHAR(",longtext,text,mediumtext,"),
INTEGER(",int,mediumint,"),
REAL(",float,"),
OTHER(",point,multipoint,multilinestring,multipolygon,polygon,geometrycollection,linestring,"),
TINYINT(",tinyint,"),
BIGINT(",bigint,"),
TIMESTAMP(",datetime,timestamp,"),
VARCHAR(",varchar,tinytext,"),
BINARY(",tinyblob,binary,geometry,"),
CHAR(",enum,char,set,"),
DOUBLE(",double,"),
BIT(",bit,"),
SMALLINT(",smallint,"),
LONGVARBINARY(",mediumblob,blob,longblob,"),
DECIMAL(",decimal,"),
VARBINARY(",varbinary,"),
TIME(",time,");
private String type;
private JdbcType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public static String getJdbcType(String dbtype){
if(dbtype.indexOf("(")!=-1){
dbtype = dbtype.substring(0,dbtype.indexOf("("));
}
dbtype = ","+dbtype+",";
for(JdbcType jdbc:JdbcType.values()){
if(jdbc.getType().indexOf(dbtype)!=-1){
return jdbc.name();
}
}
return "OTHER";
}
}
生成xml
/**
*@author vvxtoys
*添加xml节点
*/
package cc.vvxtoys.vgenerator;
import java.util.Map;
import cc.vvxtoys.bean.XMLBean;
import cc.vvxtoys.factory.MapperFactory;
import cc.vvxtoys.utils.Vconst;
public class SqlGenerator {
private XMLBean xml;
private int k_size;//主键数
private int c_size;//去除主键列数
private String column;
private String endText = "";//最后一行 where id = ""
public static Map<String,String> keys; //主键 name = jdbctype
public static Map<String,String> columns;//列 name = jdbctype
public SqlGenerator(XMLBean xml) {
this.xml = xml;
}
public void generatorSql(){
if(k_size>0){
generator1();
}else{
generator2();
}
}
private void generator1(){
addResultMap();
addSql();
addInsert1();
addInsert2();
addSelect();
addDelete();
addUpdate1();
addUpdate2();
}
private void generator2(){
addResultMap();
addSql();
addInsert1();
addInsert2();
}
private void addResultMap(){
column = "";
int count = 0;
XMLBean resultMap = new XMLBean("resultMap");
xml.addElement(resultMap);
resultMap.init();
resultMap.addAttribute("id", "BaseResultMap");
resultMap.addAttribute("type", xml.getPojo());
if(k_size>0){
for(String key:keys.keySet()){
XMLBean id = new XMLBean("id");
resultMap.addElement(id);
id.init();
id.addAttribute("column", key);
id.addAttribute("property", key);
id.addAttribute("jdbcType", keys.get(key));
if(count>10){
column+="\n";
count=0;
}
column+=","+key;
count++;
setEndText(key, keys.get(key));
}
}
if(c_size>0){
for(String key:columns.keySet()){
XMLBean result = new XMLBean("result");
resultMap.addElement(result);
result.init();
result.addAttribute("column", key);
result.addAttribute("property", key);
result.addAttribute("jdbcType", columns.get(key));
if(count>10){
column+="\n";
count=0;
}
column+=","+key;
count++;
}
}
}
private void addSql(){
XMLBean sql = new XMLBean("sql");
xml.addElement(sql);
sql.init();
sql.addAttribute("id", "Base_Column_List");
sql.addText(column.substring(1));
}
private void addSelect(){
XMLBean select = new XMLBean("select");
xml.addElement(select);
select.init();
select.addAttribute("id", Vconst.select);
select.addAttribute("resultMap", "BaseResultMap");
if(k_size==1){
String prefix = "java.lang.";
String type = MapperFactory.ktypes.get(0);
if(type.equalsIgnoreCase("date")){
type = "java.util.Date";
}else{
type = prefix+type;
}
select.addAttribute("parameterType", type);
}
select.addText("select");
XMLBean include = new XMLBean("include");
select.addElement(include);
include.init();
include.addAttribute("refid", "Base_Column_List");
select.addEndText("from "+xml.getTname());
select.addText(getEndText());
}
private void addDelete(){
XMLBean delete = new XMLBean("delete");
xml.addElement(delete);
delete.init();
delete.addAttribute("id", Vconst.delete);
if(k_size==1){
String prefix = "java.lang.";
String type = MapperFactory.ktypes.get(0);
if(type.equalsIgnoreCase("date")){
type = "java.util.Date";
}else{
type = prefix+type;
}
delete.addAttribute("parameterType", type);
}
delete.addText("delete from "+xml.getTname());
delete.addText(getEndText());
}
private void addInsert1(){
XMLBean insert = new XMLBean("insert");
xml.addElement(insert);
insert.init();
insert.addAttribute("id", Vconst.insert);
insert.addAttribute("parameterType", xml.getPojo());
String attr = null;
String jdbc = null;
int count1 = 0;
int count2 = 0;
if(k_size>0){
for(String key:keys.keySet()){
if(attr==null){
attr = "(";
}else{
attr+=",";
}
if(jdbc==null){
jdbc = "(";
}else{
jdbc+=",";
}
if(count1>10){
attr+="\n";
count1=0;
}
if(count2>3){
jdbc+="\n";
count2=0;
}
count1++;
count2++;
attr+=key;
jdbc+="#{"+key+","+"jdbcType="+keys.get(key)+"}";
}
}
if(c_size>0){
for(String key:columns.keySet()){
if(attr==null){
attr = "(";
}else{
attr+=",";
}
if(jdbc==null){
jdbc = "(";
}else{
jdbc+=",";
}
if(count1>10){
attr+="\n";
count1=0;
}
if(count2>3){
jdbc+="\n";
count2=0;
}
count1++;
count2++;
attr+=key;
jdbc+="#{"+key+","+"jdbcType="+columns.get(key)+"}";
}
}
attr+=")";
jdbc+=")";
insert.addText("insert into "+xml.getTname());
insert.addText(attr);
insert.addText("values");
insert.addText(jdbc);
}
private void addInsert2(){
XMLBean insert = new XMLBean("insert");
xml.addElement(insert);
insert.init();
insert.addAttribute("id", Vconst.insert1);
insert.addAttribute("parameterType", xml.getPojo());
insert.addText("insert into "+xml.getTname());
XMLBean trim = new XMLBean("trim");
insert.addElement(trim);
trim.init();
trim.addAttribute("prefix", "(");
trim.addAttribute("suffix", ")");
trim.addAttribute("suffixOverrides", ",");
if(c_size>0){
for(String key:columns.keySet()){
XMLBean test = new XMLBean("if");
trim.addElement(test);
test.init();
test.addAttribute("test", key+" != null");
test.addText(key+",");
}
}
XMLBean trim1 = new XMLBean("trim");
insert.addElement(trim1);
trim1.init();
trim1.addAttribute("prefix", "values (");
trim1.addAttribute("suffix", ")");
trim1.addAttribute("suffixOverrides", ",");
if(c_size>0){
for(String key:columns.keySet()){
XMLBean test = new XMLBean("if");
trim1.addElement(test);
test.init();
test.addAttribute("test", key+" != null");
test.addText("#{"+key+",jdbcType="+columns.get(key)+"},");
}
}
insert.addEndText(getEndText());
}
private void addUpdate1(){
XMLBean update = new XMLBean("update");
xml.addElement(update);
update.init();
update.addAttribute("id", Vconst.update);
update.addAttribute("parameterType", xml.getPojo());
update.addText("update "+xml.getTname());
String content = null;
if(c_size>0){
for(String key:columns.keySet()){
if(content==null){
content = "set ";
}else{
content+=",";
}
content+=key+" = "+"#{"+key+",jdbcType="+columns.get(key)+"}\n";
}
}
update.addText(content);
update.addText(getEndText());
}
private void addUpdate2(){
XMLBean update = new XMLBean("update");
xml.addElement(update);
update.init();
update.addAttribute("id", Vconst.update1);
update.addAttribute("parameterType", xml.getPojo());
update.addText("update "+xml.getTname());
XMLBean set = new XMLBean("set");
update.addElement(set);
set.init();
if(c_size>0){
for(String key:columns.keySet()){
XMLBean test = new XMLBean("if");
set.addElement(test);
test.init();
test.addAttribute("test", key+" != null");
test.addText(key+" = #{"+key+",jdbcType="+columns.get(key)+"},");
}
}
update.addEndText(getEndText());
}
private void setEndText(String key,String value){
if(!"".equals(endText)){
endText +=",";
}
endText+=key+" = "+"#{"+key+","+"jdbcType="+value+"}";
}
private String getEndText(){
return "where "+endText;
}
public void setK_size(int k_size) {
this.k_size = k_size;
}
public void setC_size(int c_size) {
this.c_size = c_size;
}
}
write类
/**
*@author vvxtoys
*写入项目
*/
package cc.vvxtoys.vgenerator;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import cc.vvxtoys.bean.ClassBean;
import cc.vvxtoys.bean.MapperBean;
import cc.vvxtoys.bean.XMLBean;
import cc.vvxtoys.utils.Common;
public class VgeneratorWrite {
public static List<ClassBean> clazzs = new ArrayList<ClassBean>();
public static List<MapperBean> mappers = new ArrayList<MapperBean>();
public static List<XMLBean> xmls = new ArrayList<XMLBean>();
public void generatorFile() {
generatorClazz();
generatorMapper();
generatorXML();
}
private void generatorClazz() {
if (clazzs.size() > 0) {
for (ClassBean bean : clazzs) {
try {
File file = new File(bean.getFileName());
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
if(file.exists()){
file.delete();
}
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Common.writeFile(bean.getFileName(), bean.getContext());
System.out.println("generator "+bean.getFileName()+" done");
}
}
}
private void generatorMapper() {
if (mappers.size() > 0) {
for (MapperBean bean : mappers) {
try {
File file = new File(bean.getFileName());
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
if(file.exists()){
file.delete();
}
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Common.writeFile(bean.getFileName(), bean.getContext());
System.out.println("generator "+bean.getFileName()+" done");
}
}
}
private void generatorXML() {
if (xmls.size() > 0) {
for (XMLBean bean : xmls) {
try {
File file = new File(bean.getFileName());
File fileParent = file.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
if(file.exists()){
file.delete();
}
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
Common.writeFile(bean.getFileName(), bean.toString());
System.out.println("generator "+bean.getFileName()+" done");
}
}
}
}
generator类
/**
*@author vvxtoys
*调用该类实现构建
*/
package cc.vvxtoys.vgenerator;
import cc.vvxtoys.config.Engine;
import cc.vvxtoys.factory.VgeneratorFactory;
public class Vgenerator {
private VgeneratorFactory factory;
public Vgenerator() {
factory = new VgeneratorFactory();
}
public Vgenerator(String configpath){
Engine.setConfigFile(configpath);
factory = new VgeneratorFactory();
}
public void help(){
System.out.println("url :数据库url");
System.out.println("name:服务名称");
System.out.println("user:用户名");
System.out.println("pass:密码");
System.out.println("targetProject:生成相对路径");
System.out.println("targetPackage:实体类包名");
System.out.println("targetMapper : mapper包名");
System.out.println("targetXML : xml包名");
System.out.println("tableName : 表名");
System.out.println("objectName : 实体名");
}
public void generator(){
factory.runProperties();
factory.close();
}
public void generatorAllFromDB(){
factory.runDB();
factory.close();
}
}
generator工厂
package cc.vvxtoys.factory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cc.vvxtoys.bean.BaseBean;
import cc.vvxtoys.bean.ClassBean;
import cc.vvxtoys.bean.DBtype;
import cc.vvxtoys.bean.JdbcType;
import cc.vvxtoys.bean.MapperBean;
import cc.vvxtoys.bean.XMLBean;
import cc.vvxtoys.config.DB;
import cc.vvxtoys.config.Engine;
import cc.vvxtoys.utils.Common;
import cc.vvxtoys.utils.Vconst;
import cc.vvxtoys.vgenerator.SqlGenerator;
import cc.vvxtoys.vgenerator.VgeneratorWrite;
public class VgeneratorFactory {
private VgeneratorWrite write = new VgeneratorWrite();
private Engine engine = Engine.getInstance();
private ClassFactory cF;
private MapperFactory mF;
private XMLFactory xF;
public void runProperties(){
DB db = DB.getInstance();
String [] tableNames = Engine.tableName;
String [] objectNames = Engine.objectName;
if(tableNames.length!=objectNames.length){
for(int i=0;i<tableNames.length;i++){
startOperator(tableNames[i], Common.upInitials(tableNames[i]),db);
}
}else{
for(int i=0;i<tableNames.length;i++){
startOperator(tableNames[i], objectNames[i],db);
}
}
}
public void runDB(){
DB db = DB.getInstance();
try{
List<String> list = db.getTables();
for(String s:list){
startOperator(s, Common.upInitials(s), db);
}
}catch(Exception e){
e.printStackTrace();
}
}
private void startOperator(String tname,String oname,DB db){
SqlGenerator.keys = new HashMap<String, String>();
SqlGenerator.columns = new HashMap<String, String>();
ClassBean bean = new ClassBean(oname);
MapperBean mbean = new MapperBean(oname);
XMLBean xbean = new XMLBean(Vconst.DEFAULT_XML_ROOT,tname,oname);
List<String> ps = db.getPrimaryKey(tname);
if(ps==null||ps.size()==0){
mbean.setPrimary(false);
}
cF = new ClassFactory(bean);
mF = new MapperFactory(mbean);
xF = new XMLFactory();
int k_size = 0;
int c_size = 0;
List<String> kname = new ArrayList<String>();
List<String> ktype = new ArrayList<String>();
boolean flag = false;//是否存在时间格式
Map<String,String> cs = db.getColumns(tname);
for(Map.Entry<String, String> m:cs.entrySet()){
String name = m.getKey();
String type = m.getValue();
if(mbean.isPrimary()&&ps.contains(name)){
kname.add(name);
ktype.add(DBtype.getModelType(type));
SqlGenerator.keys.put(name, JdbcType.getJdbcType(type));
k_size++;
}else{
SqlGenerator.columns.put(name, JdbcType.getJdbcType(type));
c_size++;
}
cF.addAttribute(DBtype.getModelType(type), name);
if(type.equalsIgnoreCase("date")){
flag = true;
}
}
bean.setDependency(flag);
cF.autoAddMethod();
cF.addConstructor();
cF.loadClass();
MapperFactory.knames = kname;
MapperFactory.ktypes = ktype;
mF.loadMapper();
xF.createDoc(xbean,k_size,c_size);
write.generatorFile();
}
public void close(){
engine.clear();
}
}
流程分析
实例化Vgenerator g
- 实例化 VgeneratorFactory
- 实例化 VgeneratorWrite
- 实例化 Engine
- 读取配置文件,加载文件保存到静态属性
- 无参默认使用vgenerator.properties
- 有参方法设置自定义配置文件,该方法会设置Engine的isDefault属性
- 实例化 VgeneratorFactory
调用g.help
- 输出配置文件注释
调用g.generator() 读取配置文件中的表(支持多表,英文逗号隔开),生成对应的文件
- factory.runProperties()
- DB实例化
- 获得表名和实体类名
- 开始构建 startOperator()
- 获得主键和所有列,获得列名和类型,保存到SqlGenerator和MapperFactory
- 判断是否存在Date类型 设置classBean的dependency属性
- ClassFactory 生成类文本保存到 classbean的context
- VgeneratorWrite 保存classbean
- MapperFactory根据主键生成mapper文本保存到mapperbean的context
- VgeneratorWrite 保存mapperbean
- xmlFactory创建文本
- SqlGenerator根据主键生成对应xml文本保存到xmlbean
- VgeneratorWrite 保存xmlbean
- SqlGenerator根据主键生成对应xml文本保存到xmlbean
- VgeneratorWrite generatorFile生成文件
- 开始构建 startOperator()
- factory.clear() 结束
- factory.runProperties()
调用g.generatorAllFromDB() 构建该数据库所有表,除了需要获取所有表,其他流程都一样。
结语
第一个自己写的小项目,虽然有瑕疵,但是感觉还是蛮充实的。下一步需要加强技术了,看看编程思想和设计模式。
逆向工程vgenerator(三)的更多相关文章
- 逆向工程vgenerator(二)
前言 接上篇,这一篇主要的工具类和工厂类,包括数据库方法.通用方法,三个工厂. 常量 /** *author vvxtoys *默认xml开头 *文档分隔 *默认方法名 */ package cc.v ...
- 逆向工程vgenerator(一)
前言 想要自己实现一个mybatis-generator类似的轮子,目前只实现MySQL部分的方法.利用下班时间,写了一个小项目,实现了这个功能.我准备分成三篇博客来写这个东西. 基类 /** *基类 ...
- VP-UML系统建模工具研究
一.基本信息 标题:VP-UML系统建模工具研究 时间:2014 出版源:软件工程师 领域分类:面向对象:CASE:UML:系统建模: 二.研究背景 问题定义:VP-UML系统建模的主要特点 难点:运 ...
- SSM Spring SpringMVC Mybatis框架整合Java配置完整版
以前用着SSH都是老师给配好的,自己直接改就可以.但是公司主流还是SSM,就自己研究了一下Java版本的配置.网上大多是基于xnl的配置,但是越往后越新的项目都开始基于JavaConfig配置了,这也 ...
- Java商城秒杀系统的设计与实战视频教程(SpringBoot版)
课程目标掌握如何基于Spring Boot构建秒杀系统或者高并发业务系统,以及构建系统时采用的前后端技术栈适用人群Spring Boot实战者,微服务或分布式系统架构实战者,秒杀系统和高并发实战者,中 ...
- 开发工具:Mybatis.Plus.插件三种方式的逆向工程
本文源码:GitHub·点这里 || GitEE·点这里 一.逆向工程简介 在Java开发中,持久层最常用的框架就是mybatis,该框架需要编写sql语句,mybatis官方提供逆向工程,可以把数据 ...
- springboot整合mybatis增删改查(三):mybatis逆向工程
上一篇已经把项目基本框架完善,接下来就是利用Mybatis Generator逆向工程进行mybatis的整合. 我们在创建项目开始的时候已经勾选web,mybatis,sql等,但是这些依赖还是不够 ...
- 逆向工程学习第三天--另外一个ShellCode
上周自己打造的添加用户的shellcode太长,不过当时主要目的是为了锻炼手动asm,熟悉一些复杂的参数类型如何手动进行构造,然后通过堆栈传递. 接下来就打造一个弹计算器的shellcode来进行接下 ...
- mybatis入门基础(九)----逆向工程
一.什么是逆向工程 mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml.po. ...
随机推荐
- pycharm配置tensorflow环境 适用于Python3.6 CPU
一.环境 基于安装Python3.6以及pycharm. 二.在项目设置里配置编译环境 打开pycharm新建一个项目. 打开pycharm->file->setting->proj ...
- 多系统引导 Grub
目录 1. 多系统引导程序 1.1 简介 1.2 Grub History 1.3 支持Windows系统下安装的grub版本 1.4多系统引导程序对比: 1.5 相关参考 1.5.1 Grub 2 ...
- 2019嘉韦思杯线上初赛writeup
1 土肥原贤二 看到页面怀疑是sql注入,写了个4'进去就发生报错.could not to the database You have an error in your SQL syntax; ch ...
- 简单kmp算法(poj3461)
题目简述: 给你两个字符串p和s,求出p在s中出现的次数. 思路简述: 在介绍看BF算法时,终于了解到了大名鼎鼎的KMP算法,结果属于KMP从入门到放弃系列,后来看了几位大神的博客,似乎有点懂了.此题 ...
- python中的time模块和datetime模块
>>> import time>>> time.time()1511330865.656656>>> time.localtime(time.ti ...
- 批量找注入 python3+sqlmap结合
注入一直都是用sqlmap 导致本来就不怎么精通的手工注入现在就忘的一干二净 想实战练习 却一时又找不到有注入的网站 于是便有了这篇文章 想找个批量获取域名链接的工具 但都是只是获取域名而 ...
- JavaException的使用
(第一个文件 MyException.java) package exception;import java.io.*; //先创建自己的异常类继承与Exception的类 public class ...
- redux源码解读(二)
之前,已经写过一篇redux源码解读(一),主要分析了 redux 的核心思想,并用100多行代码实现一个简单的 redux .但是,那个实现还不具备合并 reducer 和添加 middleware ...
- voc-fcn-alexnet网络结构理解
一.写在前面 fcn是首次使用cnn来实现语义分割的,论文地址:fully convolutional networks for semantic segmentation 实现代码地址:https: ...
- 关于微信小程序获取二维码的踩坑记录
1.踩坑需求:获取小程序的二维码 2.踩坑接口: https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN3 踩坑代码 pu ...