Java代码生成器

博主分享了自己在公司一个月内的学习心得,并决定开始写博客记录成长。他讲述了如何从零开始学习并修改Freemarker模板,以适应项目的新UI需求,然后受此启发,着手编写自己的Java代码生成器。文章中提供了自定义表字段结构、工具类及生成器类的部分代码,旨在帮助读者理解代码生成器的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来公司一个月了,时间虽然不长,但是我感觉自己还是学到了不少的东西,至少在学校从没有过这样的感觉。并且我也从现在开始写博客了,希望博友能多来浏览,提出批评指正。先来看看我的第一篇博客。

事情起源于修改项目的UI(这个过程就不多说了)。我的指导人让我把项目的生成模版修改一下,适应我们的新样式,可是模版是freemarker写的,我完全都不会哭,没办法,只能自己去摸索了,费了九牛二虎之力终于修改好了。闲来无事,我就想这玩意好像还挺有用的,要不我也自己弄一个,于是就开始了编写我自己的代码生成器了。

好了,“废话”不多说,直接上代码。

自定义表字段结构和表结构

public class MyField {	
	private String field_name;
	private String sql_type;
	private String java_type;
	private String field_comment;
	private boolean is_primary;
}

public class MyTable {		

	//表名
	private String tableName;
	//普通字段集合,一个字段包括了:字段名,SQL类型,Java类型,注释,是否主键标志
	private List<MyField> common_fields;
	//主键字段集合
	private List<MyField> key_fields;
	//字段中是否有日期型的,如果有,那么在生成实体类时,就需要导java.util.Date包了
	public boolean date_flag = false;
	//字段中是否有numeric和decimal类型的,如果有,那么,在生成实体类时就需要导java.math.BigDecimal包了
	public boolean math_flag = false;
	//生成实体类,mapper接口等等时的基名
	public String package_name_base;

}


工具类(里面有一部分是我最开始想纯粹用java代码来生成想要的文件,后来发现这种方法完全是硬编码,所以放弃)

public class DataBaseUtil {
	
	private static String jdbc_url ;
	private static String jdbc_driver ;
	private static String jdbc_user ;
	private static String jdbc_password ;
	private static Integer jdbc_poolsize;	

	private static List<Connection> connectionPool;
	
	
	/**
	 * 静态代码块,用于读取db.properties,初始化和数据库连接相关的信息
	 */
	static{
		Properties properties = new Properties();
		InputStream input = null;
		String path = "db.properties";
		input = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
		try {
			properties.load(input);
			jdbc_driver = properties.getProperty("jdbc_driver");
			jdbc_url = properties.getProperty("jdbc_url");
			jdbc_user = properties.getProperty("jdbc_user");
			jdbc_password = properties.getProperty("jdbc_password");
			jdbc_poolsize = Integer.parseInt(properties.getProperty("jdbc_poolsize"));
			connectionPool = new ArrayList<Connection>(jdbc_poolsize);			
			Class.forName(jdbc_driver);
			int i;
			for(i = 0 ; i < jdbc_poolsize ; i++){
				Connection connection = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_password);
				connectionPool.add(connection);
			}			
		} catch (IOException e) {
			System.out.println("加载属性文件失败,请检查db.properties文件是否在src下面");
			System.exit(-1);
		} catch (ClassNotFoundException e) {
			StringBuffer buffer = new StringBuffer();
			buffer.append("加载数据库驱动出错,请检查:\r\n");
			buffer.append("(1)db.properties中的jdbc_driver是否写错?\r\n");
			buffer.append("(2)数据库驱动JAR包是否已导入?");
			System.out.println(buffer.toString());
			System.exit(-1);
		} catch (SQLException e) {
			StringBuffer buffer = new StringBuffer();
			buffer.append("Oh,My God! 数据库连接失败,请检查:\r\n");
			buffer.append("(1)数据库服务是否已开启?\r\n");
			buffer.append("(2)db.properties中的jdbc_url是否写错?\r\n");
			buffer.append("(3)db.properties中的jdbc_user是否写错?\r\n");
			buffer.append("(4)db.properties中的jdbc_password是否写错?\r\n");
			System.out.println(buffer.toString());
			System.exit(-1);
		} finally{
			try {
				if(input != null)
					input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * @return 获取数据库连接,可能返回null
	 */
	public static Connection getConnection(){
		if(connectionPool != null){
			if(connectionPool.size() > 0){
				return connectionPool.remove(connectionPool.size() - 1);
			}else{
				Connection connection = null;
				try {
					Class.forName(jdbc_driver);
					connection = DriverManager.getConnection(jdbc_url, jdbc_user, jdbc_password);
					return connection;
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
					return connection;
				} catch (SQLException e) {
					e.printStackTrace();
					return connection;
				}
			}
		}else
			return null;
	}
	
	/**
	 * @return 返回某个数据库下所有的表结构
	 * @throws Exception 
	 */
	public static List<MyTable> getAllTables(){
		List<MyTable> tables = null;
		Connection connection = getConnection();		
		if(connection != null){
			PreparedStatement statement = null;
			ResultSet connectionRS = null , queryRS = null;
			tables = new ArrayList<MyTable>();
			try {
				DatabaseMetaData metaData = connection.getMetaData();
				connectionRS = metaData.getTables(null, null, null, new String[]{"TABLE"});
				while(connectionRS.next()){
					String tableName = connectionRS.getString(3);
					String querySQL = "show full columns from " + tableName;
					statement = connection.prepareStatement(querySQL);
					queryRS = statement.executeQuery();
					
					MyTable myTable = new MyTable();
					List<MyField> key_fields = new ArrayList<MyField>();
					List<MyFi
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值