mybatis-generator的使用

    Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。

1.准备

首先,我们去下载工具类。https://github.com/mybatis/generator/releases
由于我使用的是mysql数据,所以我使用了mysql的相关jar。
这里写图片描述

2.配置信息
<?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_3.dtd">

<generatorConfiguration>
    <!--数据库驱动jar -->
    <classPathEntry
        location="D:/repositories/mysql/mysql-connector-java/5.1.29/mysql-connector-java-5.1.29.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--去除注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!--数据库连接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3315/rms" userId="test"
            password="test">
        </jdbcConnection>
        <!--默认false Java type resolver will always use java.math.BigDecimal if 
            the database column is of type DECIMAL or NUMERIC. -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--生成实体类 指定包名 以及生成的地址 (可以自定义地址,但是路径不存在不会自动创建 使用Maven生成在target目录下,会自动创建) -->
        <javaModelGenerator targetPackage="com.jzq.rms.domain"
            targetProject="src">
            <property name="enableSubPackages" value="false" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--生成SQLMAP文件 -->
        <sqlMapGenerator targetPackage="mapping"
            targetProject="src">
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!--生成Dao文件 可以配置 type="XMLMAPPER"生成xml的dao实现 context id="DB2Tables" 修改targetRuntime="MyBatis3" -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.jzq.rms.dao" targetProject="src">
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!--对应数据库表 mysql可以加入主键自增 字段命名 忽略某字段等 -->

        <table tableName="contact_sensitive_record" domainObjectName="ContactSensitiveRecord"
               enableCountByExample="false" enableDeleteByExample="fasle"
               enableSelectByExample="false" enableUpdateByExample="false"
               selectByExampleQueryId="fasle" />

    </context>
</generatorConfiguration>

上面配置文件中的:

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

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

3.生成

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

生成语句:

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

这样,我们就能成功地生成了service,mapper,dao以及domain类。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值