由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。

一、建立表结构

CREATE TABLE `user` (
  `id` varchar(50) NOT NULL,
  `username` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(18) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `name` varchar(18) DEFAULT NULL,
  `sex` varchar(2) DEFAULT NULL,
  `birthday` varchar(50) DEFAULT NULL,
  `address` varchar(500) DEFAULT NULL,
  `tel` varchar(18) DEFAULT NULL,
  `qq` varchar(18) DEFAULT NULL,
  `image` varchar(50) DEFAULT NULL,
  `sfjh` varchar(1) DEFAULT NULL,
  `sfzx` varchar(1) DEFAULT NULL,
  `sfhf` varchar(1) DEFAULT NULL,
  `sfpl` varchar(1) DEFAULT NULL,
  `sffx` varchar(1) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf-8;

二、下载mybatis-generator-core

进入:http://code.google.com/p/mybatis/

选择Downloads,再选择MyBatis Generator Tool下载即可。

三、生成配置文件

新建一个空的XML配置文件,名称可以随便取,这里以generatorConfig.xml为名。最好将这个文件放在下载后的lib目录中,如图:

其中mysql的驱动可以随便放在非中文路径的地方,这里为了方便就放在lib目录下。

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.xml这个文件的具体内容:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE generatorConfiguration
  3.  
    PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
  5.  
    <generatorConfiguration>
  6.  
    <!-- 数据库驱动-->
  7.  
    <classPathEntry location="mysql-connector-java-5.0.6-bin.jar"/>
  8.  
    <context id="DB2Tables" targetRuntime="MyBatis3">
  9.  
    <commentGenerator>
  10.  
    <property name="suppressDate" value="true"/>
  11.  
    <!-- 是否去除自动生成的注释 true:是 : false:否 -->
  12.  
    <property name="suppressAllComments" value="true"/>
  13.  
    </commentGenerator>
  14.  
    <!--数据库链接URL,用户名、密码 -->
  15.  
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="test" password="test">
  16.  
    </jdbcConnection>
  17.  
    <javaTypeResolver>
  18.  
    <property name="forceBigDecimals" value="false"/>
  19.  
    </javaTypeResolver>
  20.  
    <!-- 生成模型的包名和位置-->
  21.  
    <javaModelGenerator targetPackage="test.model" targetProject="src">
  22.  
    <property name="enableSubPackages" value="true"/>
  23.  
    <property name="trimStrings" value="true"/>
  24.  
    </javaModelGenerator>
  25.  
    <!-- 生成映射文件的包名和位置-->
  26.  
    <sqlMapGenerator targetPackage="test.mapping" targetProject="src">
  27.  
    <property name="enableSubPackages" value="true"/>
  28.  
    </sqlMapGenerator>
  29.  
    <!-- 生成DAO的包名和位置-->
  30.  
    <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
  31.  
    <property name="enableSubPackages" value="true"/>
  32.  
    </javaClientGenerator>
  33.  
    <!-- 要生成哪些表-->
  34.  
    <table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  35.  
    <table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  36.  
    <table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
  37.  
    </context>
  38.  
    </generatorConfiguration>

1、其中需要注意的有数据库驱动、数据库URL、用户名、密码、生成模型的包名和位置、生成映射文件的包名和位置、生成DAO的包名和位置以及最后需要生成的表名和对应的类名。

四、运行

需要通过CMD命令行方式来运行,首先可以先准备一个运行的脚本,这里使用的脚本是:java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

需要注意的是:mybatis-generator-core-1.3.2.jar为下载的对应版本的jar,generatorConfig.xml 为配置文件名,如果不为这个可以在这里进行修改。

启动cmd进入到“F:\soft\mybatis-generator-core-1.3.2\lib”这个目录下,如图:

生成成功后进到src目录下,可以看到已经生成了对应的model、dao、mapping,如图:

下面可以看看生成后的UserMapper.xml

  1.  
    <?xml version="1.0" encoding="UTF-8" ?>
  2.  
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3.  
    <mapper namespace="test.dao.UserDtoMapper" >
  4.  
    <resultMap id="BaseResultMap" type="test.model.UserDto" >
  5.  
    <id column="id" property="id" jdbcType="VARCHAR" />
  6.  
    <result column="username" property="username" jdbcType="VARCHAR" />
  7.  
    <result column="password" property="password" jdbcType="VARCHAR" />
  8.  
    <result column="email" property="email" jdbcType="VARCHAR" />
  9.  
    <result column="name" property="name" jdbcType="VARCHAR" />
  10.  
    <result column="sex" property="sex" jdbcType="VARCHAR" />
  11.  
    <result column="birthday" property="birthday" jdbcType="VARCHAR" />
  12.  
    <result column="address" property="address" jdbcType="VARCHAR" />
  13.  
    <result column="tel" property="tel" jdbcType="VARCHAR" />
  14.  
    <result column="qq" property="qq" jdbcType="VARCHAR" />
  15.  
    <result column="image" property="image" jdbcType="VARCHAR" />
  16.  
    <result column="sfjh" property="sfjh" jdbcType="VARCHAR" />
  17.  
    <result column="sfzx" property="sfzx" jdbcType="VARCHAR" />
  18.  
    <result column="sfhf" property="sfhf" jdbcType="VARCHAR" />
  19.  
    <result column="sfpl" property="sfpl" jdbcType="VARCHAR" />
  20.  
    <result column="sffx" property="sffx" jdbcType="VARCHAR" />
  21.  
    </resultMap>
  22.  
    <sql id="Base_Column_List" >
  23.  
    id, username, password, email, name, sex, birthday, address, tel, qq, image, sfjh,
  24.  
    sfzx, sfhf, sfpl, sffx
  25.  
    </sql>
  26.  
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
  27.  
    select
  28.  
    <include refid="Base_Column_List" />
  29.  
    from user
  30.  
    where id = #{id,jdbcType=VARCHAR}
  31.  
    </select>
  32.  
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
  33.  
    delete from user
  34.  
    where id = #{id,jdbcType=VARCHAR}
  35.  
    </delete>
  36.  
    <insert id="insert" parameterType="test.model.UserDto" >
  37.  
    insert into user (id, username, password,
  38.  
    email, name, sex, birthday,
  39.  
    address, tel, qq, image,
  40.  
    sfjh, sfzx, sfhf, sfpl,
  41.  
    sffx)
  42.  
    values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
  43.  
    #{email,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR},
  44.  
    #{address,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR},
  45.  
    #{sfjh,jdbcType=VARCHAR}, #{sfzx,jdbcType=VARCHAR}, #{sfhf,jdbcType=VARCHAR}, #{sfpl,jdbcType=VARCHAR},
  46.  
    #{sffx,jdbcType=VARCHAR})
  47.  
    </insert>
  48.  
    <insert id="insertSelective" parameterType="test.model.UserDto" >
  49.  
    insert into user
  50.  
    <trim prefix="(" suffix=")" suffixOverrides="," >
  51.  
    <if test="id != null" >
  52.  
    id,
  53.  
    </if>
  54.  
    <if test="username != null" >
  55.  
    username,
  56.  
    </if>
  57.  
    <if test="password != null" >
  58.  
    password,
  59.  
    </if>
  60.  
    <if test="email != null" >
  61.  
    email,
  62.  
    </if>
  63.  
    <if test="name != null" >
  64.  
    name,
  65.  
    </if>
  66.  
    <if test="sex != null" >
  67.  
    sex,
  68.  
    </if>
  69.  
    <if test="birthday != null" >
  70.  
    birthday,
  71.  
    </if>
  72.  
    <if test="address != null" >
  73.  
    address,
  74.  
    </if>
  75.  
    <if test="tel != null" >
  76.  
    tel,
  77.  
    </if>
  78.  
    <if test="qq != null" >
  79.  
    qq,
  80.  
    </if>
  81.  
    <if test="image != null" >
  82.  
    image,
  83.  
    </if>
  84.  
    <if test="sfjh != null" >
  85.  
    sfjh,
  86.  
    </if>
  87.  
    <if test="sfzx != null" >
  88.  
    sfzx,
  89.  
    </if>
  90.  
    <if test="sfhf != null" >
  91.  
    sfhf,
  92.  
    </if>
  93.  
    <if test="sfpl != null" >
  94.  
    sfpl,
  95.  
    </if>
  96.  
    <if test="sffx != null" >
  97.  
    sffx,
  98.  
    </if>
  99.  
    </trim>
  100.  
    <trim prefix="values (" suffix=")" suffixOverrides="," >
  101.  
    <if test="id != null" >
  102.  
    #{id,jdbcType=VARCHAR},
  103.  
    </if>
  104.  
    <if test="username != null" >
  105.  
    #{username,jdbcType=VARCHAR},
  106.  
    </if>
  107.  
    <if test="password != null" >
  108.  
    #{password,jdbcType=VARCHAR},
  109.  
    </if>
  110.  
    <if test="email != null" >
  111.  
    #{email,jdbcType=VARCHAR},
  112.  
    </if>
  113.  
    <if test="name != null" >
  114.  
    #{name,jdbcType=VARCHAR},
  115.  
    </if>
  116.  
    <if test="sex != null" >
  117.  
    #{sex,jdbcType=VARCHAR},
  118.  
    </if>
  119.  
    <if test="birthday != null" >
  120.  
    #{birthday,jdbcType=VARCHAR},
  121.  
    </if>
  122.  
    <if test="address != null" >
  123.  
    #{address,jdbcType=VARCHAR},
  124.  
    </if>
  125.  
    <if test="tel != null" >
  126.  
    #{tel,jdbcType=VARCHAR},
  127.  
    </if>
  128.  
    <if test="qq != null" >
  129.  
    #{qq,jdbcType=VARCHAR},
  130.  
    </if>
  131.  
    <if test="image != null" >
  132.  
    #{image,jdbcType=VARCHAR},
  133.  
    </if>
  134.  
    <if test="sfjh != null" >
  135.  
    #{sfjh,jdbcType=VARCHAR},
  136.  
    </if>
  137.  
    <if test="sfzx != null" >
  138.  
    #{sfzx,jdbcType=VARCHAR},
  139.  
    </if>
  140.  
    <if test="sfhf != null" >
  141.  
    #{sfhf,jdbcType=VARCHAR},
  142.  
    </if>
  143.  
    <if test="sfpl != null" >
  144.  
    #{sfpl,jdbcType=VARCHAR},
  145.  
    </if>
  146.  
    <if test="sffx != null" >
  147.  
    #{sffx,jdbcType=VARCHAR},
  148.  
    </if>
  149.  
    </trim>
  150.  
    </insert>
  151.  
    <update id="updateByPrimaryKeySelective" parameterType="test.model.UserDto" >
  152.  
    update user
  153.  
    <set >
  154.  
    <if test="username != null" >
  155.  
    username = #{username,jdbcType=VARCHAR},
  156.  
    </if>
  157.  
    <if test="password != null" >
  158.  
    password = #{password,jdbcType=VARCHAR},
  159.  
    </if>
  160.  
    <if test="email != null" >
  161.  
    email = #{email,jdbcType=VARCHAR},
  162.  
    </if>
  163.  
    <if test="name != null" >
  164.  
    name = #{name,jdbcType=VARCHAR},
  165.  
    </if>
  166.  
    <if test="sex != null" >
  167.  
    sex = #{sex,jdbcType=VARCHAR},
  168.  
    </if>
  169.  
    <if test="birthday != null" >
  170.  
    birthday = #{birthday,jdbcType=VARCHAR},
  171.  
    </if>
  172.  
    <if test="address != null" >
  173.  
    address = #{address,jdbcType=VARCHAR},
  174.  
    </if>
  175.  
    <if test="tel != null" >
  176.  
    tel = #{tel,jdbcType=VARCHAR},
  177.  
    </if>
  178.  
    <if test="qq != null" >
  179.  
    qq = #{qq,jdbcType=VARCHAR},
  180.  
    </if>
  181.  
    <if test="image != null" >
  182.  
    image = #{image,jdbcType=VARCHAR},
  183.  
    </if>
  184.  
    <if test="sfjh != null" >
  185.  
    sfjh = #{sfjh,jdbcType=VARCHAR},
  186.  
    </if>
  187.  
    <if test="sfzx != null" >
  188.  
    sfzx = #{sfzx,jdbcType=VARCHAR},
  189.  
    </if>
  190.  
    <if test="sfhf != null" >
  191.  
    sfhf = #{sfhf,jdbcType=VARCHAR},
  192.  
    </if>
  193.  
    <if test="sfpl != null" >
  194.  
    sfpl = #{sfpl,jdbcType=VARCHAR},
  195.  
    </if>
  196.  
    <if test="sffx != null" >
  197.  
    sffx = #{sffx,jdbcType=VARCHAR},
  198.  
    </if>
  199.  
    </set>
  200.  
    where id = #{id,jdbcType=VARCHAR}
  201.  
    </update>
  202.  
    <update id="updateByPrimaryKey" parameterType="test.model.UserDto" >
  203.  
    update user
  204.  
    set username = #{username,jdbcType=VARCHAR},
  205.  
    password = #{password,jdbcType=VARCHAR},
  206.  
    email = #{email,jdbcType=VARCHAR},
  207.  
    name = #{name,jdbcType=VARCHAR},
  208.  
    sex = #{sex,jdbcType=VARCHAR},
  209.  
    birthday = #{birthday,jdbcType=VARCHAR},
  210.  
    address = #{address,jdbcType=VARCHAR},
  211.  
    tel = #{tel,jdbcType=VARCHAR},
  212.  
    qq = #{qq,jdbcType=VARCHAR},
  213.  
    image = #{image,jdbcType=VARCHAR},
  214.  
    sfjh = #{sfjh,jdbcType=VARCHAR},
  215.  
    sfzx = #{sfzx,jdbcType=VARCHAR},
  216.  
    sfhf = #{sfhf,jdbcType=VARCHAR},
  217.  
    sfpl = #{sfpl,jdbcType=VARCHAR},
  218.  
    sffx = #{sffx,jdbcType=VARCHAR}
  219.  
    where id = #{id,jdbcType=VARCHAR}
  220.  
    </update>
  221.  
    </mapper>

接下来就可以将这三个目录拷贝到对应项目的目录中,如果需要新增自己的方法可以修改dao类。

MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping的更多相关文章

  1. mybatis generator.xml 配置 自动生成model,dao,mapping

    generator.xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE gener ...

  2. MyBatis学习---使用MyBatis_Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  3. 使用MyBatis_Generator生成Dto、Dao、Mapping

    由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类.Dao接口类甚至Mappi ...

  4. Mybatis学习笔记(三) 之Dao开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法,常用还是Mapper接口开发. SqlSession的使用范围 public class test1 { ...

  5. 使用mybatis generator插件,自动生成dao、dto、mapper等文件

    mybatis generator 介绍 mybatis generator中文文档http://mbg.cndocs.tk/ MyBatis Generator (MBG) 是一个Mybatis的代 ...

  6. mybatis 学习笔记(三):mapper 代理开发 dao 层

    mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...

  7. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  8. 【MyBatis学习04】mapper代理方法开发dao

    上一篇博文总结了mybatis使用 原始dao的方法存在的一些弊端,我们肯定不会去用它,那么mybatis中该如何开发dao呢?如题所述,这篇博文主要来总结一下使用mapper代理的方法来开发dao的 ...

  9. mybatis generator cmd 终端命令 生成dao model mapper

    mybatis generator cmd 终端命令 生成dao model mapper 文件包下载 mybatis-generator-core-1.3.2.jar 下载地址:https://gi ...

随机推荐

  1. HDU4626+博弈

    博弈... /* 博弈 对于当前人来说,如果完成自己的操作后,若mat[n][m]==0,则自己是胜者. 因为 如果mat其他位置不存在1了,肯定自己胜:如果存在1,则让下一位去反转那个1. */ # ...

  2. qq 自制表情包

      如何自制qq表情包? CreateTime--2018年2月5日15:34:35 Author:Marydon 1.添加表情 2.选择表情(可以进行多选) 3.对添加的表情进行分组 4.添加成功( ...

  3. OFBiz:添加实体栏位

    如何添加实体栏位?这里演示为PostalAddress添加planet栏位.打开applications/party/entitydef/entitymodel.xml,找到PostalAddress ...

  4. 【LeetCode】96. Unique Binary Search Trees (2 solutions)

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  5. c#通过FileStream读取、写入文件

    网上找过一些FileStream读取写入文件的代码,但是都有些小问题. 于是自己整理一下,以备不时之需.说明一下,以下代码我都运行过. 1.FileStream读取文件 // FileStream读取 ...

  6. Redis总结(三)Redis 的主从复制(转载)

    接着上一篇,前面两篇我总结了<Redis总结(一)Redis安装>和<Redis总结(二)C#中如何使用redis> 所以这一篇,会讲讲Redis 的主从复制以及C#中如何调用 ...

  7. Android编程的写法规范

    一.获取View对象 1.private EditText m_txtSmsPhone = null;//定义一个EditText控件的调用对象 m_表示为View对象 txt表示文本框 2.在pro ...

  8. 使用多字节字符集的跨平台(PC、Android、IOS、WP)编码/解码方法

    随着移动端的发展,跨平台已成为通讯架构设计的重要考虑因素,PC.Android.IOS.WP等跨多平台间的数据通讯,必然要解决字符编码/解码的问题. 多字节字符集MBCS不是跨平台的首选字符集,面向跨 ...

  9. 网站收到的url请求链接,字母全部变为小写

    http://www.ithao123.cn/content-5360465.html

  10. atitit,it人怎么样才容易事业成功?? 有以下五种性格的人容易成功

    atitit,it人怎么样才容易事业成功?? 有以下五种性格的人容易成功 有以下五种性格的人容易成功 一.不撞南墙不回头的人:由于这种人有脚踏实地,做事拼命.不撞南墙不回头的性格特点, 势有不到黄河心 ...