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

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

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

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

安装好后,我是新建了一个工程,专门执行这个功能,以后其他工程也可以使用该工程作为工具来生成entity、dao、xml文件。
工程结构如下:
利用mybatis-generator自动生成entity、dao和mapper.xml文件_第2张图片
这里最主要的部分就是xml这个配置文件,其实所有操作,都是根据generatorConfig.xml这个配置文件来执行的。下面是我在项目中用到的一些配置。
generatorConfig.xml



<generatorConfiguration>

    
    <classPathEntry location="G:\mysql-connector-java-5.1.34.jar" />

    <context id="DB2Tables">

        <commentGenerator>
            
            <property name="suppressDate" value="true" />
            
            <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" />

        
        <sqlMapGenerator targetPackage="com.mapper" targetProject="mybatisGenerator/src" />

        
        <javaClientGenerator targetPackage="com.dao" targetProject="mybatisGenerator/src" type="XMLMAPPER" />

        
        
        
        
        
        <table schema="blog" tableName="blog_account_info" domainObjectName="BlogAccountInfo" 
            enableCountByExample="false" enableDeleteByExample="false" enableUpdateByExample="false"
            enableSelectByExample="false">
            
            <property name="useActualColumnNames" value="false" />
            
            
            
            
        table>
    context>
generatorConfiguration>

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

你可能感兴趣的:(mybatis,java)