与mybatis的对象中的持续收集
-
28-09-2019 - |
题
我有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
}
然后我创建一张票和一些地方:
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:
session.insert("insertTicket", ticket);
session.commit();
在mapperconfig.xml中,我写这一行:
<insert id="insertTicket" parameterType="Ticket">
INSERT INTO tickets (cost, time) VALUES (#{cost}, #{time})
</insert>
我如何保存 列出位置 在自动模式下? Mybatis可以为我保存吗?或者我需要手动迭代 foreach 并插入每个 地方 用手?
谢谢你的帮助。
解决方案
即使Mybatis能够支持反向方向(即在使用嵌套选择或从联接中填写查询期间填写列表),也没有自动模式将包含列表插入数据库中。
根据这个 Google组讨论 您必须手动插入列表元素。
不隶属于 StackOverflow