基于maven實現(xiàn)逆向工程
1.新建maven項目阅羹,選擇:
注意:可能生出項目在index.jsp里會報錯继效,這是因為項目沒有導(dǎo)入Tomcat服務(wù)器早像,
2.在pom.xml里引入逆向工程依賴的jar包
<dependency>
????<groupId>org.mybatis.generator</groupId>
????<artifactId>mybatis-generator-core</artifactId>
????<version>1.3.5</version>
</dependency>
<dependency>
?<groupId>org.mybatis</groupId>?
?<artifactId>mybatis</artifactId>?
?<version>3.4.2</version>?
?</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
3.建包:
4.新建bgm.xml:
內(nèi)容如何編寫可以參考
1:http://www.mybatis.org/generator/configreference/xmlconfig.html
'''
<?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>
? <context id="DB2Tables" targetRuntime="MyBatis3">
? ? <jdbcConnection
? ? driverClass="com.mysql.jdbc.Driver"
? ? ? ? connectionURL="jdbc:mysql://localhost:3306/crud?characterEncoding=utf-8"
? ? ? ? userId="root"
? ? ? ? password="q6687015">
? ? </jdbcConnection>
? ? <javaTypeResolver >
? ? ? <property name="forceBigDecimals" value="false" />
? ? </javaTypeResolver>
<!-- 指定java bean生成的位置 -->
? ? <javaModelGenerator
? ? targetPackage="com.qjp.crud.bean"
? ? targetProject=".\src\main\java">
? ? ? <property name="enableSubPackages" value="true" />
? ? ? <property name="trimStrings" value="true" />
? ? </javaModelGenerator>
<!-- 指定映射文件生成的位置 -->
? ? <sqlMapGenerator
? ? targetPackage="com.qjp.crud.mapper"?
? ? targetProject=".\src\main\java">
? ? ? <property name="enableSubPackages" value="true" />
? ? </sqlMapGenerator>
<!-- 指定dao接口的路徑 -->
? ? <javaClientGenerator type="XMLMAPPER"
? ? targetPackage="com.qjp.crud.mapper"?
? ? targetProject=".\src\main\java">
? ? ? <property name="enableSubPackages" value="true" />
? ? </javaClientGenerator>
<!-- 指定每個表的生成策略 -->
? ? <table? tableName="tbl_dept" domainObjectName="dept" ></table>
? ? <table? tableName="tbl_emp" domainObjectName="emp" ></table>
? </context>
</generatorConfiguration>
'''
這里主要是編寫pojo桨踪,mapper映射文件,dao的接口對應(yīng)生成的位置掀亩,以及數(shù)據(jù)庫表的生成策略舔哪。
5.基于java程序驅(qū)動運行MyBatis Generator:
新建一個java程序編寫驅(qū)動程序
'''
package com.qjp.crud.test;
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.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 Mybatisgenerator {
public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
// TODO Auto-generated method stub
List<String> warnings = new ArrayList<String>();
? boolean overwrite = true;
? File configFile = new File("bgm.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);
}
}
'''
編寫參考:http://www.mybatis.org/generator/running/running.html