mybatis 逆向工程

使用逆向工程来生成数据库表,单表的Mapper

1、搭建环境

 

2、按实际填写好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="dbfortestTables" targetRuntime="MyBatis3">

  

      <commentGenerator>

         <!-- 是否去除自动生成的注释 true:是 : false:否 -->

         <property name="suppressAllComments" value="true" />

      </commentGenerator>

     

     

      <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->

      <jdbcConnection driverClass="com.mysql.jdbc.Driver"

         connectionURL="jdbc:mysql://localhost:3306/dbfortest" userId="root"

         password="root">

      </jdbcConnection>

     

     

 

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

         NUMERIC 类型解析为java.math.BigDecimal -->

      <javaTypeResolver>

         <property name="forceBigDecimals" value="false" />

      </javaTypeResolver>

 

  

      <!-- targetProject:bean/pojo位置 -->

      <javaModelGenerator targetPackage="com.xzw.bean"

         targetProject=".src">

         <!-- enableSubPackages:是否让schema作为包的后缀 -->

         <property name="enableSubPackages" value="false" />

         <!-- 从数据库返回的值被清理前后的空格 -->

         <property name="trimStrings" value="true" />

      </javaModelGenerator>

     

     

        <!-- targetProject:mapper映射文件生成的位置 -->

      <sqlMapGenerator targetPackage="com.xzw.mapper"

         targetProject=".src">

         <!-- enableSubPackages:是否让schema作为包的后缀 -->

         <property name="enableSubPackages" value="false" />

      </sqlMapGenerator>

     

      <!-- targetPackage:mapper接口生成的位置 -->

      <javaClientGenerator type="XMLMAPPER"

         targetPackage="com.xzw.mapper"

         targetProject=".src">

         <!-- enableSubPackages:是否让schema作为包的后缀 -->

         <property name="enableSubPackages" value="false" />

      </javaClientGenerator>

     

     

      <!-- 指定数据库表 -->

      <table tableName="tb_user" domainObjectName="User"></table>

     

      <table tableName="tb_novel" domainObjectName="Novel">

      <!--useActualColumnNames使用实际列名作为字段,默认false  -->

      <property name="useActualColumnNames" value="true"/>

      </table>

     

      <table tableName="tb_chacter" domainObjectName="Chacter">

      <property name="useActualColumnNames" value="true"/>

      </table>

      <table tableName="clist">

      <property name="useActualColumnNames" value="true"/>

      </table>

     

     

   </context>

</generatorConfiguration>

3、java程序。

 

运行这一段java代码。

 

4.结果

 

5.使用例子

主键查询

 

自定义条件查询

 

条件

 

选择性插入(selective)

 

 生成的mapper.xml

 

全部参数的。

 -------------------------

可以通过读生成的java和xml文件来了解怎么应用生成的mapper。同时,生成的mapper.xml不失为一个写mapper.xml的学习模板。

原文地址:https://www.cnblogs.com/jway1101/p/5773614.html