1. 新建java项目
那我的毕设开刀
2.在根目录下 新建文件夹 lib
3.将 jar包放入lib中
4.将lib文件夹 add as library
5. 创建generator.xml文件
从此文件夹中
打开index.html文件
找到
复制如下内容到generator.xml中
6. 改配置文件
注:
targetPackage="com.ybj.newWebsite.dao" 这个是目标包下
targetProject="src\main\java" 这个是目标项目下
二者是可以联通的 ------src\main\java\com.ybj.newWebsite.dao 即 targetProject\targetPackage
<?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/newswebsite?useUnicode=true&characterEncoding=utf8"
userId="root"
password="312ybj">
</jdbcConnection>
<!--java类型转换-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--model生成包名和位置-->
<javaModelGenerator targetPackage="com.ybj.newWebsite.model" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--mybatis的映射 .xml-->
<sqlMapGenerator targetPackage="com.ybj.newWebsite.mapper" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--mybatis mapper接口生成的dao包路径-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.ybj.newWebsite.dao" targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--配置生成表的模型-->
<table tableName="user" domainObjectName="user" ></table>
<table tableName="article" domainObjectName="article" ></table>
<table tableName="comment" domainObjectName="comment" ></table>
</context>
</generatorConfiguration>
7. 写个main方法, 进行逆向工程
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;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class main {
public static class Generator {
public static void main(String[] args) throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("src/generator.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);
}
}
}