美文网首页
秒杀第三节:mybatis自动生成器使用方式

秒杀第三节:mybatis自动生成器使用方式

作者: 小石读史 | 来源:发表于2020-07-10 07:39 被阅读0次

在resources目录新建mapping目录,用来保存mapper文件,新建dataobject用来存放po类,新建dao目录存放dao
为了使用mybatis自动生成器,我们需要新建文件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>

    <!--<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />-->


    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--数据库连接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://xxxx/miaosha"
                        userId="xxxx"
                        password="xxxx">
        </jdbcConnection>


        <!--<javaTypeResolver >-->
            <!--<property name="forceBigDecimals" value="false" />-->
        <!--</javaTypeResolver>-->

        <!--生成Model/DataObject类存放的位置-->
        <javaModelGenerator targetPackage="com.miaoshaproject.dataobject" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件存放的位置-->
        <sqlMapGenerator targetPackage="mapping"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--生成Dao类存放的位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.miaoshaproject.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>


        <!--生成对应表及类名-->
        <!--<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"-->
        <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
        <!--selectByExampleQueryId="false"></table>-->
        <!--<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"-->
               <!--enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"-->
               <!--selectByExampleQueryId="false"></table>-->
    </context>
</generatorConfiguration>

注意:需要根据自己的项目路径修改配置数据库连接地址账号密码、targetPackage、targetProject。
在pom配置文件中plugins的下新增如下配置

<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.41</version>
            </dependency>
          </dependencies>
          <executions>
            <execution>
              <id>mybatis generator</id>
              <phase>package</phase>
              <goals>
                <goal>generate</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <!--允许移动生成的文件-->
            <verbose>true</verbose>
            <!--允许自动覆盖文件-->
            <overwrite>false</overwrite>
            <configurationFile>
              src/main/resources/mybatis-generator.xml
            </configurationFile>
          </configuration>

        </plugin>

执行自动生成代码工具类,操作如下:

image.png
选择maven点击+号
name里写入 mybatis-generator
command line里记入 mybatis-generator:generate -e
image.png
然后在run-run-选择mybatis-generator
image.png

下面测试使用上面生成的代码。首先修改app.java上的注解


image.png

相关文章

网友评论

      本文标题:秒杀第三节:mybatis自动生成器使用方式

      本文链接:https://www.haomeiwen.com/subject/rmfzqktx.html