Mybatis的xml配置备忘
生活随笔
收集整理的這篇文章主要介紹了
Mybatis的xml配置备忘
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!-- 利用hashmap傳遞參數數據查詢用戶 --><select id ="selectUserById4Map" parameterType="string" resultType="hashmap" >select<include refid ="allColumns" />from user where id = #{id}</select><!-- 利用hashmap傳遞參數數據插入用戶信息 --><select id ="insertUser4Map" parameterType="hashmap" >insert into user(id,name,age,address) values(#{id},#{name},#{age},#{address})</select><!-- 模糊查詢 --><select id ="selectUserByCondition3" parameterType="com.judy.mybatis.domain.User" resultType="com.judy.mybatis.domain.User" >select * from user where 1 = 1<if test ="id!=null" >and id = #{id}</if><if test ="name!=null" >and name like '%${name}%'</if><if test ="age!=null" >and age = #{age}</if><if test ="address!=null" >and address = #{address}</if></select>
<!-- 利用動態sql語句更新user數據 --><insert id ="updateUserByCondition" parameterType="com.judy.mybatis.domain.User" >update user<set><if test ="name!=null" >name = #{name}</if><if test ="age!=null" >age = #{age}</if><if test ="address!=null" >address = #{address}</if></set>where id=#{id}</insert><!-- 動態seql語句查詢用戶 --><select id ="selectUserByCondition" parameterType="com.judy.mybatis.domain.User" resultType="com.judy.mybatis.domain.User" >select * from user where 1 = 1<if test ="id!=null" >and id = #{id}</if><if test ="name!=null" >and name = #{name}</if><if test ="age!=null" >and age = #{age}</if><if test ="address!=null" >and address = #{address}</if></select><!-- 動態seql語句查詢用戶方式2 --><!-- 這種方式會自動根據是否有id,如果有沒有id,那么第一個條件將不加and --><select id ="selectUserByCondition2" parameterType="com.judy.mybatis.domain.User" resultType="com.judy.mybatis.domain.User" >select * from user<where><if test ="id!=null" >id = #{id}</if><if test ="name!=null" >and name = #{name}</if><if test ="age!=null" >and age = #{age}</if><if test ="address!=null" >and address = #{address}</if></where></select>
<!-- 當字段很多的時候,可以通過下面這種方式抽取字段 --><sql id ="allColumns" >id,name,age,address</sql><!-- 如果在sqlMapConfig.xml中配置了別名,那么這里的resultType就可以直接寫User --><select id ="selectUserById" parameterType="string" resultMap="userMap" >select<include refid ="allColumns" />from user where id = #{id}</select>
轉載于:https://www.cnblogs.com/judylucky/p/7082786.html
總結
以上是生活随笔為你收集整理的Mybatis的xml配置备忘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合并两个排序的链表
- 下一篇: 哈希表查找速度为什么那么快?快在哪里了?