zoukankan      html  css  js  c++  java
  • SpringBoot中使用mybatis-generator自动生产

    步骤:

    1.在pom.xml中添加插件配置

    <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.5</version>
                    <dependencies>
                        <dependency>
                            <groupId> mysql</groupId>
                            <artifactId> mysql-connector-java</artifactId>
                            <version> 5.1.39</version>
                        </dependency>
                        <dependency>
                            <groupId>org.mybatis.generator</groupId>
                            <artifactId>mybatis-generator-core</artifactId>
                            <version>1.3.5</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>Generate MyBatis Artifacts</id>
                            <phase>package</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!--允许移动生成的文件 -->
                        <verbose>true</verbose>
                        <!-- 是否覆盖 -->
                        <overwrite>true</overwrite>
                        <!-- 自动生成的配置 -->
                        <configurationFile>
                            src/main/resources/mybatis-generator.xml</configurationFile>
                    </configuration>
                </plugin>

    2.在resources文件夹中添加mybatis-generator.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>
        <context id="DB2Tables" targetRuntime="MyBatis3">
            <commentGenerator>
                <property name="suppressDate" value="true"/>
                <property name="suppressAllComments" value="true"/>
            </commentGenerator>
            <!--数据库链接地址账号密码-->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/longhubang" userId="root" password="hongda$123456">
            </jdbcConnection>
            <javaTypeResolver>
                <property name="forceBigDecimals" value="false"/>
            </javaTypeResolver>
            <!--生成Model类存放位置-->
            <javaModelGenerator targetPackage="com.example.scheduleService.model" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
                <property name="trimStrings" value="true"/>
            </javaModelGenerator>
            <!--生成映射文件存放位置-->
            <sqlMapGenerator targetPackage="com.example.scheduleService.mapper" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </sqlMapGenerator>
            <!--生成Dao类存放位置-->
            <!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
                    type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
                    type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
                    type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口
            -->
            <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.example.scheduleService.dao" targetProject="src/main/java">
                <property name="enableSubPackages" value="true"/>
            </javaClientGenerator>
            <!--生成对应表及类名-->
            <!--<table tableName="stocktradeinfo" domainObjectName="StockTradeInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
            <table tableName="theme" domainObjectName="Theme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
            <table tableName="stockTheme" domainObjectName="StockTheme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        </context>
    </generatorConfiguration>

    上面的是不带xml的配置,

    生成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">
      <!-- 第一种mybatis逆向生成xml配置 -->
    <generatorConfiguration>
    
    <context id="sqlserverTables" targetRuntime="MyBatis3">
      <!-- 生成的pojo,将implements Serializable-->
      <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
      <commentGenerator>
        <!-- 是否去除自动生成的注释 true:是 : false:否 -->
        <property name="suppressAllComments" value="true" />
      </commentGenerator>
    
      <!-- 数据库链接URL、用户名、密码 -->
      <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://18.16.200.42:3306/personnel-dev"
        userId="root"
        password="shitou$root">
      </jdbcConnection>
    
      <!--
      默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
          true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
      -->
      <javaTypeResolver>
        <property name="forceBigDecimals" value="false" />
      </javaTypeResolver>
    
      <!--
      生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
      也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下
      -->
      <!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->
      <javaModelGenerator targetPackage="com.jsy.order.mybatis.entity" targetProject="./src/main/java">
        <property name="enableSubPackages" value="true"/>
        <!-- 从数据库返回的值被清理前后的空格  -->
        <property name="trimStrings" value="true" />
      </javaModelGenerator>
    
      <!--对应的mapper.xml文件  -->
      <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
        <property name="enableSubPackages" value="true"/>
      </sqlMapGenerator>
    
      <!-- 对应的Mapper接口类文件 -->
      <javaClientGenerator type="XMLMAPPER" targetPackage="com.jsy.order.mybatis.dao" targetProject="./src/main/java">
        <property name="enableSubPackages" value="true"/>
      </javaClientGenerator>
    
    
      <!--生成对应表及类名-->
      <!--<table tableName="stocktradeinfo" domainObjectName="StockTradeInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
      <table tableName="tb_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    
    </context>
    </generatorConfiguration>

    3.根据配置创建对应的model,mapper,dao文件夹

    4.使用maven中的mybatis-generator:generate根据数据库里面表生产相关的类,Mapper

    注意:

    这里面自动生产的Mapper

    1.没有@Mapper注解

    2.在insert的时候会带上主键ID

    如何给生成字段为驼峰值或者跟表字段名称一样

      <table tableName="configInfo" domainObjectName="ConfigInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
                <property name="useActualColumnNames" value="true"/>
            </table>

    添加属性useActualColumnNames为true,那么生成的对象字段就跟表一样

    命令生成代码:

    mvn mybatis-generator:generate

    http://www.cnblogs.com/hyyq/p/7087620.html

    http://blog.csdn.net/yezhuanxu/article/details/53483130

    http://blog.csdn.net/gzg1001/article/details/51935948

  • 相关阅读:
    POJ 2923 Relocation ★(状态压缩+01背包)
    POJ 1062 昂贵的聘礼 (带限制的最短路)
    HDU 4355 Party All the Time (三分求凸函数极值)
    POJ 1860 Currency Exchange (BellmanFord)
    POJ 2923 Relocation ★(状态压缩+01背包)
    【HNOI2011】数学作业(BZOJ 2326)
    POJ 1062 昂贵的聘礼 (带限制的最短路)
    作为当代大学生,面对着信息增长加快,老化周期变短,你应该如何做?
    作为当代大学生,面对着信息增长加快,老化周期变短,你应该如何做?
    信息分析与预测考前模拟试题
  • 原文地址:https://www.cnblogs.com/hongdada/p/7583625.html
Copyright © 2011-2022 走看看