eclipse+maven+mybatis自动生成entity、dao层

eclipse+maven+mybatis自动生成entity、dao层

原文地址——http://blog.csdn.net/qq525099302/article/details/46914831

本文总结自本人真实项目,写项目时参考了网上一些资料,并非原创,只为记录分享。 ——Elong_Deo

说明

本文说的自动生成dao需要maven环境以及在maven项目中使用,分为下面三个步奏。

  • 配置项目的pom.xml文件

  • 添加并配置generatorConfig.xml

  • 配置eclipsetool以及快捷方式

pom.xml配置

在pom.xml的project>build>plugins里面添加如下代码,让maven环境支持mybatis-generator组件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.1</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.1</version>
    <configuration>
    </configuration>
</plugin>

如图
pom.xml配置

generatorConfig.xml配置

在项目的根目录或src/main/resources目录下增加一个名为generatorConfig.xml的文件,路径如图
generatorConfig.xml路径1generatorConfig.xml路径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_0.dtd" >
<generatorConfiguration>
    <!-- jdbc连接库jar包路径 -->
    <classPathEntry location="d://mysql-connector-java-5.1.27.jar" />
    <!-- 
        eclipse tool配置
        Location = E:\Java\apache-maven-3.2.3\bin\mvn.bat
        Working Direction = ${workspace_loc:/linetoy-common}
        Arguments = mybatis-generator:generate
     -->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!-- 去掉注释,一般都会去掉,那个注释真的不敢让人恭维 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- jdbc连接配置 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://111.40.50.60:3306/wp" userId="dev"
            password="1qazxsw2">
        </jdbcConnection>
        <!-- 数字字段是否强制使用BigDecimal类 -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- entity创建后放在那个项目的哪个包路径上 -->
        <javaModelGenerator targetPackage="com.linetoy.common.entity"
            targetProject="E:\workspaces\eclipse_luna\linetoy\linetoy-common\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- dao创建后放在那个项目的哪个包路径上 -->
        <sqlMapGenerator targetPackage="com.linetoy.common.dao"
            targetProject="E:\workspaces\eclipse_luna\linetoy\linetoy-common\src\main\java">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- dao的.xml描述sql文件创建后放在那个项目的哪个包路径上 -->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="com.linetoy.common.dao" 
            targetProject="E:\workspaces\eclipse_luna\linetoy\linetoy-common\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- 
            要生成的表配置,可以多个
            tableName:表名
            domainObjectName:指定类名
        -->
        <table tableName="wp_users" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false"  
            enableDeleteByExample="false" enableSelectByExample="false"  
            selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

配置eclipse的tool

  • 找到工具栏中的工具

  • 点击下拉箭头后点击配置外部工具

  • 配置

  • 新建一个program,起个名,点击第一个蓝框选择本地maven的mvn.bat文件

  • 点击第二个蓝框选择maven项目

  • 在参数框中输入mybatis-generator:generate

  • 运行

  • 刷新对应包目录查看成果

配置快捷入口

  • 点击红色框 配置快捷入口

  • 点击add添加到快捷入口添加快捷入口

-就会出现在这里了配置外部工具
统计

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值