Mybatis Geneartor 初步使用
官方文档:http://www.mybatis.org/generator/index.html
创建package及资源文件夹
![屏幕快照 2018-10-22 下午5.09.32](Mybatis-Geneartor-初步使用/屏幕快照 2018-10-22 下午5.09.32.png)
配置文件
<?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="MyBatis3Simple">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=utf8&useSSL=false"
userId="root"
password="mysql123">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定model-->
<javaModelGenerator targetPackage="com.an.study.model" targetProject="/Users/bxan/IdeaProjects/MybatisGeneator/src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--指定sql mapper-->
<sqlMapGenerator targetPackage="com.an.study.dao" targetProject="/Users/bxan/IdeaProjects/MybatisGeneator/src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--指定java 接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.an.study.dao"
targetProject="/Users/bxan/IdeaProjects/MybatisGeneator/src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--指定要逆向分析哪个表 创建对应的javabean 名字domainObjectName -->
<table tableName="user" domainObjectName="User">
</table>
<table tableName="address" domainObjectName="Address">
</table>
</context>
</generatorConfiguration>
运行Mybatis Generator
MyBatis Generator (MBG) can be run in the following ways:
- From the command prompt with an XML configuration
- As an Ant task with an XML configuration
- As a Maven Plugin
- From another Java program with an XML configuration
- From another Java program with a Java based configuration
- As an Eclipse Feature
Each method is described in detail on the linked pages.
示例:采用Java和XML的方式运行
特别注意:file要写对路径(可以是Mybatis Generator配置文件的相对路径也可以是Mybatis Generator配置文件的绝对路径)
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("/Users/bxan/IdeaProjects/MybatisGeneator/src/main/resources/mbg.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);
运行即可自动生成!