https://www.w3cschool.cn/mybatis/7zy61ilv.html
- 常见的语法
-
<select>
<select id= "getId" paramerType="com.xx.xx" resultType="com.xx.xx">
select id
from a
where id = #{id}
</select> -
<insert>
--支持返回自增组件id
</insert id="insertId" parameterType="com.xx.xx" useGeneratedKeys="true" keyProperty="xx.id">
insert into table_name (id,
)
values(#{xx.id,jdbcType=xx})
</insert> -
<update>
<update id="updateid">
update table_name
<set>
id = #{id}
</set>
where id = #{id}
</update> -
<delete id="delete" parameterType="com.xx">
<delete>
delete from table_name where id=#{id}
</delete>
1.1 参数引用
参数引用使用格式:#{name}
对象参数引用格式:#{object.name}
如果引用的值在动态sql中,那么就不需要加#
2 resultMap --定义mysql格式和输出格式的定义方式
两个基本属性,id标记resultMap的名称,type表示映射之后的类名称
<resultMap id="myMap" type="com.xx.xx">
<id column="id" > </id>
<result column="abc" property="ABC" jdbcType="varchar" javaType="java.Lang.String"></result>
<resultMap>
resultMap的使用方式
<select id="select" resultMap="myMap">
select id from table_name
</select>
- sql 复用
<sql id = "baseColumns">
id,gmt_create
</sql>
定义的sql复用格式举例:<include refid="baseColumns"></include>
<select id="">
select <include refid="baseColumns"></include>
from a
</select> - 动态sql
-
<if> if动态判断
举例
<select>
select id
from a
where status =0
<if test='title != null'>title like #{title}</if>
</select> -
<choose> // case when 判断
-
<where> //消除where中多余的and
- <set> //消除动态语句中的,
<update>
update table
<set>
<if test="name != null">name=#{name} </if>
</set>
where id = #{id}
</update> - foreach //遍历数组
<select>
select if
from table
where id in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
list传参:注意点:foreach中collection的属性值名称是传参的名称
网友评论