Mybatis 動態傳sql能夠查詢表名,任意表名,不固定字段的個數返回未定義的類型以及增刪改

mysql查詢表名:java

SELECT table_name FROM information_schema.tables WHERE table_schema='sell' AND table_type='base table';

查詢表中的字段:mysql

SELECT column_name FROM information_schema.columns WHERE table_schema='sell' AND table_name='seller_info'

SpringBoot 中mybatis 使用map的接收返回類型的時候爲空的字段 不反回在配置文件中sql

mybatis.configuration.call-setters-on-nulls=truemybatis

今天作項目,遇到的問題就是需求修改數據表的記錄,並且字段名都不是固定的,也就是說是須要經過參數傳入的,

mybatis動態傳表名和字段不規定個數進行條件查詢使用map返回app

也能夠進行動態的表名字段 進行增刪改測試

注意:表名要用${}覺得表名不須要預編譯this

dao層:spa

List<Map<String, String>> getTableInfo(@Param("tableName") String tableName, @Param("cloums") List<Cloum> cloums); int addTableInfo(@Param("tableName") String tableName, @Param("cloums") List<Cloum> cloums); int updateTableINfo(@Param("tableName") String tableName, @Param("cloums") List<Cloum> cloums, @Param("id") String id); int delTableInfo(@Param("tableName") String tableName, @Param("ids") List<String> id);

 

 mapper:code

<select id="getTableInfo" resultType="map"> SELECT * FROM ${tableName} where 1=1 <if test="cloums != null">
            <foreach collection="cloums" item="item" index="index" open="and" separator="and"> ${item.cloum}=#{item.val} </foreach>
        </if>

    </select>


    <insert id="addTableInfo" parameterType="com.cmbchina.ccd.itpm.entity.Cloum"> insert into ${tableName} <foreach collection="cloums" item="item" index="index" open="(" separator="," close=")"> ${item.cloum} </foreach> VALUES <foreach collection="cloums" item="item" index="index" open="(" separator="," close=")"> #{item.val} </foreach>
    </insert>
    <delete id="delTableInfo" parameterType="java.lang.String"> delete from ${tableName} where id IN <foreach collection="ids" item="item" index="index" open="(" separator="," close=")"> #{item} </foreach>
    </delete>
    <update id="updateTableINfo" parameterType="com.cmbchina.ccd.itpm.entity.Cloum"> update ${tableName} set <if test="cloums != null">
            <foreach collection="cloums" item="item" index="index" separator=","> ${item.cloum}=#{item.val} </foreach>
        </if> where id = #{id} </update>

 

  這裏我是使用一個實體類接收字段和字段值進行傳參orm

Cloum實體類:
public class Cloum {
    private String cloum;
    private String val;

    public String getCloum() {
        return cloum;
    }

    public void setCloum(String cloum) {
        this.cloum = cloum;
    }

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }
}

  

 

 這樣就能夠了,本人已測試成功!

相關文章
相關標籤/搜索