mybatis反转生成entity

本文介绍如何通过MyBatis Generator插件自动生成数据库映射文件、实体类及DAO层接口,包括所需依赖配置、生成配置文件设置及Maven命令执行流程。

1:引入相关pom

<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.44</version>
   <scope>runtime</scope>
  </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:在project下resources下创建

\src\main\resources\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>

  <!--数据库驱动jar包的真实路径 -->
  <classPathEntry
    location="D:\repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar"/>

  <context id="context" targetRuntime="MyBatis3">
    <!-- 是否去除自动生成的注释 true:是 : false:-->
    <commentGenerator>
      <property name="suppressAllComments" value="false"/>
      <property name="suppressDate" value="true"/>
    </commentGenerator>

    <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
      connectionURL="jdbc:mysql://localhost:3306/cms?characterEncoding=UTF8"
      userId="root"
      password="123456"/>
    <!-- 默认false,把JDBC DECIMAL  NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
        NUMERIC 类型解析为java.math.BigDecimal -->
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>

    <!--指定包名生成实体类 以及生成的地址 (可以自定义地址,如果路径不存在会自动创建) -->
    <javaModelGenerator targetPackage="com.saic.travel.bean" targetProject=".\src\main\java">
      <!-- enableSubPackages:是否让schema作为包的后缀 -->
      <property name="enableSubPackages" value="false"/>
      <!-- 从数据库返回的值被清理前后的空格 -->
      <property name="trimStrings" value="true"/>
    </javaModelGenerator>

    <!-- !!!! Mapper XML Configurations !!!! -->
    <sqlMapGenerator targetPackage="com.saic.travel.mapper" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="false"/>
    </sqlMapGenerator>

    <!-- !!!! Mapper Interface Configurations !!!! -->
    <javaClientGenerator targetPackage="com.saic.travel.dao" targetProject=".\src\main\java"
      type="XMLMAPPER">
      <property name="enableSubPackages" value="false"/>
    </javaClientGenerator>

    <!-- 指定数据库表 -->
    <table tableName="tb_cms_driver_honor" domainObjectName="DriverHonor"
      enableCountByExample="false" enableUpdateByExample="false"
      enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <!--<table schema="db_wechat" tableName="t_commandcontent"
           enableCountByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false"/>
    <table schema="db_wechat" tableName="t_automessage"
           enableCountByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false"/>
    <table schema="db_wechat" tableName="t_command"
           enableCountByExample="false" enableDeleteByExample="false"
           enableSelectByExample="false" enableUpdateByExample="false"/>-->
  </context>
</generatorConfiguration>
3:配置maven运行:
mybatis-generator:generate -e


4:运行maven即可.

这里是可以运行生成, 具体需要复杂功能的部分, 后续再添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值