表名/条件/字段 都可以传入进去

<?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 >
    DELETE FROM ${tablename} where ${key}=#{value}
  </delete>

  <!--通用更新-->
  <update >
    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 >
    select COLUMN_NAME,DATA_TYPE from user_tab_cols WHERE TABLE_NAME=#{0} ORDER BY COLUMN_ID
  </select>

  <!--获取表字段-->
  <select >
    select COLUMN_NAME from user_tab_cols WHERE TABLE_NAME=#{0} ORDER BY COLUMN_ID
  </select>

  <!--通用插入方法-->
  <insert >
    <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 >
    select
    <foreach collection="fields" item="field" separator=",">
      ${field}
    </foreach>
    from ${tablename} where
    ${con1}=#{val1}
  </select>

  <!--两个条件的情况下,获取map返回值-->
  <select >
    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 >
    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 >
    select ${field} from ${tablename} where ${con} = #{val}
  </select>

</mapper>

  

相关文章:

  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
  • 2021-10-26
  • 2021-10-22
猜你喜欢
  • 2021-08-18
  • 2021-10-09
  • 2022-01-19
  • 2021-08-25
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
相关资源
相似解决方案