【Mybatis】xml常用总结(持续更新)
最佳答案 问答题库768位专家为你答疑解惑
目录
一、常用增删改查
查询(SELECT)操作
插入(INSERT)操作
更新(UPDATE)操作
删除(DELETE)操作
批量插入(INSERT)操作
批量删除(DELETE)操作
批量更新(UPDATE)操作
参数传递
结果映射
动态SQL
二、常用关键词总结
关键字表格总结
SQL举例
三、其他一些总结
处理数据转数据
生成指定表的insert语句
一、常用增删改查
下面是关于MyBatis XML配置中增删改查以及批量操作的常用写法总结:
查询(SELECT)操作
<select id="getUserById" resultType="com.example.User">SELECT * FROM users WHERE id = #{id}
</select><select id="queruByIds" parameterType="com.example.User" resultType="com.example.User">SELECT *where id in<foreach collection="list" item="item" open="(" close=")" separator=",">#{item}</foreach>;
</select>
这个示例中,`getUserById`是查询操作的唯一标识符,`resultType`指定了返回结果的类型。
插入(INSERT)操作
<insert id="insertUser" parameterType="com.example.User">INSERT INTO users (id, name, email) VALUES (#{id}, #{name}, #{email})
</insert>
这个示例中,`insertUser`是插入操作的唯一标识符,`parameterType`指定了传入参数的类型。
更新(UPDATE)操作
<update id="updateUser" parameterType="com.example.User">UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}
</update>
这个示例中,`updateUser`是更新操作的唯一标识符,`parameterType`指定了传入参数的类型。
删除(DELETE)操作
<delete id="deleteUserById" parameterType="int">DELETE FROM users WHERE id = #{id}
</delete>
这个示例中,`deleteUserById`是删除操作的唯一标识符,`parameterType`指定了传入参数的类型。
批量插入(INSERT)操作
<insert id="batchInsert" parameterType="java.util.List">INSERT INTO users (id, name, email)VALUES<foreach collection="list" item="user" separator=",">(#{user.id}, #{user.name}, #{user.email})</foreach>
</insert>
这个示例中,`insertUsers`是批量插入操作的唯一标识符,`parameterType`指定了传入参数的类型。使用`<foreach>`标签来循环遍历传入的列表进行批量插入。
批量删除(DELETE)操作
<delete id="deleteUsersByIds" parameterType="java.util.List">DELETE FROM users WHERE id IN<foreach collection="list" item="id" open="(" close=")" separator=",">#{id}</foreach>
</delete><update id="batchDel">update table_name set deleted = 1where deleted = 0<if test="list!=null and list.size != 0 ">and id in<foreach collection="list" item="item" open="(" separator="," close=")">#{item}</foreach></if>
</update>
这个示例中,`deleteUsersByIds`是批量删除操作的唯一标识符,`parameterType`指定了传入参数的类型。使用`<foreach>`标签来循环遍历传入的列表进行批量删除。
批量更新(UPDATE)操作
<update id="updateUsers" parameterType="java.util.List"><foreach collection="list" item="user" separator=";">UPDATE users SET name = #{user.name}, email = #{user.email} WHERE id = #{user.id}</foreach>
</update>
这个示例中,`updateUsers`是批量更新操作的唯一标识符,`parameterType`指定了传入参数的类型。使用`<foreach>`标签来循环遍历传入的列表进行批量更新。
参数传递
在上述示例中,使用了`#{parameter}`语法来引用传入的参数。例如,`#{id}`引用了传入参数中的`id`字段。
结果映射
使用`resultType`指定查询结果的类型,或者使用`resultMap`来进行更复杂的结果映射。
动态SQL
MyBatis支持动态SQL,可以根据条件生成不同的SQL语句。例如,使用`<if>`、`<choose>`、`<when>`和`<otherwise>`等标签来编写动态SQL。
这些是一些常用的MyBatis XML配置写法总结,涵盖了增删改查等操作。您可以根据具体需求和情况进行相应的配置和使用。
二、常用关键词总结
关键字表格总结
SQL举例
<select id="select"resultType="com.god.demo.app.entity.vo.DemoVo"parameterType="com.god.demo.app.entity.dto.DemoDTO">SELECT-- 加序号 @i := @i + 1 AS row,( SELECT @i := 0 ) r@i := @i + 1 AS row,ac.`name` companyName,ac.`name` nnn,ad.`name` departmentName,sus.userName creatorName,-- case when then else endCASEWHEN NOW() <= su.effective_start_time THEN "未生效"WHEN NOW() >= su.effective_start_time AND NOW() < su.effective_end_time THEN "生效中"WHEN NOW() >= su.effective_end_time THEN "已过期"ELSE NULLEND effectiveStatus,concat( TRUNCATE ( IFNULL( su.user_integral / su.user_integral, 0 ) * 100, 2 ), '%' ) ratio,-- 想要的时间格式DATE_FORMAT( su.create_time, '%Y-%m-%d %H:%I:%s' ) createTime1,DATE_FORMAT( su.create_time, '%Y-%m-%d %H:%I' ) createTime2,DATE_FORMAT( su.create_time, '%Y-%m-%d %H' ) createTime3,DATE_FORMAT( su.create_time, '%Y-%m-%d' ) createTime4,DATE_FORMAT( su.create_time, '%Y-%m' ) createTime5,DATE_FORMAT( su.create_time, '%Y' ) createTime6,-- 关联俩字段concat_ws( '-', su.create_time, su.create_time ) time,-- 符号转义方法1:<![CDATA[< ]]>CASEWHEN su.user_integral <![CDATA[< ]]> 1000 THEN '普通会员'WHEN su.user_integral <![CDATA[>= 1000 AND su.user_integral <= ]]> 10000 THEN '黄金会员'WHEN su.user_integral <![CDATA[>= 10000 AND su.user_integral <= ]]> 20000 THEN '钻石会员'WHEN su.user_integral <![CDATA[>= ]]> 20000 THEN '星钻会员'ELSE '普通用户' END integralLevel,su.overage amount,su.*FROMsys_user suLEFT JOIN app_company ac ON su.company_id = ac.idLEFT JOIN app_department ad ON su.department_id = ad.idLEFT JOIN (SELECT s.id, s.`name` userName FROM sys_user s WHERE s.del_flag = '0' AND s.`status` = '1' ANDDATE_FORMAT( s.create_time, '%Y' )= '2020' ) sus ON su.creator_id = sus.id,( SELECT @i := 0 ) r<where>su.del_flag='0'<if test="companyId != null and companyId !=''">AND su.company_id=#{companyId}</if><if test="departmentId != null and departmentId !=''">AND su.department_id=#{departmentId}</if><if test="postId != null and postId !=''">AND su.post_id=#{postId}</if><if test="level != null and level !=''">AND su.user_level=#{level}</if>-- 模糊比较<if test="name != null and name !=''">AND su.name like CONCAT('%', #{name,jdbcType=VARCHAR}, '%')</if><if test="email != null and email !=''">AND su.email like CONCAT('%', #{email,jdbcType=VARCHAR}, '%')</if><if test="phone != null and phone !=''">AND su.phone like CONCAT('%', #{phone}, '%')</if>-- 字符转义1: <![CDATA[< ]]><if test="businessStartDate != null">and t.business_date <![CDATA[>= ]]> #{businessStartDate}</if><if test="businessStartDate != null">and t.business_date >= #{businessStartDate}</if><if test="businessEndDate != null">and <![CDATA[ t.business_date <= #{businessEndDate} ]]></if>-- 字符转义2 < ><if test="age != null and age != ''">and age >= #{age}</if>-- 数组判空<if test="phoneArray != null and phoneArray.length > 0">and 1=1</if>-- 集合判空<if test="phoneList != null and phoneList.size() > 0">AND su.phone in<foreach collection="phoneList" index="index" item="item" open="(" separator="," close=")">#{item}</foreach></if><if test="statusList!=null">and status in<foreach collection="statusList" item="item" open="(" separator="," close=")">#{item}</foreach></if>-- 集合模糊匹配<if test="happys != null and happys.size != 0">and<foreach collection="happys" item="happy" open="(" separator=" or " close=")">happy like concat('%', #{happy}, '%')</foreach></if><include refid="Base_date_param"/></where>ORDER BYsu.create_time</select>
三、其他一些总结
处理数据转数据
<insert id="insertUser">INSERT INTO demo1 ( `id`, `name`, `age`, `company`, `department`, `level`, `deleted` ) SELECT`c_id` id,`first_name` NAME,18 age,"阿凡哒" company,"xx" department,1 LEVEL,'0' deletedFROMdemo2WHEREdeleted = '0'</insert>
生成指定表的insert语句
SELECTCONCAT( 'INSERT INTO table_name (id,name,age,company,department,deleted) VALUES(', id, ', "', NAME, '", "', age, '", "', company, '", "', department, '", ', deleted, ');' ) AS insert_statement
FROMtable_name
WHEREdeleted = 0;
欢迎大佬们留言补充!
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"【Mybatis】xml常用总结(持续更新)":http://eshow365.cn/6-33625-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 【C语言基础教程】函数指针与指针大小
- 下一篇: Paste v4.1.2(Mac剪切板)