参考博客: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
#返回的是 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>