逆向工程代码的生成

本文探讨了逆向工程中代码生成的过程,重点关注了`test.java`和`MyXm.xml`两个关键文件在这一过程中的作用。通过对这两个文件的分析,揭示了如何从已有的二进制代码或XML配置还原出源代码,从而理解并重构软件系统。

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

1.结构

在这里插入图片描述

test.java

package com.generation;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

public class test {
		/**
		 * @param args
		 * @throws XMLParserException 
		 * @throws IOException 
		 * @throws InvalidConfigurationException 
		 * @throws InterruptedException 
		 * @throws SQLException 
		 */
		public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
			//用来放置生成过程中产生的各种警告信息
			List<String> warnings=new ArrayList<String>();
			//指定是否覆盖原有文件
			boolean overwrite =true;
			//加载配置文件
			File configfile=new File("MyXm.xml");
			//解析生成配置文件对象
			ConfigurationParser cp=new ConfigurationParser(warnings);
			Configuration config=cp.parseConfiguration(configfile);
			//处理重复文件
			ShellCallback callback=new DefaultShellCallback(overwrite);
			//逆向工程对象
			MyBatisGenerator mygenerator=new MyBatisGenerator(config, callback, warnings);
			//执行逆向工程生成操作
			mygenerator.generate(null);
		}
	}

MyXm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>

	<!-- 
		targetRuntime="MyBatis3Simple":生成简单版的CRUD
		MyBatis3:豪华版
	
	 -->
  <context id="DB2Tables" targetRuntime="MyBatis3">
  <!-- 是否去注释  -->
    <commentGenerator>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    
  	<!-- jdbcConnection:指定如何连接到目标数据库 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/sims"
        userId="root"
        password="root">
    </jdbcConnection>
    
	<!--  -->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>

	<!-- javaModelGenerator:指定javaBean的生成策略 
	targetPackage="test.model":目标包名
	targetProject="\MBGTestProject\src":目标工程
	-->
    <javaModelGenerator targetPackage="com.entity"
    		targetProject=".\src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>

	<!-- sqlMapGenerator:sql映射生成策略: -->
    <sqlMapGenerator targetPackage="com.dao" targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>

	<!-- javaClientGenerator:指定mapper接口所在的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.dao"
    	targetProject=".\src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>

	<!-- 指定要逆向分析哪些表:根据表要创建javaBean -->
    <table tableName="s_admin" domainObjectName="Admin"></table>
	<table tableName="s_student" domainObjectName="Student"></table>
	<table tableName="s_course" domainObjectName="Course"></table>
	<table tableName="s_grades" domainObjectName="Grades"></table>
	<table tableName="s_score" domainObjectName="Score"></table>
  </context>
</generatorConfiguration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值