mysql和oracle的mybatis操作

1.Oracle、MySQL插入时返回下一个主键的操作

Xml代码

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);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。