我有 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);
现在我想将它保存到数据库:
session.insert("insertTicket", ticket);
session.commit();
在 MapperConfig.xml 我写了这行:
<insert id="insertTicket" parameterType="Ticket">
INSERT INTO tickets (cost, time) VALUES (#{cost}, #{time})
</insert>
如何在自动模式下保存列表位置?MyBatis 可以为我保存吗?或者我需要使用foreach手动迭代并手动插入每个地方?
谢谢你的帮助。