Mybatis Common Mapper文件


表名/條件/字段 都可以傳入進去

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.CommonDao">

  <!--通用刪除-->
  <delete id="delete" parameterType="map">
    DELETE FROM ${tablename} where ${key}=#{value}
  </delete>

  <!--通用更新-->
  <update id="update" parameterType="map">
    update ${tablename} set
    <foreach collection="data.keys" item="key" separator=",">
      <if test="null!=data[key]">
        ${key}=#{data[${key}]}
      </if>
    </foreach>
    where 1=1
    <foreach collection="conditions.keys" item="key" separator="and" open="and">
      <if test="null!=conditions[key]">
        ${key}=#{conditions[${key}]}
      </if>
    </foreach>
  </update>

  <!--查詢表字段信息-->
  <select id="getTableFieldInfo" parameterType="string" resultType="map">
    select COLUMN_NAME,DATA_TYPE from user_tab_cols WHERE TABLE_NAME=#{0} ORDER BY COLUMN_ID
  </select>

  <!--獲取表字段-->
  <select id="getTableField" parameterType="string" resultType="string">
    select COLUMN_NAME from user_tab_cols WHERE TABLE_NAME=#{0} ORDER BY COLUMN_ID
  </select>

  <!--通用插入方法-->
  <insert id="insert" parameterType="map">
    <selectKey keyProperty="id" resultType="integer" order="BEFORE">
      select ${seq}.nextval from dual
    </selectKey>
    insert into ${tablename}
    (id,
      <foreach collection="map.keys" item="key" separator=",">
          ${key}
      </foreach>
    )
    values
    (#{id},
      <foreach collection="map.values" item="value" separator=",">
          #{value}
      </foreach>
    )
  </insert>

  <!--一個條件的情況下,獲取map返回值-->
  <select id="getResultMapByCondition" parameterType="map" resultType="map">
    select
    <foreach collection="fields" item="field" separator=",">
      ${field}
    </foreach>
    from ${tablename} where
    ${con1}=#{val1}
  </select>

  <!--兩個條件的情況下,獲取map返回值-->
  <select id="getResultMapByConditions" parameterType="map" resultType="map">
    select
    <foreach collection="fields" item="field" separator=",">
      ${field}
    </foreach>
    from ${tablename}
    where 1=1
    <foreach collection="conditions.keys" item="key" separator="and" open="and">
      <if test="null!=conditions[key]">
        ${key}=#{conditions[${key}]}
      </if>
    </foreach>
  </select>

  <!--查詢一個字段,根據多個條件-->
  <select id="getResultObjectByConditions" parameterType="map" resultType="object">
    select ${field} from ${tablename}
    where 1=1
    <foreach collection="conditions.keys" item="key" separator="and" open="and">
      <if test="null!=conditions[key]">
        ${key}=#{conditions[${key}]}
      </if>
    </foreach>
  </select>

  <!--查詢一個字段-->
  <select id="getResultObjectByCondition" parameterType="map" resultType="object">
    select ${field} from ${tablename} where ${con} = #{val}
  </select>

</mapper>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM