自动构建Mybatis DAO MODEL XML文件

参考文章 :https://www.cnblogs.com/smileberry/p/4145872.html

需要用到的jar 包Mybatis-Generator 

下载地址:https://github.com/mybatis/generator/releases

测试库用Mysql,这里需要在准备一个连接mysql数据库的驱动jar包

下载地址: https://mvnrepository.com/artifact/mysql/mysql-connector-java

文件截图


下面为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>
      <!--数据库驱动-->
      <classPathEntry    location="mysql-connector-java-5.1.41.jar"/>
      <context id="DB2Tables"    targetRuntime="MyBatis3">
          <commentGenerator>
             <property name="suppressDate" value="true"/>
             <property name="suppressAllComments" value="true"/>
         </commentGenerator>
         <!--数据库链接地址账号密码-->
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/bills" userId="root" password="123456">
         </jdbcConnection>
         <javaTypeResolver>
             <property name="forceBigDecimals" value="false"/>
         </javaTypeResolver>
         <!--生成Model类存放位置-->
         <javaModelGenerator targetPackage="lcw.model" targetProject="src">
             <property name="enableSubPackages" value="true"/>
             <property name="trimStrings" value="true"/>
         </javaModelGenerator>
         <!--生成映射文件存放位置-->
         <sqlMapGenerator targetPackage="lcw.mapping" targetProject="src">
             <property name="enableSubPackages" value="true"/>
         </sqlMapGenerator>
         <!--生成Dao类存放位置-->
         <javaClientGenerator type="XMLMAPPER" targetPackage="lcw.dao" targetProject="src">
             <property name="enableSubPackages" value="true"/>
         </javaClientGenerator>
         <!--生成对应表及类名-->
         <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
     </context>

 </generatorConfiguration>

注意:看注释内容,修改数据库用户名,密码,文件生成的目录等

 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false)。

当需要生成多个table时只需在后面添加<table>......</table>

eg:



生成文件语句命令 

java -jar mybatis-generator-core-1.3.6.jar -configfile generatorConfig.xml -overwrite 

使用方法

在该目录按住Shift键,右键鼠标选择"在此处打开命令窗口",复制粘贴生成语句的文件代码即可。

操作图如下






最后说一下中途遇到的问题

按Enter执行命令时出现error 

 XML Parser Error on line 13: The string "--" is not permitted within comments.

出现的原因最新版的mybatis jar包1.3.6 是不支持jdk1.6的,换成1.8后问题解决

排查过程 切换成jdk1.6,将mybatis jar包改为1.3.2,结果正常


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zsigner

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值