逆向工程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. ...
随机推荐
- Windows服务器修改网站上传文件的大小限制
ASP程序 方法一: 修改该网站的的最大上传文件的大小限制 在Windows server上会出现上传大小受限制的问题,这是由于windows server的IIS管理器做了限制所致,IIS默认设置是 ...
- 校园管家(Android开发团队项目)NABCD
N(Need)需求: 现如今数据越来越零碎化,繁杂化,身为在校大学生的我们也因此对于时间的利用率也相应减少,为了时间的充分利用,减少在冗杂的信息中耽误的时间,充分利用大学资源,因此我们打算做一个专门发 ...
- frag
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...
- Mabatis面试题
Mybatis面试题 1请写出Mybatis核心配置文件MyBatis-config.xml的内容? <?xml version="1.0" encoding="U ...
- 关于E-R图
E-R图 简介: E-R图也称实体-联系图(Entity Relationship Diagram),提供了表示实体类型.属性和联系的方法.用来描述现实世界的概念模型.它是描述现实世界概念结构模型的有 ...
- Beff的学习
Beff介绍 BeEF,全称The Browser Exploitation Framework,是一款针对浏览器的渗透测试工具. 首先打开kali,直接点击beef图标打开beef 浏览器会默认弹出 ...
- tnsping无法ping通的问题,TNS-12535 TNS操作超时 (服务器环境:window server 2008R2 数据库环境:oracle 11 g)
今天新搭建一个测试用的数据库服务器,操作系统为WIN server 2008 r2 版本.系统内已安装oracle 11g database,数据库服务端已配置完毕,监听listener已开启. 我在 ...
- mvc框架模式
首先分为3个板块 路由的api相当于一个域名. 根据当前地址在执行路由里的代码; 逻辑层: 书写业务逻辑的都代码都放在controller层 数据处理层: model 写数据的增删改查方法,导出一般供 ...
- 信号监测---verilog
信号监测---verilog 此模块用于监测某一信号源是否持续稳定的传送. 监测思路:监测信号源高电平或者低电平的宽度是否始终保持一致(一定范围内允许有误差) `timescale 1ns / 1ps ...
- 【Dubbo源码学习】负载均衡算法(1)-随机算法
/** * random load balance. * */public class RandomLoadBalance extends AbstractLoadBalance { public s ...