关闭

用Maven插件生成Mybatis代码

104人阅读 评论(0) 收藏 举报
分类:

现在代码管理基本上是采用Maven管理,Maven的好处此处不多说,大家用百度搜索会有很多介绍,本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件。

一、配置Maven pom.xml 文件

在pom.xml增加以下插件:

            <span class="kwrd"><</span><span class="html">plugin</span><span class="kwrd">></span>
                <span class="kwrd"><</span><span class="html">groupId</span><span class="kwrd">></span>org.mybatis.generator<span class="kwrd"></</span><span class="html">groupId</span><span class="kwrd">></span>
                <span class="kwrd"><</span><span class="html">artifactId</span><span class="kwrd">></span>mybatis-generator-maven-plugin<span class="kwrd"></</span><span class="html">artifactId</span><span class="kwrd">></span>
                <span class="kwrd"><</span><span class="html">version</span><span class="kwrd">></span>1.3.2<span class="kwrd"></</span><span class="html">version</span><span class="kwrd">></span>
                <span class="kwrd"><</span><span class="html">configuration</span><span class="kwrd">></span>
                    <span class="kwrd"><</span><span class="html">verbose</span><span class="kwrd">></span>true<span class="kwrd"></</span><span class="html">verbose</span><span class="kwrd">></span>
                    <span class="kwrd"><</span><span class="html">overwrite</span><span class="kwrd">></span>true<span class="kwrd"></</span><span class="html">overwrite</span><span class="kwrd">></span>
                <span class="kwrd"></</span><span class="html">configuration</span><span class="kwrd">></span>
            <span class="kwrd"></</span><span class="html">plugin</span><span class="kwrd">></span>

配置好Maven插件,下面需要配置插件需要配置文件

 

二、插件配置文件

Maven的项目配置文件存放路径如下图:

TDANAZMA{395UE}_3AYI9KX

插件默认会读到src/main/resources目录下的generatorConfig.xml 文件。

具体配置如下范例:

<span class="kwrd"><?</span><span class="html">xml</span> <span class="attr">version</span><span class="kwrd">="1.0"</span> <span class="attr">encoding</span><span class="kwrd">="UTF-8"</span> ?<span class="kwrd">></span>
<span class="kwrd"><!</span><span class="html">DOCTYPE</span> <span class="attr">generatorConfiguration</span> <span class="attr">PUBLIC</span> <span class="kwrd">"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"</span> <span class="kwrd">"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"</span> <span class="kwrd">></span>
<span class="kwrd"><</span><span class="html">generatorConfiguration</span><span class="kwrd">></span>
    <span class="kwrd"><</span><span class="html">classPathEntry</span>
        <span class="attr">location</span><span class="kwrd">="D:\repo\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar"</span> <span class="kwrd">/></span>
    <span class="kwrd"><</span><span class="html">context</span> <span class="attr">id</span><span class="kwrd">="context1"</span> <span class="attr">targetRuntime</span><span class="kwrd">="MyBatis3"</span><span class="kwrd">></span>
        <span class="kwrd"><</span><span class="html">jdbcConnection</span> <span class="attr">driverClass</span><span class="kwrd">="com.mysql.jdbc.Driver"</span>
            <span class="attr">connectionURL</span><span class="kwrd">="jdbc:mysql://xxxxxxx:8406/CL_DEMO?useUnicode=true&amp;characterEncoding=UTF-8"</span>
            <span class="attr">userId</span><span class="kwrd">="root"</span> <span class="attr">password</span><span class="kwrd">="password"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">javaModelGenerator</span> <span class="attr">targetPackage</span><span class="kwrd">="xxx.account.model"</span>
            <span class="attr">targetProject</span><span class="kwrd">="D:\workspace\项目名\src\main\java"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">sqlMapGenerator</span> <span class="attr">targetPackage</span><span class="kwrd">="xxxx.account.persistence"</span>
            <span class="attr">targetProject</span><span class="kwrd">="D:\workspace\项目名\包名\src\main\resources"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">javaClientGenerator</span> <span class="attr">targetPackage</span><span class="kwrd">="xxxx.account.persistence</span>
            <span class="attr">targetProject</span><span class="kwrd">="D:\\workspace\项目名\src\main\java"</span> <span class="attr">type</span><span class="kwrd">="XMLMAPPER"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">table</span> <span class="attr">schema</span><span class="kwrd">="CL_DEMO"</span> <span class="attr">tableName</span><span class="kwrd">="tb_user"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">table</span> <span class="attr">schema</span><span class="kwrd">="CL_DEMO"</span> <span class="attr">tableName</span><span class="kwrd">="tb_role"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">table</span> <span class="attr">schema</span><span class="kwrd">="CL_DEMO"</span> <span class="attr">tableName</span><span class="kwrd">="tb_permission"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">table</span> <span class="attr">schema</span><span class="kwrd">="CL_DEMO"</span> <span class="attr">tableName</span><span class="kwrd">="tb_role_user"</span> <span class="kwrd">/></span>
        <span class="kwrd"><</span><span class="html">table</span> <span class="attr">schema</span><span class="kwrd">="CL_DEMO"</span> <span class="attr">tableName</span><span class="kwrd">="tb_permission_role"</span> <span class="kwrd">/></span>
    <span class="kwrd"></</span><span class="html">context</span><span class="kwrd">></span>
<span class="kwrd"></</span><span class="html">generatorConfiguration</span><span class="kwrd">></span>
<span class="kwrd"></span>

在本例中,使用的是MySQL数据库,需要指定MySQL数据库的JDBC驱动。

1、指定连接数据库的URL;

2、指定生成数据模型对象的包名,如com.xxx.xxx.model, targetProject 指定是项目及存放model的目录。

3、sqlMapGenerator  需要设置包名,和存放映射文件的路径。如果用maven 管理,一般xml文件放在src/main/resources目录下。

4、javaClientGenerator 需要设置包名及路径。

6、接下来需要配置你需要生成的表名。

 

三、生成代码

配置搞掂后,现在就可以生成代码了,

FJ@3OL0KA]HA200ME1D8VP9

如果是在eclipse 中,选择pom.xml文件,击右键先择Run AS——>Maven Build… ——>在Goals框中输入:mybatis-generator:generate 

如果在命令行输入Maven命令即可,注意:一定是当前项目目录下运行该命令:

mvn mybatis-generator:generate

代码生成完,大功告别成。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33872次
    • 积分:483
    • 等级:
    • 排名:千里之外
    • 原创:0篇
    • 转载:96篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论