MyBatis学习4—使用MyBatis_Generator生成Dto、Dao、Mapping

MyBatis学习4—使用MyBatis_Generator生成Dto、Dao、Mapping由于MyBatis属于一种半自动的ORM框架,所以主要的工作将是书写Mapping映射文件,但是由于手写映射文件很容易出错,所以查资料发现有现成的工具可以自动生成底层模型类、Dao接口类甚至Mapping映射文件。一、建立表结构CREATETABLE`user`(`id`varchar(50)NOTNULL,`username`varchar(18)CHAR

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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

程序老哥,一名10年老程序猿,目前为广告系统技术负责人,长期工作在一线主要关注服务器后端的开发,在Java、微服务、Docker、高可用架构均有深入实践。本博客以后的更新全部迁移到微信公众号上,欢迎大家都关注本公众号,我会定时的更新各种技术文章,方便大家学习进步。

由于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目录中,如图:

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

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

自动生成最重要的就是配置文件的书写,现在就开始介绍generatorConfig.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="mysql-connector-java-5.0.6-bin.jar"/>
	<context id="DB2Tables"	targetRuntime="MyBatis3">
		<commentGenerator>
			<property name="suppressDate" value="true"/>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true"/>
		</commentGenerator>
		<!--数据库链接URL,用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/test" userId="test" password="test">
		</jdbcConnection>
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false"/>
		</javaTypeResolver>
		<!-- 生成模型的包名和位置-->
		<javaModelGenerator targetPackage="test.model" targetProject="src">
			<property name="enableSubPackages" value="true"/>
			<property name="trimStrings" value="true"/>
		</javaModelGenerator>
		<!-- 生成映射文件的包名和位置-->
		<sqlMapGenerator targetPackage="test.mapping" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</sqlMapGenerator>
		<!-- 生成DAO的包名和位置-->
		<javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src">
			<property name="enableSubPackages" value="true"/>
		</javaClientGenerator>
		<!-- 要生成哪些表-->
		<table tableName="about" domainObjectName="AboutDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="user" domainObjectName="UserDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
		<table tableName="syslogs" domainObjectName="SyslogsDto" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
	</context>
</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”这个目录下,如图:

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

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

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

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

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="test.dao.UserDtoMapper" >
  <resultMap id="BaseResultMap" type="test.model.UserDto" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="username" property="username" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="name" property="name" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="VARCHAR" />
    <result column="birthday" property="birthday" jdbcType="VARCHAR" />
    <result column="address" property="address" jdbcType="VARCHAR" />
    <result column="tel" property="tel" jdbcType="VARCHAR" />
    <result column="qq" property="qq" jdbcType="VARCHAR" />
    <result column="image" property="image" jdbcType="VARCHAR" />
    <result column="sfjh" property="sfjh" jdbcType="VARCHAR" />
    <result column="sfzx" property="sfzx" jdbcType="VARCHAR" />
    <result column="sfhf" property="sfhf" jdbcType="VARCHAR" />
    <result column="sfpl" property="sfpl" jdbcType="VARCHAR" />
    <result column="sffx" property="sffx" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, username, password, email, name, sex, birthday, address, tel, qq, image, sfjh, 
    sfzx, sfhf, sfpl, sffx
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from user
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from user
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="test.model.UserDto" >
    insert into user (id, username, password, 
      email, name, sex, birthday, 
      address, tel, qq, image, 
      sfjh, sfzx, sfhf, sfpl, 
      sffx)
    values (#{id,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=VARCHAR}, 
      #{address,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}, #{qq,jdbcType=VARCHAR}, #{image,jdbcType=VARCHAR}, 
      #{sfjh,jdbcType=VARCHAR}, #{sfzx,jdbcType=VARCHAR}, #{sfhf,jdbcType=VARCHAR}, #{sfpl,jdbcType=VARCHAR}, 
      #{sffx,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="test.model.UserDto" >
    insert into user
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="username != null" >
        username,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="email != null" >
        email,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="sex != null" >
        sex,
      </if>
      <if test="birthday != null" >
        birthday,
      </if>
      <if test="address != null" >
        address,
      </if>
      <if test="tel != null" >
        tel,
      </if>
      <if test="qq != null" >
        qq,
      </if>
      <if test="image != null" >
        image,
      </if>
      <if test="sfjh != null" >
        sfjh,
      </if>
      <if test="sfzx != null" >
        sfzx,
      </if>
      <if test="sfhf != null" >
        sfhf,
      </if>
      <if test="sfpl != null" >
        sfpl,
      </if>
      <if test="sffx != null" >
        sffx,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="username != null" >
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null" >
        #{birthday,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="tel != null" >
        #{tel,jdbcType=VARCHAR},
      </if>
      <if test="qq != null" >
        #{qq,jdbcType=VARCHAR},
      </if>
      <if test="image != null" >
        #{image,jdbcType=VARCHAR},
      </if>
      <if test="sfjh != null" >
        #{sfjh,jdbcType=VARCHAR},
      </if>
      <if test="sfzx != null" >
        #{sfzx,jdbcType=VARCHAR},
      </if>
      <if test="sfhf != null" >
        #{sfhf,jdbcType=VARCHAR},
      </if>
      <if test="sfpl != null" >
        #{sfpl,jdbcType=VARCHAR},
      </if>
      <if test="sffx != null" >
        #{sffx,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="test.model.UserDto" >
    update user
    <set >
      <if test="username != null" >
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null" >
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="name != null" >
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        sex = #{sex,jdbcType=VARCHAR},
      </if>
      <if test="birthday != null" >
        birthday = #{birthday,jdbcType=VARCHAR},
      </if>
      <if test="address != null" >
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="tel != null" >
        tel = #{tel,jdbcType=VARCHAR},
      </if>
      <if test="qq != null" >
        qq = #{qq,jdbcType=VARCHAR},
      </if>
      <if test="image != null" >
        image = #{image,jdbcType=VARCHAR},
      </if>
      <if test="sfjh != null" >
        sfjh = #{sfjh,jdbcType=VARCHAR},
      </if>
      <if test="sfzx != null" >
        sfzx = #{sfzx,jdbcType=VARCHAR},
      </if>
      <if test="sfhf != null" >
        sfhf = #{sfhf,jdbcType=VARCHAR},
      </if>
      <if test="sfpl != null" >
        sfpl = #{sfpl,jdbcType=VARCHAR},
      </if>
      <if test="sffx != null" >
        sffx = #{sffx,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="test.model.UserDto" >
    update user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      name = #{name,jdbcType=VARCHAR},
      sex = #{sex,jdbcType=VARCHAR},
      birthday = #{birthday,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      tel = #{tel,jdbcType=VARCHAR},
      qq = #{qq,jdbcType=VARCHAR},
      image = #{image,jdbcType=VARCHAR},
      sfjh = #{sfjh,jdbcType=VARCHAR},
      sfzx = #{sfzx,jdbcType=VARCHAR},
      sfhf = #{sfhf,jdbcType=VARCHAR},
      sfpl = #{sfpl,jdbcType=VARCHAR},
      sffx = #{sffx,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
</mapper>

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/179767.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月20日 下午8:46
下一篇 2022年10月20日 下午8:46


相关推荐

  • 再来利用java学学javaweb——–html+css+ JavaScript[通俗易懂]

    再来利用java学学javaweb——–html+css+ JavaScript[通俗易懂]今日内容1.web概念概述2.HTMLweb概念概述*JavaWeb: *使用Java语言开发基于互联网的项目*软件架构: 1.C/S:Client/Server客户端/服务器端 *在用户本地有一个客户端程序,在远程有一个服务器端程序 *如:QQ,迅雷… *优点: 1.用户体验好 *缺点: 1.开发、安装,部署,维护麻烦 2.B/S:Browser/Server浏览器/服务器端 *只需要一个浏览器,用户通过不同的网

    2022年7月12日
    16
  • 数据仓库ODS层的作用

    数据仓库ODS层的作用ODS(OperationalDataStore)是数据仓库体系结构中的一个可选部分,ODS具备数据仓库的部分特征和OLTP系统的部分特征,它是“面向主题的、集成的、当前或接近当前的、不断变化的”数据。  一般在带有ODS的系统体系结构中,ODS都设计为如下几个作用:  1)在业务系统和数据仓库之间形成一个隔离层  一般的数据仓库应用系统都具有非常复杂的数据来源,这些数据存放在不同

    2022年10月5日
    4
  • java 幂等性

    java 幂等性理解 HTTP 幂等性基于 HTTP 协议的 WebAPI 是时下最为流行的一种分布式服务提供方式 无论是在大型互联网应用还是企业级架构中 我们都见到了越来越多的 SOA 或 RESTful 的 WebAPI 为什么 WebAPI 如此流行呢 我认为很大程度上应归功于简单有效的 HTTP 协议 HTTP 协议是一种分布式的面向资源的网络应用层协议 无论是服务器端提供 Web 服务 还是客户端消费 Web 服务都非常

    2026年3月16日
    3
  • MySQL——事务(Transaction)详解

    MySQL——事务(Transaction)详解该博客详解MySQL中的事务一、事务定义Transaction事务:一个最小的不可再分的工作单元;通常一个事务对应一个完整的业务(例如银行账户转账业务,该业务就是一个最小的工作单元)一个完整的业务需要批量的DML(insert、update、delete)语句共同联合完成事务只和DML语句有关,或者说DML语句才有事务。这个和业务逻辑有关,业务逻辑不同,DML语句的个数不同…

    2022年5月5日
    46
  • 如何防止Arp攻击

    如何防止Arp攻击首先需要查找网关的 IP 和 MAC 地址 查看的方法很多 最直接的在 cmd 里面执行 arp a 但这是被 arp 攻击之前 被攻击成功后 192 168 0 1 对应的 mac 地址会变成攻击电脑的 Mac 地址 还有另一个方法 登录路由器查看 在浏览器输入 192 168 0 1 这里是个人的网关 然后输入帐号密码 登录进去 一般在这里都可以找到一个 lanmac 地址 这个就是 其次将本机的 arp 中的 192 168 0 1 对应的 mac 地址改成静态 在运行中输入 cmd nbsp gt nbsp arp amp

    2026年3月26日
    2
  • Manus清空微博、小红书!总部已迁至新加坡

    Manus清空微博、小红书!总部已迁至新加坡

    2026年3月15日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号