数据库表生成实体 包括注释 并生成mybatis Mapper映射 加入注释 通用SQL查询映射

为了解决频繁修改数据库表后需要大量更新实体类、Mapper接口和XML配置的问题,作者创建了一个实用工具,能自动完成以下工作:1. 从数据库表生成带有注释的实体类;2. 生成带泛型的Mapper接口;3. 生成字段对应的XML配置;4. 添加分页映射;5. 自动生成CRUD操作的SQL,包括分页查询。此工具能节省大量时间,尤其适用于拥有大量表的项目。
摘要由CSDN通过智能技术生成

想着自己几次修改数据库表,每次修改数据库表,跟着需要修改一堆东西,现在项目近40张表,如果一个个写实体,写映射文件,写查询SQL等等,那还了得。

索性写了一个超级工具!可以一步生成:

1. 数据库表到实体Bean,注意这里包括了comment,当做字段注释,包含序列化及annotation

2. 对应Mapper接口生成,包括泛型

3. 对应字段与数据库字段对应xml配置

4. 分页映射

5. 通用CRUD 查询,删除,修改,增加等SQL,包括分页


这个类我没写注释,相信大家很快就能够明白,大家随便修改修改就可以拿去自己用了!会至少为你省下一天时间,而且不会然你心烦,表越多越好。。。


package com.bling.saysays.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EntityUtil {
	
	private String type_char = "char";
	private String type_date = "date";
	private String type_int = "int";
	private String type_text = "text";
	
	private String bean_path = "d:/entity_bean";
	private String mapper_path = "d:/entity_mapper";
	
	private String bean_package = "com.bling.saysays.bean";
	private String mapper_package = "com.bling.saysays.mapper";
	private String mapper_extends = "SqlMapper";
	
	private String driverName = "com.mysql.jdbc.Driver";
	private String user = "root";
	private String password = "84004880";
	private String url = "jdbc:mysql://localhost:3306/saysays";
	
	private String tableName;
	private String beanName;
	private String mapperName;
	
	private Connection conn;
	
	private void init() throws ClassNotFoundException, SQLException{
		Class.forName(driverName);
		conn = DriverManager.getConnection(url, user, password);
	}
	
	private List<String> getTables() throws SQLException{
		List<String> tables = new ArrayList<String>();
		PreparedStatement pstate = conn.prepareStatement("show tables");
		ResultSet results = pstate.executeQuery();
		while(results.next()){
			tables.add(results.getString(1));
		}
		return tables;
	}
	
	private void processTable(String table){
		StringBuffer sb = new StringBuffer(table.length());
		table = table.toLowerCase();
		String[] tables = table.split("_");
		String temp = null;
		for(int i=1;i<tables.length;i++){
			temp = tables[i].trim();
			sb.append(temp.substring(0,1)
				.toUpperCase())
				.append(temp.substring(1));
		}
		beanName =  sb.toString();
		mapperName = beanName+"Mapper";
	}
	
	private String processType(String type){
		if(type.indexOf(type_char)>-1){
			return "String";
		}else if(type.indexOf(type_int)>-1){
			return "Integer";
		}else if(type.indexOf(type_date)>-1){
			return "Date";
		}else if(type.indexOf(type_text)>-1){
			return "String";
		}
		return null;
	}
	
	private String processField(String field){
		StringBuffer sb = new StringBuffer(field.length());
		field = field.toLowerCase();
		String[] fields = field.split("_");
		String temp = null;
		sb.append(fields[0]);
		for(int i=1;i<fields.length;i++){
			temp = fields[i].trim();
			sb.append(temp.substring(0,1).toUpperCase())
			.append(temp.substring(1));
		}
		return sb.toString();
	}
	
	private String processResultMapId(String beanName){
		return beanName.substring(0,1).toLowerCase() + beanName.substring(1);
	}
	
	private void outputAuthor(BufferedWriter bw,String text) throws IOException{
		bw.newLine();
		bw.newLine();
		bw.write("/**");bw.newLine();
		bw.write(" *<pre>");bw.newLine();
		bw.write(" *  " + text);bw.newLine();
		bw.write(" *  通过BLingSoft编写工具生成,版权所有,禁止转载");bw.newLine();
		bw.write(" *  时间:2013-03-21");bw.newLine();
		bw.write(" *</pre>");bw.newLine();
		bw.write(" * @author BLingSoft");bw.newLine();
		bw.write(" * @version 1.0");bw.newLine();
		bw.write("**/");
		bw.newLine();
		bw.newLine();
	}
	
	private void outputBaseBean() throws IOException{
		File folder = new File(bean_path);
		if(!folder.exists()){
			folder.mkdir();
		}
		File beanFile = new File(bean_path,"BaseBean.java");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(beanFile)));
		bw.write("package " + bean_package + ";");
		bw.newLine();
		bw.write("import java.io.Serializable;");
		this.outputAuthor(bw,"排序基类");
		bw.newLine();
		bw.write("@SuppressWarnings(\"serial\")");
		bw.newLine();
		bw.write("public class BaseBean implements Serializable{");
		bw.newLine();
		bw.write("\t/*
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值