mybatis generator 使用方法

 

环境: ubuntu   eclipse maven

一. 简介

mybatis-geneator是一款mybatis自动代码生成工具,可以通过配置,快速生成mapper和xml文件以及pojo

二.配置

pom.xml配置

 <dependencies>
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
  </dependencies>

<build>
        <finalName>shop</finalName>
        <plugins>
        <plugin>
          <groupId>org.mybatis.generator</groupId>
          <artifactId>mybatis-generator-maven-plugin</artifactId>
          <version>1.3.7</version>
           <configuration>
    <verbose>true</verbose>
    <overwrite>true</overwrite>
    </configuration>
        </plugin>
</plugin>
</plugins>
</build>

在main的resource目录下创建generatorConfig.xml文件

mybatis generator 使用方法

generatorConfig.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">

<!-- classPath:数据库的JDBC驱动-->  
<generatorConfiguration>
    <classPathEntry
            location="/home/jiang/.m2/repository/mysql/mysql-connector-java/8.0.16/mysql-connector-java-8.0.16.jar"/>
    <context id="default" targetRuntime="MyBatis3">
    
        <commentGenerator>
         
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://106.13.46.152:3306/shop" userId="jiang"
                        password="1"/>
 
        <javaModelGenerator targetPackage="com.feilong.shop.entity"
                            targetProject="./src/main/java">
             <!-- TODO enableSubPackages:是否让schema作为包的后缀-->
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格-->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
          <!--map xml生成器 --> 
        <sqlMapGenerator targetPackage="com.feilong.shop.dao.mappers"
                         targetProject="./src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
         <!-- dao生成器-->  
        <javaClientGenerator targetPackage="com.feilong.shop.dao"
                             targetProject="./src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        
 <!-- 数据表与Bean的映射 -->
        <table tableName="shop_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"
               
               >
            <!--<columnRenamingRule searchString="^D_"
                                replaceString=""/>-->
        </table>
          <table tableName="shop_address" domainObjectName="Address"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        
          <table tableName="shop_cart" domainObjectName="Cart"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
          <table tableName="shop_category" domainObjectName="Category"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        
          <table tableName="shop_comment" domainObjectName="Comment"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        
          <table tableName="shop_goods" domainObjectName="Goods"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        <table tableName="shop_order" domainObjectName="Order"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
         <table tableName="shop_orderdetail" domainObjectName="OrderDetail"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
         <table tableName="shop_orderstatus" domainObjectName="OrderStatus"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        <table tableName="visit" domainObjectName="Visit"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false" enableInsert="true"            
               >
        </table>
        
    </context>
</generatorConfiguration>

 

三. 生成文件

linux控制台在项目pom.xml配置文件的同级目录下输入

mvn mybatis-generator:generate

 

上一篇:基于SpringBoot的sql项目环境配置及连接数据库


下一篇:SpringBoot环境中使用MyBatis代码生成工具