使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(数据库mysqll5.7.21和mybatis-generator-core-1.3.7)

1、准备文件

1)下载Mybatis-Generator核心包(https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core/1.3.7

2)mysql数据库的驱动jar包(https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.47

3)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="E:/code/repository/ruidatielu/mysql/mysql-connector-java/5.1.47/mysql-connector-java-5.1.47.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:3306/ruidatielu?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.ruidatielu.entity" targetProject="Gen">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="mapper" targetProject="Gen">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ruidatielu.dao" targetProject="Gen">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

注意的点:1)数据库驱动那里的location换成自己驱动包的位置,注意是/;

                   2) 数据库连接地址那里的connectionURL="jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&amp;characterEncoding=UTF-8&amp;注意是&amp;

                 3)下面的生成资源位置的targetPackage="com.ruidatielu.entity" targetProject="Gen"。注意的点是targetProject后面的路径一定要提前手动建好。targetProject后面的值可以是自己创建的任何名称,targetPackage后面的值关系到项目的包结构。

                 4)生成对应表及类名 <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

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

3.生成文件的命令语句

管理员权限打开cmd窗口,进入到 mybatis-generator-core-1.3.7.jar所在的文件夹,运行下面的生成语句。

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

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值