轉與拼接:
MyBatis 是支持普通 SQL查詢,存儲過程和高級映射的優秀持久層框架。MyBatis 消除了幾乎所有的JDBC代碼和參數的手工設置以及結果集的檢索。MyBatis 使用簡單的 XML或注解用於配置和原始映射,將接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。
在mybatis的xml配置文件中,有時可以看到<![ CDATA[ ] ]> ,表示不應由xml解析器進行解析的文本數據,
早在xml中"<,>,&"等 都是非法的,"<"在xml解析中表示新元素開始,
加了cdata[],里面內容會被解析器忽略,
在實際使用中,一般將符號替換
| < | < | 小於 |
| > | > | 大於 |
| & | & | 和號 |
| ' | ' | 省略號 |
| " | " | 引號 |
例子
resultMap表示結果集是集合類型,
resultType是直接表示返回類型的(對應着我們的model對象中的實體),而resultMap則是對外部ResultMap的引用(提前定義了db和model之間的隱射key-->value關系),
但是resultType跟resultMap不能同時存在。
parameterType設置是參數類型
<select id="currentDateData" resultType="Procurement" > select <include refid="allColumn" /> from procurement <where> <![CDATA[DATEDIFF(NOW(),createDate)<6 and DATEDIFF(NOW(),createDate)>0 ]]> </where> order by refreshDatetime desc </select>
################
<resultMap id="UserResultMap" type="com.xixicat.domain.UserInfo"> <result property="id" column="id" /> <result property="username" column="username" /> <result property="sex" column="sex" /> </resultMap> <select id="getUserInfoMap" resultMap="UserResultMap"> select id,username,sex from user_info </select>
##################################
trim標識為格式化標識,可以與其他標識完成where和set的功能
prefix 前綴增加 suffix 后綴增加 prefixOverrides 自動判斷前置 suffixOverrides 自動判斷后置
接着來測試一下,在user.xml中添加
<update id="updateUserTrim" parameterType="User"> UPDATE User <trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{id}" > <if test="userName != null and userName != '' "> userName = #{userName}, </if> <if test="password != null and password != '' "> password=#{password}, </if> </trim>
