Mybatis常见技巧

参考博客:https://javaguide.cn/interview-preparation/teach-you-how-to-prepare-for-the-interview-hand-in-hand.html

mybatis中查询与更新、新建语句

查询与更新resultMap返回结果类型。parameterType表示参数类型

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

mybatis sql语句转化

SELECT
<include refid=“Base_Column_List”/>
FROM
mmall_product
<where>
<if test=“productName != null”>
and name=#{productName}
</if>
<if test=“productId”>
and id=#{productId}
</if>
</where>
标签对作用:如果where标签下的if标签成立,则将成立的第一个if标签下的and或or转为 where

<if test=“”>标签的条件判断

使用方法

在这里插入图片描述

非字符串类型的特殊用法

在这里插入图片描述

Mapper中的参数类型parameterMap、parametertype、resultMap和resultType

文章转载自: https://blog.csdn.net/yeshu2780/article/details/104551250/

入参类型

一、Type类型
parameterType 和 resultType是直接接受Java类,并与SQL的列完成自动映射关系
1、parameterType
<insert id=“insert” parameterType=“String”>
insert into optionTemp(id) values (#{id})
</insert>

<insert id=“insert” parameterType=“com.bean.Stu”>
insert into Stu(id,name,age) values (#{id},#{name},#{age})
</insert>

<insert id=“insert” parameterType=“map”>
insert into Stu(id,name,age) values (#{id},#{name},#{age})
</insert>
传入的任意类型的数据可以被看成封装成map<key , val>
例如传入com.bean.Stu类后实质被封装为map<id,id_val>,map<name,name_val>,map<age,age_val>,在mapper中则会通过 #{id} 进行取值

2、ResultType
返回Map类型
#返回list => map<列名,列表>
<select id=“selectAll” resultType=“map”>
select id col1,name col2, age col3 from Stu
</select>
在这里插入图片描述
返回封装的Bean类

#返回的是 List
<select id=“selectAll” resultType=“com.bean.Stu”>
select id,name,age from Stu
</select>
在这里插入图片描述
二、Map类型

在Map的类型中,需要自己来映射sql中的列 与 (Map中的key或者bean中的属性)关系

1、parameterMap
parameterMap中的stuMes类对象在java代码里定义
<insert id=“updateMortPrice” statementType=“CALLABLE” parameterMap=“stuMess”>
call Mort.PK_VALUATION.sp_add_mortgage_price(?,?,?,?,?)
</insert>

<resultMap id = “stuMess” type=“map”>
<result jdbcType=“varchar” mode=“IN” property=“id”>
<result jdbcType=“varchar” mode=“IN” property=“name”>
<result jdbcType=“varchar” mode=“IN” property=“age”>
<!–接受返回的类型–>
<result jdbcType=“numeric” mode=“OUT” property=“o_errcode”>
<result jdbcType=“varchar” mode=“OUT” property=“o_errmsg”>
</resultMap>

2、ResultMap

sql语句类型
<select id=“searchAll” resultMap = “stuMess”>
select a.id , a.name , a.age , b,classId . b.schoolId from Stu a,School b where a.id = b.id
</select>

<resultMap id = “stuMess” type=“map”>
<result column=“id” property=“id”>
<result column=“id” property=“id”>
<result column=“id” property=“id”>
<result column=“id” property=“id”>
<result column=“id” property=“id”>
</resultMap>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值