1.Oracle、MySQL插入時返回下一個主鍵的操作
Oracle:
<insert id="insert" parameterClass="ROLE">
<selectKey keyProperty="id" resultClass="java.lang.Long" type="pre">
SELECT SEQ_ROLE.NEXTVAL AS ID FROM DUAL
</selectKey>
insert into ROLE(ID, NAME, CREATE, MODIFY) values (#{id}, #{name}, sysdate, sysdate)
</insert>
注意:這邊的keyProperty="id"中"id"指的是Java對象ROLE中的屬性,而並非數據庫中ROLE表的屬性。
1.2.Oracle、MySQL插入時返回當前主鍵的操作
Oracle:
<insert id="insert" parameterClass="ROLE">
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="roleId">
SELECT seq_LSP_ROLE.currval as ROLEID from DUAL
</selectKey>
insert into ROLE(ID, NAME, CREATE, MODIFY) values (#{id}, #{name}, sysdate, sysdate)
</insert>
MySQL:
<insert id="insert" parameterType="Role" useGeneratedKeys="true" keyProperty="roleId">
insert into role (name, create_time, update_time) values (#{name,jdbcType=VARCHAR}, now(), now())
</insert>
注意:role表的role_id字段是自動增長的,所以插入時不需要傳入值;keyProperty="roleId"中"roleId"指的是Java對象Role中的屬性,而並非數據庫中role表的屬性。
2.查詢‘_’,'%' 這樣的字符
java: roleName= roleName.replaceAll("_", "/_");
roleName= roleName.replaceAll("%", "/%");
<select id="selectRole" parameterType="java.util.Map"
resultType="java.lang.Integer">
SELECT COUNT(1) FROM LSP_ROLE_TAB T
<where>
<if test="roleName != null and roleName != ''">
AND T.ROLE_NAME LIKE CONCAT('%',CONCAT(#{roleName},'%')) ESCAPE '/'
</if>
<if test="1==1">
AND T.DELETE_FLAG = 0
</if>
</where>
</select>
3.插入、修改時存儲創建、修改時間為數據庫時間
Oracle: insert into role(ID, NAME, CREATE, MODIFY) values (#{id}, #{name}, sysdate, sysdate)
MySQL: insert into role (name, create_time, update_time) values (#{name,jdbcType=VARCHAR}, now(), now())
4.關系運算符的處理及轉義
主要處理的是小於/小於等於符號(”<“/”<=“)與XML的尖括號”<“之間的沖突。可以用以下代碼解決:
<where>
<if test="id != null">
<![CDATA[ AND T.ID < #{id} ]]>
</if>
...
</where>
包含在<![CDATA[ ]]>之間可避免
5.批量插入、修改、刪除操作
<!-- MySQL批量插入 --> <insert id="insertBatch" parameterType="java.util.List"> insert into role_authority (role_id, authority_id) values <foreach collection="list" item="item" index="index" separator=","> (${item.roleId}, ${item.authorityId}) </foreach> </insert>
<!-- MySQL、Oracle通過主鍵集合批量刪除記錄 --> <delete id="batchRemove" parameterType="java.util.List"> DELETE FROM ld_user WHERE id IN <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </delete>
<!-- 批量修改與批量刪除類似 注意:SQL的執行長度有限制,需要注意不能超長-->
6.模糊查詢
<if test="entName != null and entName != ''">
AND t2.`name` LIKE "%"#{entName,jdbcType=VARCHAR}"%"
<!-- '%${entName,jdbcType=VARCHAR}%' -->
</if>
<if test="prdName != null and prdName != ''">
AND t3.`name` LIKE "%"#{prdName,jdbcType=VARCHAR}"%"
<!-- '%${prdName,jdbcType=VARCHAR}%' -->
</if>
<if test="roleName != null and roleName != ''">
AND T.ROLE_NAME LIKE CONCAT('%',CONCAT(#{roleName},'%'))
</if>
6.分頁
<!--分頁結果 oracle-->
<select id="selectPageList" parameterType="java.util.Map"
resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from (select ROWNUM as rn,
<include refid="Base_Column_List" />
FROM LSP_MUTUALINFO_TAB t
<where>
<if test="serverName != null and serverName != ''">
and t.SERVER_NAME like CONCAT('%',CONCAT(#{serverName},'%')) escape '/'
</if>
<if test="serverId != null and serverId != ''">
and t.SERVER_ID = #{serverId,jdbcType=VARCHAR}
</if>
<![CDATA[AND ROWNUM <= #{iPageSize} ]]>
</where>)
<where> <![CDATA[AND rn > #{iPageIndex} ]]></where>
</select>
7.oracle中刪除表中某字段出現重復的信息 保留其中一條
1、將根據name相同 ID不同來的方式來判斷(id必須唯一)
delete from test a where exists (select null from test b where b.name=a.name and b.id>a.id);
2、用rowid 來代替其中的id,比上面的方法更適用,沒有字段唯一限制
delete from test a where exists (select null from test b where b.name=a.name and b.rowid>a.rowid);
3、 通過分析函數根據name 分組生成序號,然后刪除序號大於1 的數據
這里的ROW_NUMBER() OVER (partition by name order by name) 是先把name列升序,再為降序以后的沒條name記錄返回一個序號,
delete from test where rowid in (select rowid from (select rowid as rid, row_number() over(partition by name order by id) as seq from test) where seq > 1);