使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)

出处“https://www.cnblogs.com/smileberry/p/4145872.html

1、相关文件

关于Mybatis-Generator的下载可以到这个地址:https://github.com/mybatis/generator/releases

下载后的目录结构如图

 

和Hibernate逆向生成一样,这里也需要一个配置文件:generatorConfig.xml

2.自己去添加一个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.46.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://202.117.16.167:3306/course" userId="testyu" password="testyu123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.spc.model" targetProject="src">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.spc.mapping" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.spc.dao" targetProject="src">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="ClassAll" domainObjectName="ClassAll" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true"/> 
        </table>
    </context>
</generatorConfiguration>

该配置文件中:

<table tableName="message" domainObjectName="Messgae" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

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

tableName和domainObjectName为必选项,分别代表数据库表名和生成的实力类名,其余的可以自定义去选择(一般情况下均为false),配置中的name="useActualColumnNames"表示使用驼峰式命名规则。不需要则可以注释掉

<!--location后填写的是下载后的jar包文件-->
<classPathEntry    location="mysql-connector-java-5.1.46.jar"/>

<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://202.117.16.167:3306/course" userId="testyu" password="testyu123456">

<!--生成Model类存放位置,targetPackage对应上你项目的位置,targetProject则是需要自己建立文件夹与项目结构对应-->
<javaModelGenerator targetPackage="com.spc.model" targetProject="src">
      <property name="enableSubPackages" value="true"/>
      <property name="trimStrings" value="true"/>
</javaModelGenerator>

<!--生成映射文件存放位置,targetPackage同上的道理-->
<sqlMapGenerator targetPackage="com.spc.mapping" targetProject="src">
     <property name="enableSubPackages" value="true"/>
</sqlMapGenerator>

3.生成语句

进入到你自己的下载的目录中,打开windows命令行进入mybatis-generator-core-1.3.7,输入:

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

最后生成对应的文件

4.生成后的文件如图:

5.生成相关的代码如图:

Model

个人整理后的资源链接https://download.csdn.net/download/l8947943/10887129,如有问题,请留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值