zoukankan      html  css  js  c++  java
  • Mybatis 自动生成代码,数据库postgresql

    最近做了一个项目,使用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

    注:这里的核心是路径,最好都是用绝对路径

  • 相关阅读:
    哈希算法是怎么实现的
    高并发下日志组件的各种实现方式
    算法是如何影响程序编码方式的
    <<.NET B/S 架构实践>> 几种概念区别
    如何扩大系统盘空间
    区别:ASP.NET MVC的Model、DTO、Command
    能递归检查DataAnnotations的验证函数
    NuGet的本地服务器安装与Package的发布(呕吐)
    多模块分布式系统的简单服务访问
    多模块后带来的问题解决方法
  • 原文地址:https://www.cnblogs.com/liqiu/p/3869294.html
Copyright © 2011-2022 走看看