Mybatis 自动生成代码

最近做了一个项目,使用Mybatis自动生成代码,下面做一下总结:

一、提前准备:

1、工具类:mybatis-generator-core-1.3.2.jar

2、postgresql驱动:postgresql-9.2-1003-jdbc4.jar

3、xml文件

这些我都上传到了附件上,下载链接:Download

二、XML详解

咱们的核心配置文件:mybatisGeneratorConfig.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="/Users/liqiu/git/study/web/soft/postgresql-9.2-1003-jdbc4.jar" />
        <context id="tts2">
                <jdbcConnection driverClass="org.postgresql.Driver"
                        connectionURL="jdbc:postgresql://l-***.com:5433/crm"
                        userId="menpiao_dev" password="d**904-8dce-**d0-bb1b-79***cc">
                </jdbcConnection>

        <javaModelGenerator targetPackage="com.qunar.study.entity" targetProject="/Users/liqiu/git/study/web/src/main/java">
                        <property name="enableSubPackages" value="false" />
                        <property name="trimStrings" value="true" />
                </javaModelGenerator>

        <sqlMapGenerator targetPackage="mybatis" targetProject="/Users/liqiu/git/study/web/src/main/resources/">
                </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.qunar.study.mapper" targetProject="/Users/liqiu/git/study/web/src/main/java">
                        <property name="enableSubPackages" value="false" />
                </javaClientGenerator>
                 
         <table schema="public" tableName="users" domainObjectName="Users"></table>
        <table schema="public" tableName="region_manager" domainObjectName="RegionManager"></table>
        <table schema="public" tableName="region" domainObjectName="Region"></table>
        <table schema="public" tableName="merchant" domainObjectName="Merchant"></table>
        <table schema="public" tableName="state_machine" domainObjectName="StateMachine"></table>
        <table schema="public" tableName="work_log" domainObjectName="WorkLog"></table>
        <table schema="public" tableName="contract" domainObjectName="Contract"></table>
        <table schema="public" tableName="notice" domainObjectName="Notice"></table>
        <table schema="public" tableName="contact_person_info" domainObjectName="ContactPersonInfo"></table>
        <table schema="public" tableName="express" domainObjectName="Express"></table>
        <table schema="public" tableName="comment" domainObjectName="Comment"></table>
        <!--    
        <table schema="public" tableName="operation" domainObjectName="Operation" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true" ></table>
        -->
        </context>
</generatorConfiguration>
复制代码

三、执行代码:

java -jar /Users/liqiu/git/study/web/soft/mybatis-generator-core-1.3.2/lib/mybatis-generator-core-1.3.2.jar -configfile /Users/liqiu/git/study/web/soft/mybatisGeneratorConfig.xml -overwrite

这时就可以看见生成的代码了

复制代码
├── src
│   ├── main
│   │   ├── java
│   │   │   ├── com
│   │   │   │   └── qunar
│   │   │   │       └── study
│   │   │   │           ├── entity
│   │   │   │           │   ├── Comment.java
│   │   │   │           │   ├── CommentExample.java
│   │   │   │           │   ├── ContactPersonInfo.java
│   │   │   │           │   ├── ContactPersonInfoExample.java
│   │   │   │           │   ├── Contract.java
│   │   │   │           │   ├── ContractExample.java
│   │   │   │           │   ├── Express.java
│   │   │   │           │   ├── ExpressExample.java
│   │   │   │           │   ├── Merchant.java
│   │   │   │           │   ├── MerchantExample.java
│   │   │   │           │   ├── Notice.java
│   │   │   │           │   ├── NoticeExample.java
│   │   │   │           │   ├── Operation.java
│   │   │   │           │   ├── OperationExample.java
│   │   │   │           │   ├── Region.java
│   │   │   │           │   ├── RegionExample.java
│   │   │   │           │   ├── RegionManager.java
│   │   │   │           │   ├── RegionManagerExample.java
│   │   │   │           │   ├── StateMachine.java
│   │   │   │           │   ├── StateMachineExample.java
│   │   │   │           │   ├── Users.java
│   │   │   │           │   ├── UsersExample.java
│   │   │   │           │   ├── WorkLog.java
│   │   │   │           │   └── WorkLogExample.java
│   │   │   │           └── mapper
│   │   │   │               ├── CommentMapper.java
│   │   │   │               ├── ContactPersonInfoMapper.java
│   │   │   │               ├── ContractMapper.java
│   │   │   │               ├── ExpressMapper.java
│   │   │   │               ├── MerchantMapper.java
│   │   │   │               ├── NoticeMapper.java
│   │   │   │               ├── OperationMapper.java
│   │   │   │               ├── RegionManagerMapper.java
│   │   │   │               ├── RegionMapper.java
│   │   │   │               ├── StateMachineMapper.java
│   │   │   │               ├── UsersMapper.java
│   │   │   │               └── WorkLogMapper.java
│   │   │   └── test
│   │   │       └── qunar
│   │   │           └── com
│   │   │               └── web
│   │   │                   └── App.java
│   │   └── resources
│   │       └── mybatis
│   │           ├── CommentMapper.xml
│   │           ├── ContactPersonInfoMapper.xml
│   │           ├── ContractMapper.xml
│   │           ├── ExpressMapper.xml
│   │           ├── MerchantMapper.xml
│   │           ├── NoticeMapper.xml
│   │           ├── OperationMapper.xml
│   │           ├── RegionManagerMapper.xml
│   │           ├── RegionMapper.xml
│   │           ├── StateMachineMapper.xml
│   │           ├── UsersMapper.xml
│   │           └── WorkLogMapper.xml
复制代码
原文地址:https://www.cnblogs.com/duanxingxing/p/4890417.html