利用Maven自动生成mybatis的映射类文件

1.首先在pom.xml里配置包依赖和plugin插件

        <!-- 自动生成 -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
        </dependency>

在plugins标签内新加:

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>

因为我这自动生成的SQL server数据库的,所以添加支持SQL server的包依赖

<!-- 导入SQL SERVER 数据库链接jar包 -->
        <dependency>
            <groupId>com.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>2.0</version>
        </dependency>

这里maven添加jar包的cmd命令行给大家分享下:

mvn install:install-file -Dfile=D:/druid-0.2.19.jar -DgroupId=com.alibaba  -DartifactId=druid  -Dversion=0.2.19 -Dpackaging=jar

2.配置自动生成的配置文件

在项目的配置文件那(src/main/resources)创建generatorConfig.xml 里面具体代码如下:

<!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:\server\MavenRepository\maven_jar\com\sqlserver\sqljdbc4\2.0\sqljdbc4-2.0.jar"/>

    <context id="VenueGenerator"  targetRuntime="MyBatis3">
        <commentGenerator>
            <!--关闭时间注释 -->
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
       connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=MINYA_2018" userId="sa" password="Ab123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成model的包名和位置-->
        <javaModelGenerator targetPackage="com.ye_0809.bean" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射XML文件的包名和位置-->
        <sqlMapGenerator targetPackage="com.ye_0809.mapping" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成Mapper文件包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ye_0809.mapping" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成哪些表-->
        <table tableName="title" domainObjectName="Title" enableCountByExample="false" enableUpdateByExample="false" 
       enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true" />
            <generatedKey column="tit_id" sqlStatement="SqlServer" type="post" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

3.创建maven的build

右键项目如下:

刷新项目,就会在相应路径出现刚生成的映射类文件。如果没有就去

这个里找到刚创建的那个

跑完刷新项目就出现了,注意,如果生成了再生成,生成的文件就可能回出错,删除掉重新生成

猜你喜欢

转载自blog.csdn.net/u012310865/article/details/81562110