MyBatis 3中实现一对多的插入和查询

summary:

MyBatis3中实现一对多的查询比较简单,可以自动完成。但插入操作要复杂一些,需要相关的DAO配合完成,这点不如Hibernate。


场景描述:

类:Mail和Attachment类

关系描述:一封邮件(Mail)可以有0个或多个附件(Attachment),附件(Attachment)仅对应一封邮件。

表格:mail表(主键:id_mail)和attachment表(外键:id_mail)。


POJO:

Mail.java

public class Mail implements Serializable { private static final long serialVersionUID = 7427977743354005783L; private Integer id; private String sender; private String subject; private String content; private String fromAddress; ... getters and setters... }

Attachment.java

public class Attachment implements Serializable { private static final long serialVersionUID = -1863183546552222728L; private String id; private String mailId; private String name; private String relativePath; ... getters and setters... }

SqlMapConfig:

Mappers

MailMapper.xml

.... insert into note(sender, from_address, subject, content, send_time) values(#{sender}, #{fromAddress}, #{subject}, #{content}, #{sendTime}) select LAST_INSERT_ID()

AttachmentMapper.xml

insert into attachments(id_mail, name, relative_path) values(#{mailId}, #{name}, #{relativePath})

DAO

AttachmentDAO

public class AttachmentDAO { private SqlSessionFactory sqlSessionFactory; public AttachmentDAO(){ this.sqlSessionFactory = TestConnectionFactory.getSqlSessionFactory(); } public void insert(Attachment attachment){ SqlSession session = sqlSessionFactory.openSession(); AttachmentMapper attachmentMapper = session.getMapper(AttachmentMapper.class); try { attachmentMapper.insert(attachment); session.commit(); } finally { session.close(); } } }

MailDAO

public class MailDAO { private SqlSessionFactory sqlSessionFactory; public MailDAO(){ sqlSessionFactory = TestConnectionFactory.getSqlSessionFactory(); } public void insertMailOnly(Mail mail){ SqlSession session = sqlSessionFactory.openSession(); MailMapper mailMapper = session.getMapper(MailMapper.class); try { mailMapper.insert(mail); session.commit(); } finally { session.close(); } } //inset public void insertMail(Mail mail){ SqlSession session = sqlSessionFactory.openSession(); MailMapper mailMapper = session.getMapper(MailMapper.class); AttachmentMapper attachmentMapper = session.getMapper(AttachmentMapper.class); try{ mailMapper.insert(mail); //这里必须commit,再执行Attachment的插入操作。否则会导致null pointer异常 session.commit(); //获得最近插入到note表的id int mailId = mailMapper.selectLastId(); for(Attachment attach : mail.getAttachments()){ attach.setMailId(String.valueOf(mailId)); attachmentMapper.insert(attach); } session.commit(); }finally{ session.close(); } } public ArrayList selectAllMails(){ ArrayList mailList = null; SqlSession session = sqlSessionFactory.openSession(); MailMapper mailMapper = session.getMapper(MailMapper.class); try { mailList = mailMapper.selectAllMails(); session.commit(); } finally { session.close(); } return mailList; } public Mail selectMailById(int i){ Mail mail = null; SqlSession session = sqlSessionFactory.openSession(); MailMapper mailMapper = session.getMapper(MailMapper.class); try { mail = mailMapper.selectById(i); session.commit(); } finally { session.close(); } return mail; } public int selectLastId(){ int id = -1; SqlSession session = sqlSessionFactory.openSession(); MailMapper mailMapper = session.getMapper(MailMapper.class); try { id = mailMapper.selectLastId(); session.commit(); } finally { session.close(); } return id; } }


你可能感兴趣的:(MyBatis 3中实现一对多的插入和查询)