Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6658478
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2012-05-08 15:18:36

summary:

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


场景描述:

类:Mail和Attachment类

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

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


POJO: Mail.java
  1. public class Mail implements Serializable {  
  2.     private static final long serialVersionUID = 7427977743354005783L;  
  3.     private Integer id;  
  4.     private String sender;  
  5.     private String subject;  
  6.     private String content;  
  7.     private String fromAddress;  
  8. ...  
  9. getters and setters...  
  10. }  
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
  1. public class Attachment implements Serializable {  
  2.     private static final long serialVersionUID = -1863183546552222728L;  
  3.     private String id;  
  4.     private String mailId;  
  5.     private String name;  
  6.     private String relativePath;  
  7. ...  
  8. getters and setters...  
  9. }  
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:
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <configuration>  
  4.     <properties resource="test/properties/mysql.properties">properties>  
  5.       
  6.     <typeAliases>  
  7.         <typeAlias type="test.model.Mail" alias="Mail"/>  
  8.         <typeAlias type="test.model.Attachment" alias="Attachment"/>        
  9.     typeAliases>  
  10.       
  11.       
  12.     <environments default="development">  
  13.         <environment id="development">  
  14.             <transactionManager type="JDBC" />  
  15.             <dataSource type="UNPOOLED">  
  16.                 <property name="driver" value="${db_driver}" />  
  17.                 <property name="url" value="${db_url}" />  
  18.                 <property name="username" value="${db_user}" />  
  19.                 <property name="password" value="${db_password}"/>  
  20.             dataSource>  
  21.         environment>  
  22.     environments>    
  23.           
  24.     <mappers>  
  25.     <mapper resource="test/data/MailMapper.xml"/>  
  26.     <mapper resource="test/data/AttachmentMapper.xml"/>  
  27.     mappers>      
  28. configuration>  
Mappers MailMapper.xml
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <mapper namespace="test.data.MailMapper">  
  4.     <cache />  
  5.   
  6.     <resultMap type="Mail" id="result_base">  
  7.         <id property="id" column="id_mail" />  
  8.         <result property="sender" column="sender"/>  
  9.         <result property="fromAddress" column="from_address" />  
  10.         <result property="subject" column="subject"/>  
  11.         <result property="content" column="content"/>  
  12.         <result property="sendTime" column="send_time" />  
  13.             ....  
  14.     resultMap>  
  15.       
  16.     <select id="selectLastId" resultType="int">  
  17.          select LAST_INSERT_ID()   
  18.     select>  
  19. mapper>  
的namespace--> insert into note(sender, from_address, subject, content, send_time) values(#{sender}, #{fromAddress}, #{subject}, #{content}, #{sendTime}) select LAST_INSERT_ID() AttachmentMapper.xml
  1. xml version="1.0" encoding="UTF-8"?>  
  2. >  
  3. <mapper namespace="test.data.AttachmentMapper">  
  4.     <cache />  
  5.       
  6.     <resultMap type="Attachment" id="result">  
  7.         <result property="id" column="id_accessory" />  
  8.         <result property="name" column="name" />  
  9.         <result property="relativePath" column="relative_path" />  
  10.         <result property="mailId" column="id_mail" />  
  11.     resultMap>  
  12.       
  13.     <insert id="insert" parameterType="Attachment">  
  14.         insert into attachments(id_mail, name, relative_path) values(#{mailId}, #{name}, #{relativePath})  
  15.     insert>  
  16.       
  17.     <select id="selectByMailId" parameterType="int" resultMap="result">  
  18.         select  id, id_mail, name, relative_path  
  19.          from attachments where id_note = #{id}  
  20.     select>  
  21. mapper>  
insert into attachments(id_mail, name, relative_path) values(#{mailId}, #{name}, #{relativePath})
DAO
AttachmentDAO
  1. public class AttachmentDAO {  
  2.     private SqlSessionFactory sqlSessionFactory;  
  3.       
  4.     public AttachmentDAO(){  
  5.         this.sqlSessionFactory = TestConnectionFactory.getSqlSessionFactory();  
  6.     }  
  7.       
  8.     public void insert(Attachment attachment){  
  9.              SqlSession session = sqlSessionFactory.openSession();  
  10.              AttachmentMapper attachmentMapper = session.getMapper(AttachmentMapper.class);   
  11.              try {  
  12.                 attachmentMapper.insert(attachment);  
  13.                 session.commit();  
  14.              } finally {  
  15.                 session.close();  
  16.              }  
  17.     }  
  18. }  
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
  1. public class MailDAO {  
  2.     private SqlSessionFactory sqlSessionFactory;  
public class MailDAO { private SqlSessionFactory sqlSessionFactory;
  1. public MailDAO(){  
  2.     sqlSessionFactory = TestConnectionFactory.getSqlSessionFactory();  
  3. }   
  4.   
  5. public void insertMailOnly(Mail mail){  
  6.     SqlSession session = sqlSessionFactory.openSession();  
  7.     MailMapper mailMapper = session.getMapper(MailMapper.class);  
  8.     try {  
  9.         mailMapper.insert(mail);         
  10.         session.commit();  
  11.     } finally {  
  12.         session.close();  
  13.     }  
  14. }   
  15. //inset    
  16. public void insertMail(Mail mail){  
  17.     SqlSession session = sqlSessionFactory.openSession();  
  18.     MailMapper mailMapper = session.getMapper(MailMapper.class);  
  19.     AttachmentMapper attachmentMapper = session.getMapper(AttachmentMapper.class);  
  20.       
  21.     try{  
  22.     mailMapper.insert(mail);  
  23.     //这里必须commit,再执行Attachment的插入操作。否则会导致null pointer异常   
  24.     session.commit();  
  25.     //获得最近插入到note表的id   
  26.     int mailId = mailMapper.selectLastId();  
  27.     for(Attachment attach : mail.getAttachments()){  
  28.         attach.setMailId(String.valueOf(mailId));  
  29.         attachmentMapper.insert(attach);  
  30.     }  
  31.     session.commit();  
  32.     }finally{  
  33.         session.close();  
  34.     }  
  35. }  
  36.   
  37. public ArrayList selectAllMails(){        
  38.     ArrayList mailList = null;  
  39.     SqlSession session = sqlSessionFactory.openSession();  
  40.     MailMapper mailMapper = session.getMapper(MailMapper.class);  
  41.     try {             
  42.         mailList = mailMapper.selectAllMails();  
  43.         session.commit();  
  44.     } finally {  
  45.         session.close();  
  46.     }  
  47. urn mailList;  
  48. }   
  49.   
  50. public Mail selectMailById(int i){  
  51.     Mail mail = null;  
  52.     SqlSession session = sqlSessionFactory.openSession();  
  53.     MailMapper mailMapper = session.getMapper(MailMapper.class);  
  54.     try {             
  55.         mail = mailMapper.selectById(i);  
  56.         session.commit();  
  57.     } finally {  
  58.         session.close();  
  59.     }  
  60. urn mail;  
  61. }  
  62.   
  63. public int selectLastId(){  
  64.     int id = -1;  
  65.     SqlSession session = sqlSessionFactory.openSession();  
  66.     MailMapper mailMapper = session.getMapper(MailMapper.class);  
  67.     try {             
  68.         id = mailMapper.selectLastId();  
  69.         session.commit();  
  70.     } finally {  
  71.         session.close();  
  72.     }  
  73. urn id;  
  74. }  
阅读(9585) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~