天天看點

eclipse 用mybatis-generator自動生成代碼(附帶源碼)

由于沒有用插件是以所需jar包需要檔案自己下載下傳

推薦下載下傳位址:https://mvnrepository.com/

需要用到的jar包

eclipse 用mybatis-generator自動生成代碼(附帶源碼)

準備

  1. 建立JavaporPorject  命名為generator
  2. 選中generator項目建立lib檔案夾  将所需檔案拷貝進lib檔案夾内
  3. 對每個jar包進行架包 (右鍵jar包build path > add to build path)
  4. 選中項目建立xml檔案 命名為mybatis-generator.xml
  5. src中建立Java類 命名為Generatormap

代碼方面

Generatormap.java

public class Generatormap {
	public void generator()throws Exception{
		List<String> warnings=new ArrayList<String>();
		boolean overwrite = true;
//		導入配置表e.g-mybatis-generator.xml
		File configFile=new File("mybatis-generator.xml");
//		解析
		ConfigurationParser cp=new ConfigurationParser(warnings);
		Configuration config=cp.parseConfiguration(configFile);
//		是否覆寫
		DefaultShellCallback dsc=new DefaultShellCallback(overwrite);
		MyBatisGenerator mg=new MyBatisGenerator(config, dsc, warnings);
		System.out.println("完成");
		mg.generate(null);
	}
	public static void main(String[] args) throws Exception  {
		try {
			Generatormap generatormap = new Generatormap();
			generatormap.generator();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("nonono");
		}
		
	}
}
           

配置mybatis-generator.xml

<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--資料庫連接配接的資訊:驅動類、連接配接位址、使用者名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/taowu?useUnicode=true&amp;characterEncoding=utf8" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- 預設false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer true,把JDBC DECIMAL 和 
			NUMERIC 類型解析為java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO類的位置 -->
		<javaModelGenerator targetPackage="com.taowu.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否讓schema作為包的字尾 -->
			<property name="enableSubPackages" value="false" />
			<!-- 從資料庫傳回的值被清理前後的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
        <!-- targetPackage:mapper映射檔案生成的位置 -->
		<sqlMapGenerator targetPackage="com.taowu.mapper" 
			targetProject=".\src">
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口的生成位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.taowu.mapper" 
			targetProject=".\src">
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		
		<!-- 指定表-->
		<table schema="" tableName="tb_content"></table>
		<table schema="" tableName="tb_content_category"></table>
		<table schema="" tableName="tb_item"></table>
		<table schema="" tableName="tb_item_cat"></table>
		<table schema="" tableName="tb_item_desc"></table>
		<table schema="" tableName="tb_item_param"></table>
		<table schema="" tableName="tb_item_param_item"></table>
		<table schema="" tableName="tb_order"></table>
		<table schema="" tableName="tb_order_item"></table>
		<table schema="" tableName="tb_order_shipping"></table>
		<table schema="" tableName="tb_user"></table>
		
	</context>
</generatorConfiguration>
           

在mybatis-generator.xml中 本人連接配接的是MySQL,逆向導出代碼

如果用其他可修改資料連接配接,并導入相應的jar包

源碼位址:https://github.com/BossWhen/generator.git