mybatis逆向工程

前提


本人對mybatis也不是很熟練,怕忘記了,提前來寫個筆記


準備(1個jar包+1個配置文件+自定義1個生成類)


需要的jar包(百度):

mybatis-generator-core-1.3.2.jar

需要的配置文件內容(generatorconfig.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>
	<!--調用數據庫驅動,最好不要有中文字符,不然會找不到 -->
	<classPathEntry location="d:/cache/mysql-connector-java-5.1.21-bin.jar" />

	<context id="DB2Tables" targetRuntime="MyBatis3">

		<commentGenerator>
			<property name="suppressDate" value="true" />
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		
		<!--數據庫鏈接地址賬號密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://127.0.0.1:3306/bookshop" userId="root"
			password="123456">
		</jdbcConnection>
		
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>
		
		<!--生成實體類entity類存放位置 -->
		<javaModelGenerator targetPackage="com.bookshop.xcl.entity"
			targetProject="src">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		
		<!--生成xml映射文件存放位置 -->
		<sqlMapGenerator targetPackage="com.bookshop.xcl.xml"
			targetProject="src">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		
		<!--生成DaoMapper類存放位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.bookshop.xcl.dao" targetProject="src">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		
		<!--生成對應表及類名,需要記住的一點是逆向工程無法生成關聯關係,只能生成單表操作 -->
		<table tableName="users" domainObjectName="Users"></table>
		<table tableName="addresss" domainObjectName="Addresss"></table>
		<table tableName="buyrecord" domainObjectName="Buyrecord"></table>
		<table tableName="shopping" domainObjectName="Shopping"></table>
		<table tableName="bookmanager" domainObjectName="Teacher"></table>
	</context>
	
</generatorConfiguration>


自定義的生成類(比如我的是generatorconfig.java


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

public class generatorconfig {

	public static void generator() throws Exception {
		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		// 獲取絕對路徑
		String relativelyPath = System.getProperty("user.dir");
		// 讀取配置文件
		File configFile = new File(relativelyPath
				+ "/config/generatorconfig.xml");
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(configFile);
		DefaultShellCallback callback = new DefaultShellCallback(overwrite);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
				callback, warnings);
		myBatisGenerator.generate(null);
	}

	public static void main(String[] args) {
		try {
			generator();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章