最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis如何批量添加一對多中間表

 更新時間:2022年02月28日 08:54:37   作者:菜雞rick  
這篇文章主要介紹了mybatis如何批量添加一對多中間表,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

批量添加一對多中間表

建立中間表A,一個id對應(yīng)多個lid;

傳入兩條參數(shù)

long id;//單個數(shù)值
List lid;//集合數(shù)值

dao層語句

int insertb(@Param("id")long id,@Param("lid")List lid);

mybatis中的寫法

insert into A(id,lid) values
? ? ? ? <foreach collection="lid" item="data" separator=",">
? ? ? ? ? ? (#{id},#{data})
? ? ? ? </foreach>

多對多條件下插入中間表(使用insert標(biāo)簽的屬性)

說下需求

我的數(shù)據(jù)庫中有兩張表,一張是Blog表,一張是Type表,分別代表了博客和博客類別,它們之間是多對多關(guān)系,它們由一張中間表blog_type維護(hù)。

(簡單起見,blog表只有兩個數(shù)據(jù),id和title。type表只有id和name)

那么需求就是:

現(xiàn)在我想插入一條Blog數(shù)據(jù),因?yàn)閎log和type是多對多關(guān)系,想插入其中一個數(shù)據(jù),就得維護(hù)他們之間那個中間表blog_type的關(guān)系(插入中間表字段)。

解決方案

那么我能想到的解決方案是:

寫兩段insert標(biāo)簽,第一段sql語句插入blog表,第二段sql語句插入insert表:

    <insert id="insertBlogWithoutType" parameterType="blog">
        insert into t_blog (title)
        values (#{title});
    </insert>
    <insert id="insertBlog_Type">
        insert into blog_type (bid, tid) values(#{blog.id},#{type.id})
    </insert>

但是這么做會有它的問題

由于blog表id為自增,所以我并沒有插入id。如何在第二個insert查詢語句中獲取剛剛插入的id呢?

經(jīng)過多方查找我發(fā)現(xiàn)了解決方案:

要用到MyBatis中insert標(biāo)簽的三個屬性:

  • useGeneratedKeys (僅對 insert 和 update 有用)這會令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法來取出由數(shù)據(jù)庫內(nèi)部生成的主鍵(比如:像 MySQL 和 SQL Server 這樣的關(guān)系數(shù)據(jù)庫管理系統(tǒng)的自動遞增字段),默認(rèn)值:false。
  • keyProperty (僅對 insert 和 update 有用)唯一標(biāo)記一個屬性,MyBatis 會通過 getGeneratedKeys 的返回值或者通過 insert 語句的 selectKey 子元素設(shè)置它的鍵值,默認(rèn):unset。如果希望得到多個生成的列,也可以是逗號分隔的屬性名稱列表。
  • keyColumn (僅對 insert 和 update 有用)通過生成的鍵值設(shè)置表中的列名,這個設(shè)置僅在某些數(shù)據(jù)庫(像 PostgreSQL)是必須的,當(dāng)主鍵列不是表中的第一列的時候需要設(shè)置。如果希望得到多個生成的列,也可以是逗號分隔的屬性名稱列表。

重新生成的代碼如下所示:

    <insert id="insertBlogWithoutType" parameterType="blog" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into t_blog (title)
        values (#{title});
    </insert>
    <insert id="insertBlog_Type">
        insert into blog_type (bid, tid) values(#{blog.id},#{type.id})
    </insert>

注意!keyProperty是Java對象的屬性名!不是數(shù)據(jù)庫表中字段名!

測試

編寫Dao層

//分成兩個方法,但是他們兩個將在Service層合二為一
    int insertBlogWithoutType(Blog blog);
    int insertBlog_Type(@Param("blog")Blog blog, @Param("type")Type type);

Dao層就是對應(yīng)的前面mapper文件里的兩個方法

Service層

public void insertBlog(Blog blog, List<Type> types) {
        blogDao.insertBlogWithoutType(blog);
        for (Type type : types) {
            blogDao.insertBlog_Type(blog, type);
        }
    }

這里的意思是,先插入一個傳進(jìn)來的blog(第一個參數(shù))。然后插入之后根據(jù)插進(jìn)來的blog的主鍵(blog的id)和傳入的type的主鍵(type的id),插入中間表。

Test類

@Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        BlogService blogServiceImpl = (BlogService) context.getBean("BlogServiceImpl");
        //設(shè)置blog
        Blog blog = new Blog();
        blog.setTitle("【MyBatis】多對多條件下插入中間表(使用insert標(biāo)簽的屬性)");
        
        //設(shè)置該blog對應(yīng)的type
        List<Type> types = new ArrayList<Type>();
        types.add(new Type(1,"數(shù)據(jù)庫"));
        types.add(new Type(2,"Debug專題"));
        blogServiceImpl.insertBlog(blog, types);
    }

可以看到,設(shè)置blog的時候,并沒有設(shè)置blog的id屬性,但是調(diào)用insertBlog時,插入中間表卻已經(jīng)知道了blog的id屬性。這就是MyBatis中insert標(biāo)簽的三個屬性的作用了!

執(zhí)行完上面的代碼,數(shù)據(jù)庫里既插入了一條新的blog,又維護(hù)了他們之間那個中間表blog_type的關(guān)系(插入了中間表),至此問題解決。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

贵港市| 城口县| 绩溪县| 曲靖市| 南汇区| 太谷县| 东光县| 永平县| 怀集县| 晋江市| 灯塔市| 陆良县| 三门县| 牙克石市| 阿拉善右旗| 永德县| 建德市| 红河县| 梁山县| 凤翔县| 韩城市| 崇文区| 镇平县| 旺苍县| 咸阳市| 镇原县| 清河县| 东海县| 云梦县| 资溪县| 阿瓦提县| 同德县| 扎囊县| 巴中市| 东海县| 成安县| 额敏县| 安乡县| 利辛县| 稷山县| 贵州省|