您的位置:首页 > 其它

generator自动生成mybatis实体类和配制文件

2015-08-13 11:05 549 查看
在学习MyBatis的过程中,发现mybatis确实是非常好用的的框架,但是,手写映射文件很容易出错,所以可利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件。这样可以省去很多的功夫,将生成的代码copy到项目工程中即可。当然了,eclipse上也有类似的插件,不过我选择generator是因为其方便,只需要下载一个jar包就好,下面就来说一下构建过程吧:

我的目录结构如下:




首先:我们需要下载generator的jar包下载地址:http://pan.baidu.com/s/1gdm4rfX

然后,需要一个数据库驱动jar包我这里使用的是mysql,给出下载地址:

http://pan.baidu.com/s/1gdIs00b

接下来就是创建配置文件了我这里是命名为generator.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="D:\software\lib\mysql-connector-java-5.1.21.jar" /> -->
    <classPathEntry location="D:\auto\mysql-connector-java-5.1.36.jar" />
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!-- 数据库链接URL、用户名、密码 -->
        <!-- <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/sy" userId="sypro" password="sypro"> -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ssm1" userId="root" password="">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.yc.ssm.cinema.entity" targetProject="D:\auto\src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成的映射文件包名和位置 -->
        <sqlMapGenerator targetPackage="com.yc.ssm.cinema.mapping" targetProject="D:\auto\src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.yc.ssm.cinema.mapper" targetProject="D:\auto\src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 要生成那些表(更改tableName和domainObjectName就可以) -->
        <table tableName="filminfo" domainObjectName="FilmInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />
        <table tableName="filmtype" domainObjectName="FilmType" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" />

    </context>
</generatorConfiguration>


然后介入命令行,切换到当前目录,运行命令

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


如图


结果:













接下来就可以将生成好的实体类和映射文件拷贝到项目当中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: