1.Oracle、MySQL插入时返回下一个主键的操作
Xml代码
Oracle:
SELECT SEQ_ROLE.NEXTVAL AS ID FROM DUAL
insert into ROLE(ID, NAME, CREATE, MODIFY) values (#{id}, #{name}, sysdate, sysdate)
注意:这边的keyProperty="id"中"id"指的是Java对象ROLE中的属性,而并非数据库中ROLE表的属性。
1.2.Oracle、MySQL插入时返回当前主键的操作
Oracle:
SELECT seq_LSP_ROLE.currval as ROLEID from DUAL
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())
注意:role表的role_id字段是自动增长的,所以插入时不需要传入值;keyProperty="roleId"中"roleId"指的是Java对象Role中的属性,而并非数据库中role表的属性。
2.查询‘_’,'%' 这样的字符
java: roleName= roleName.replaceAll("_", "/_");
roleName= roleName.replaceAll("%", "/%");
resultType="java.lang.Integer">
SELECT COUNT(1) FROM LSP_ROLE_TAB T
AND T.ROLE_NAME LIKE CONCAT('%',CONCAT(#{roleName},'%')) ESCAPE '/'
AND T.DELETE_FLAG = 0
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.关系运算符的处理及转义
主要处理的是小于/小于等于符号(”
...
包含在之间可避免
5.批量插入、修改、删除操作
insert into role_authority (role_id, authority_id) values (${item.roleId}, ${item.authorityId})
DELETE FROM ld_user WHERE id IN #{item}
6.模糊查询
AND t2.`name` LIKE "%"#{entName,jdbcType=VARCHAR}"%"
AND t3.`name` LIKE "%"#{prdName,jdbcType=VARCHAR}"%"
AND T.ROLE_NAME LIKE CONCAT('%',CONCAT(#{roleName},'%'))
6.分页
resultMap="BaseResultMap">
select
from (select ROWNUM as rn,
FROM LSP_MUTUALINFO_TAB t
and t.SERVER_NAME like CONCAT('%',CONCAT(#{serverName},'%')) escape '/'
and t.SERVER_ID = #{serverId,jdbcType=VARCHAR}
)
#{iPageIndex} ]]>
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);