日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis 动态传sql可以查询表名,任意表名,不固定字段的个数返回未定义的类型以及增删改...

發(fā)布時間:2023/12/10 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis 动态传sql可以查询表名,任意表名,不固定字段的个数返回未定义的类型以及增删改... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mysql查詢表名:

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

查詢表中的字段:

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

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

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

今天做項目,遇到的問題就是需求修改數(shù)據(jù)表的記錄,而且字段名都不是固定的,也就是說是需要通過參數(shù)傳入的,

mybatis動態(tài)傳表名和字段不規(guī)定個數(shù)進(jìn)行條件查詢使用map返回

也可以進(jìn)行動態(tài)的表名字段 進(jìn)行增刪改

注意:表名要用${}以為表名不需要預(yù)編譯

dao層:

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:

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

?

  這里我是使用一個實體類接收字段和字段值進(jìn)行傳參

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

  

?這樣就可以了,本人已測試成功!

轉(zhuǎn)載于:https://www.cnblogs.com/blackCatFish/p/10957884.html

總結(jié)

以上是生活随笔為你收集整理的Mybatis 动态传sql可以查询表名,任意表名,不固定字段的个数返回未定义的类型以及增删改...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。