微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Mybatis代码生成器

Mybatis代码生成

导入依赖

通过编码方式去运行插件先需要引入mybatis-generator-core依赖

	<dependency>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis.generator.core</artifactId>
      <version>1.3.2</version>
    </dependency>

如果使用Maven插件,那么不需要引入mybatis-generator-core依赖,只需要引入一个Maven插件mybatis-generator-maven-plugin

<plugins>
    <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
            <!-- 输出详细信息 -->
            <verbose>true</verbose>
            <!-- 覆盖生成文件 -->
            <overwrite>true</overwrite>
            <!-- 定义配置文件 -->
            <configurationFile>
            	src/main/resources/generatorConfig.xml
            </configurationFile>
        </configuration>
    </plugin>
</plugins>

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

<generatorConfiguration>

  <properties resource="db.properties"/>

    <!-- 数据库驱动,解析数据库表来生成实体类 -->
  <classpathEntry location="D:/mysql-connector-java-5.1.5.jar" />
	<!-- 一个数据库一个context -->
  <context id="MyTables" targetRuntime="MyBatis3">
      
    <commentGenerator>
        <!-- suppressDate:**阻止**生成的注释包含时间戳 -->
        <property name="suppressDate" value="true"/>
        <!-- suppressAllComments:true **阻止**生成注释 -->
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
      
	<!-- 数据库连接地址账号密码 -->
    <jdbcConnection driverClass="com.MysqL.jdbc.Driver"
        connectionURL="jdbc:MysqL://localhost:3306/health"
        userId="root"
        password="">
    </jdbcConnection>
      
     <javaTypeResolver>
      <!-- 控制是否强制DECIMAL和NUMERIC类型的字段转为java类型的java.math.BigDecimal -->
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
      
    <!-- 生成Model类存放位置 -->
    <javaModelGenerator targetPackage="com.itheima.pojo" 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>
      
    <!-- 生成Dao类存放位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.itheima.dao"  targetProject="src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
      
    <!-- 生成对应表及类名 -->
    <table schema="health" tableName="t_checkitem" domainObjectName="CheckItem" 
           enableCountByExample="false" enableupdateByExample="false" 
           enableDeleteByExample="false" enableSelectByExample="false" 
           selectByExampleQueryId="false" 
           />

    <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>

  </context>
</generatorConfiguration>

执行插件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐