maybatis逆向工程生成的mapper.xml详细解说

本文详细解读了MyBatis逆向工程生成的Mapper.xml文件,包括resultMap、sql片段、增删查改操作的配置。讲解了如何通过resultMap映射数据库字段与实体类属性,以及insert、update、select、delete标签的使用方法,旨在帮助开发者理解MyBatis的基础操作和优化技巧。
摘要由CSDN通过智能技术生成

maybatis逆向工程生成的mapper.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">
  <!--
      mybatis逆向工程生成的sql语句只会进行单表操作,不会进行复杂的操作
  -->
<mapper namespace="com.example.springboot012mybatis02.mapper.StudentMapper">
  <resultMap id="BaseResultMap" type="com.example.springboot012mybatis02.model.Student">
    <!--
     id标签只能修饰主键字段
     除了主键以外的字段都用result修饰
     column:列,数据库中的字段名称
     jdbcType:列在jdbc中的字段类型(可以省略不写)
     property:映射对象的属性名称
     resultMap的作用:
       1.当数据库字段名称与实体类对象属性名不一致时,可以进行转换
       2.当前查询结果没有对应一个表时,可以自定义一个结果集
 -->
    <id column="sid" jdbcType="INTEGER" property="sid" />
    <result column="sname" jdbcType="CHAR" property="sname" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="stime" jdbcType="TIMESTAMP" property="stime" />
  </resultMap>

  <!--
       sql语句片段,将公共的部分抽取出来
       可以通过  <include refid="Base_Column_List" />引用
   -->
  <sql id="Base_Column_List">
    sid, sname, age, stime
  </sql>



  <!--
      id:与dao的名称一致
      parameterType:可写可不写,写了就按这种格式生成
      resultMap:查询最终映射的集合
  -->
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">

--  Base_Column_List将公共部分抽取出来,相当于
--  select
--     sid, sname, age, stime
    select
    <include refid="Base_Column_List" />
    from student
    where sid = #{sid,jdbcType=INTEGER}
  </select>


  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from student
    where sid = #{sid,jdbcType=INTEGER}
  </delete>

  <!--
      这种insert必须将列出的字段全部插入,不常用
  -->
  <insert id="insert" parameterType="com.example.springboot012mybatis02.model.Student">
    insert into student (sid, sname, age, 
      stime)
    values (#{sid,jdbcType=INTEGER}, #{sname,jdbcType=CHAR}, #{age,jdbcType=INTEGER}, 
      #{stime,jdbcType=TIMESTAMP})
  </insert>

  <!--
     suffixOverrides:去除多余的逗号
     prefix:前缀
     suffix:后缀
  -->
<!--  这种insert用的多,可以选择性插入-->
  <insert id="insertSelective" parameterType="com.example.springboot012mybatis02.model.Student">
    insert into student
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        sid,
      </if>
      <if test="sname != null">
        sname,
      </if>
      <if test="age != null">
        age,
      </if>
      <if test="stime != null">
        stime,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="sid != null">
        #{sid,jdbcType=INTEGER},
      </if>
      <if test="sname != null">
        #{sname,jdbcType=CHAR},
      </if>
      <if test="age != null">
        #{age,jdbcType=INTEGER},
      </if>
      <if test="stime != null">
        #{stime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

<!--  常用,可以选择性更新-->
<!--  <set>可以自动去掉最后一个逗号-->
  <update id="updateByPrimaryKeySelective" parameterType="com.example.springboot012mybatis02.model.Student">
    update student
    <set>
      <if test="sname != null">
        sname = #{sname,jdbcType=CHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
      <if test="stime != null">
        stime = #{stime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where sid = #{sid,jdbcType=INTEGER}
  </update>

<!--不常用-->
  <update id="updateByPrimaryKey" parameterType="com.example.springboot012mybatis02.model.Student">
    update student
    set sname = #{sname,jdbcType=CHAR},
      age = #{age,jdbcType=INTEGER},
      stime = #{stime,jdbcType=TIMESTAMP}
    where sid = #{sid,jdbcType=INTEGER}
  </update>
</mapper>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值