generator-mybatis-generator-1.3.6生成实体类和Mapper.xml文件详解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fzy198926/article/details/79354860

在学习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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

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

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

如图这里写图片描述

结果: 
这里写图片描述 
这里写图片描述 
这里写图片描述 
这里写图片描述 
接下来就可以将生成好的实体类和映射文件拷贝到项目当中

猜你喜欢

转载自blog.csdn.net/fzy198926/article/details/79354860