自动生成mybtis相关代码

<!--  自动生成代码
配置根据数据表自动生成dao,mapper,entity层
1.配置依赖的jar包 mybatis-generator-core
2.配置文件
3.配置插件,在pom中
4.执行插件生成代码,在右边栏maven中
-->
1.在pom文件中配置需要的jar包
<!--mybatis自动生成代码的jar-->
    <dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-core</artifactId>
      <version>1.3.6</version>
    </dependency>
2、配置genertorconfig.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">

<!--  自动生成代码
配置根据数据表自动生成dao,mapper,entity层
    1.配置依赖的jar包  mybatis-generator-core
    2.配置文件
    3.配置插件,在pom中
    4.执行插件生成代码,在右边栏maven中
-->
<generatorConfiguration>
    <!-- 数据库驱动包物理位置 -->
    <classPathEntry location="D:\generator\mysql-connector-java-5.1.37-bin.jar" />
    <context id="oracle" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/springtest"
                        userId="root"
                        password="root">
        </jdbcConnection>

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
           NUMERIC 类型解析为java.math.BigDecimal 。

        BigDecimal类用于高精度计算。一般的float型和Double型数据只可以用来做科学计算或者是工程计算,由于在商业计算中,要求的数字精度比较高,所以要用到java.math.BigDecimal类,它支持任何精度的定点数,可以用它来精确计算货币值。
-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成实体类-->
        <javaModelGenerator targetPackage="com.hsxx.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--生成映射文件-->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--生成mapper文件对应的dao-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.hsxx.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--配置表信息-->
        <table tableName="department" domainObjectName="Department" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>

3、在pom中配置插件,插件放在<pluginManagement>下边

<plugins>
      <!-- mybatis generator 自动生成代码插件 -->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.6</version>
        <configuration>
          <!--指定配置文件的名称。默认值:${basedir}/src/main/resources/generatorConfig.xml-->
          <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
          <!--新生成的文件会覆盖原有的文件。-->
          <overwrite>true</overwrite>
          <!--如果指定该参数,执行过程会输出到控制台-->
          <verbose>true</verbose>
        </configuration>
      </plugin>
    </plugins>

4、以idea为例:

          此时界面右侧Maven里会出现mybais-generator插件,点击插件就能生成dao,mapper,entity层

猜你喜欢

转载自www.cnblogs.com/fbbg/p/12913770.html