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>

如图
eclipse+maven+mybatis自动生成entity、dao层_第1张图片

generatorConfig.xml配置

在项目的根目录或src/main/resources目录下增加一个名为generatorConfig.xml的文件,路径如图
eclipse+maven+mybatis自动生成entity、dao层_第2张图片eclipse+maven+mybatis自动生成entity、dao层_第3张图片
内容格式如下:

<?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

  • 找到工具栏中的工具

  • 点击下拉箭头后点击eclipse+maven+mybatis自动生成entity、dao层_第4张图片

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

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

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

  • 运行

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

配置快捷入口

  • 点击红色框 eclipse+maven+mybatis自动生成entity、dao层_第5张图片

  • 点击add添加到快捷入口eclipse+maven+mybatis自动生成entity、dao层_第6张图片

-就会出现在这里了eclipse+maven+mybatis自动生成entity、dao层_第7张图片

你可能感兴趣的:(eclipse,maven,mybatis,自动生成)