Java mapper.xml中常用的标签详解《一》

 Java mapper.xml中常用的标签详解《二》

目录

 Java mapper.xml中常用的标签详解《二》

一、xml对应的dao层

 二、xml文件框架

各字段格式示例:

使用示例:

 三、映射管理器ResultMap

各字段格式示例:

使用示例:

四、insert标签

各字段使用规则:

各字段格式示例:

使用示例:

五、delete标签

各字段格式示例:

使用示例:

 六、update标签

各字段格式示例: 

使用示例:

七、select标签

各字段格式示例: 

使用示例:


一、xml对应的dao层

public interface StuInfoDao {

    int deleteByPrimaryKey(Long id);

    int insert(StuInfo record);

    int insertSelective(StuInfo record);

    StuInfo selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(StuInfo record);

    int updateByPrimaryKey(StuInfo record);

}

 二、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="sql映射的实体类">

      * * * * * *

</mapper>

使用示例:

<mapper namespace="com.test.dao.StuInfoDao">

    * * * * * *

</mapper>

 三、映射管理器ResultMap

        映射管理器,是Mybatis中最强大的工具,使用其可以进行实体类之间的关系,并管理结果和实体类间的映射关系。位于Xml文件中<mapper></mapper>标签首部。

各字段格式示例:

<mapper namespace="数据库表的实体类">

	<resultMap id="映射管理器的唯一标识,外部通过该值引用" type="需要映射的实体类">

		<id column="数据库字段" jdbcType="被维护实体的类型" property="被维护实体在宿主实体中的属性名" />

		<result column="数据库字段" jdbcType="被维护实体的类型" property="被维护实体在宿主实体中的属性名" />

	</resultMap>

</mapper>

使用示例:

<mapper namespace="com.test.dao.StuInfoDao">

	<resultMap id="BaseResultMap" type="com.test.bean.StuInfo">

		<id column="id" jdbcType="BIGINT" property="id" />

		<result column="name" jdbcType="VARCHAR" property="name" />

		<result column="age" jdbcType="INTEGER" property="age" />

	</resultMap>

</mapper>

四、insert标签

各字段使用规则:

id与对应dao文件方法名一致;

parameterType:参数的类型,指定自己写的实体类,或java基本类

useGeneratedKeys:开启主键回写,true|false,和keyProperty指定后,
                运行结束,实体类获取其id(id是插入后数据库自增的)

keyColumn:指定数据库的主键,设置数据表自动生成的主键名。
          对特定数据库(如PostgreSQL),若自动生成的主键不是第一个字段则必须设置

keyProperty:主键对应的属性名 statementType

各字段格式示例:

<insert id="dao层对应的唯一标识" keyColumn="id" keyProperty="id" 
    parameterType="参数的类型,指定自己写的实体类,或java基本类" useGeneratedKeys="true">

    insert 表名 ( 字段A, 字段B)
    values ( #{值A,jdbcType=字段类型}, #{值B,jdbcType=字段类型})

 </insert>

使用示例:

<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.test.bean.StuInfo" useGeneratedKeys="true">

    insert into stu_info ( `name`, age)
    values ( #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})

 </insert>

 注意:1、id自增,故无需特意给id赋值。

             2、useGeneratedKeys开启主键回写,可以在业务层实时返回新增数据主键

五、delete标签

各字段格式示例:

<delete id="dao对应的唯一标识" parameterType="参数类型">

    delete from 表名
    where 条件

 </delete>

使用示例:

<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">

    delete from stu_info

    where id = #{id,jdbcType=BIGINT}

</delete>

 六、update标签

各字段格式示例: 

<update id="updateByPrimaryKeySelective" parameterType="com.test.bean.StuInfo">
    update stu_info
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},
      </if>
      <if test="age != null">
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

使用示例:

<update id="updateByPrimaryKeySelective" parameterType="com.test.bean.StuInfo">

    update stu_info

    <set>
          <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
          </if>

          <if test="age != null">
                age = #{age,jdbcType=INTEGER},
          </if>
    </set>

    where id = #{id,jdbcType=BIGINT}

</update>

七、select标签

各字段格式示例: 

<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">

    select 查询字段A,查询字段B

    from 表名

    where 条件

</select>

使用示例:

<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">

    select 

    <include refid="Base_Column_List" />

    from stu_info

    where id = #{id,jdbcType=BIGINT}

</select>

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值