mybatis 逆向工程

环境jar包下载:

mysql-connector-java-8.0.13
mybatis-generator-core-1.3.7

配置generatorConfig.xml:

需要注意的是MySQL url的配置,因为都用的最新版本,所以跟有些百度的不同。

  • allowPublicKeyRetrieval=true 防止出现 Public Key Retrieval is not allowed。
    这个东西在maven项目里配置jdbc时可以不加,但在这第一次运行时要加。
  • 如果数据库是MySQL 8.x 自定义的表与系统表有同名时,会自动生产两张表的对应代码,而且会有很多冲突和错误,此时设置table的schema也没有效果

jdbcConnection里添加 <property name="nullCatalogMeansCurrent" value="true" />

<?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>
 
  <!--MySQL连接驱动-->
  <classPathEntry location="mysql-connector-java-8.0.13.jar" />
  
  <!--数据库链接URL,用户名、密码 -->
  <context id="MySQL" targetRuntime="MyBatis3">
    <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->  
            <property name="suppressAllComments" value="true"/>  
    </commentGenerator> 
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/shopping?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;allowPublicKeyRetrieval=true&amp;serverTimezone=Asia/Shanghai"
        userId="root"
        password="123456">
         <!--如果数据库是MySQL 8.x  自定义的表与系统表有同名时,会自动生产两张表的对应代码,而且会有很多冲突和错误,此时设置table的schema也没有效果 -->
        <property name="nullCatalogMeansCurrent" value="true" />
    </jdbcConnection>
 
    <!--是否启用java.math.BigDecimal-->
    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 生成模型的包名和位置--> 
    <javaModelGenerator targetPackage="shopping.po" targetProject="src">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- 生成映射mapper.xml的包名和位置--> 
    <sqlMapGenerator targetPackage="shopping.dao"  targetProject="src">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 生成DAO的包名和位置--> 
    <javaClientGenerator type="XMLMAPPER" targetPackage="shopping.dao"  targetProject="src">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
    <table tableName="items" domainObjectName="Items" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>
    <table tableName="orderdetail" domainObjectName="Orderdetail" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>
 <table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>
 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>
  </context>
</generatorConfiguration>

命令行下生成代码

1、在cmd命令行定位到D:\projects\mybatis-generator-core-1.3.7\lib

2、输入命令java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
正常情况下会提示操作成功。

生成结果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值