在MyBatis的对象中持久化集合

编程入门 行业动态 更新时间:2024-10-28 05:21:20
本文介绍了在MyBatis的对象中持久化集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有POJO类:

class Ticket { private int id; private double cost; private Date time; private List<Place> places; // Getters and setters here } class Place { private int row; private int place; // Getters and setters here }

创建一张票和一些地方:

Then I create one ticket and some places:

Ticket ticket = new Ticket(); ticket.setCost(58.7); ticket.setTime(new Date()); Place place1 = new Place(); place1.setRow(1); place1.setPlace(2); ticket.addPlace(place1); Place place2 = new Place(); place2.setRow(3); place2.setPlace(4); ticket.addPlace(place2);

现在我想把它保存到DB:

And now I want to save it to DB:

session.insert("insertTicket", ticket); sessionmit();

在MapperConfig.xml中,我写这行:

In MapperConfig.xml I write this lines:

<insert id="insertTicket" parameterType="Ticket"> INSERT INTO tickets (cost, time) VALUES (#{cost}, #{time}) </insert>

如何以自动模式保存列出地点? MyBatis可以为我保存吗? 或者我需要用 foreach 手动迭代,并手动插入 Place ?

How I can save List places in automatic mode? Does MyBatis can save it for me? Or I need to iterate manually with foreach and insert every Place by hand?

任何帮助。

推荐答案

尽管MyBatis能够支持相反的方向(即在查询期间填充列表,或从连接),则不存在将包含列表插入数据库的自动模式。

Even though MyBatis is able to support the reverse direction (i.e. filling the list during a query with a nested select or from a join), there is no automatic mode that inserts the containing list into the database.

根据此 Google网上论坛讨论,您必须手动插入列表元素。

According to this Google Groups discussion you have to insert the list elements manually.

更多推荐

在MyBatis的对象中持久化集合

本文发布于:2023-05-23 14:45:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1326170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:持久   象中   MyBatis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!