利用mybatis-generator自动生成entity、dao和mapper.xml文件

利用mybatis-generator自动生成entity、dao和mapper.xml文件

最近在业余时间自己闲着没事,开发开发项目,想简单的搭个javaweb项目的框架。所以把搭建过程中遇到的问题和所用的技术,记录下来。一来,是对自己学以致用过程的记录;二来,希望能够对遇到问题的朋友们有所帮助。
好,本文主要是对mybatis-generator的配置进行描述。主要目的还是为了一个记录。
mybatis-generator的用法,根据官方文档,有5种方法,
mybatis-generator官网
这里写图片描述

我采用的是eclipse的插件方式,首先要在eclipse上安装mybatis-generator的相关插件,具体的安装方法,可以直接百度,很简单。

安装好后,我是新建了一个工程,专门执行这个功能,以后其他工程也可以使用该工程作为工具来生成entity、dao、xml文件。
工程结构如下:
这里写图片描述
这里最主要的部分就是xml这个配置文件,其实所有操作,都是根据generatorConfig.xml这个配置文件来执行的。下面是我在项目中用到的一些配置。
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>

    <!-- 指定mysql连接jar包地址 -->
    <classPathEntry location="G:\mysql-connector-java-5.1.34.jar" />

    <context id="DB2Tables">

        <commentGenerator>
            <!-- 是否生成注释代时间戳 -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!-- 数据库连接信息,驱动类,数据库地址,用户名,密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://localhost:3306/blog" userId="root"
            password="123456" />

        <!--  -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 实体的存放工程和包名 -->
        <javaModelGenerator targetPackage="com.entity" targetProject="mybatisGenerator/src" />

        <!-- mapper.xml文件的存放工程和包名 -->
        <sqlMapGenerator targetPackage="com.mapper" targetProject="mybatisGenerator/src" />

        <!-- dao的存放工程和包名 -->
        <javaClientGenerator targetPackage="com.dao" targetProject="mybatisGenerator/src" type="XMLMAPPER" />

        <!-- tableName:用于自动生成代码的数据库表;domainObjectName:对应于数据库表的javaBean类名 -->
        <!-- <table schema="untodo" tableName="T_USER" domainObjectName="User"/> -->
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <!-- <table schema="untodo" tableName="T_USER" domainObjectName="User" 
            enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" 
            enableSelectByExample="false" selectByExampleQueryId="false"/> -->
        <!--生成对应表及类名 -->
        <table schema="blog" tableName="blog_account_info" domainObjectName="BlogAccountInfo" 
            enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
            enableSelectByExample="false">
            <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名 -->
            <property name="useActualColumnNames" value="false" />
            <!-- 忽略列,不生成bean 字段 -->
            <!-- <ignoreColumn column="FRED" /> -->
            <!-- 指定列的java数据类型 -->
            <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
        </table>
    </context>
</generatorConfiguration>

当然,上述配置文件只是我用到了这些配置,还有很多其他的配置没有用到,这里贴上一个在简书上偶然看到的一篇配置文件,算是目前看到过的最全的了,有兴趣的同学可以去看看。
https://www.jianshu.com/p/e09d2370b796

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值