分享

MyBatis普通查询与一对一映射查询

 instl 2015-02-26

文档结构:



src/config

ibatisConfiguration.xml:

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration PUBLIC "-////DTD Config 3.0//EN"  
  3. "http:///dtd/mybatis-3-config.dtd">  
  4.   
  5. <configuration>  
  6.     //类型别名设置  
  7.     <typeAliases>  
  8.         <typeAlias alias="Article" type="dao.domain.Article"/>  
  9.         <typeAlias alias="Classtype" type="dao.domain.Classtype"/>  
  10.     </typeAliases>  
  11.       
  12.     //数据源配置  
  13.     <environments default="development">  
  14.         <environment id="development">  
  15.             <transactionManager type="JDBC"/>  
  16.             <dataSource type="POOLED">  
  17.                 <property name="driver" value="com.mysql.jdbc.Driver"/>  
  18.                 <property name="url" value="jdbc:mysql://localhost:3306/manage?characterEncoding=UTF-8"/>  
  19.                 <property name="username" value="root"/>  
  20.                 <property name="password" value=""/>  
  21.             </dataSource>  
  22.         </environment>  
  23.     </environments>  
  24.       
  25.     //映射表  
  26.     <mappers>  
  27.         <mapper resource="config/ArticleMapper.xml" />  
  28.         <mapper resource="config/ClassTypeMapper.xml" />  
  29.     </mappers>  
  30. </configuration>  

ArticleMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"   
  3. "http:///dtd/mybatis-3-mapper.dtd">  
  4.   
  5. <mapper namespace="dao.ArticleDAO" >  
  6.     //sql标签,用于标记常用的字符.   
  7.     <sql id="articleFileds">article_id,title,author,classid</sql>  
  8.       
  9.     //parameterType,参数类型  
  10.     //resultType。返回类型  
  11.     //id。唯一值,调用时使用。  
  12.     <select id="getArticleById" resultType="Article" parameterType="int">  
  13.         //include标签,复用sql标签  
  14.         select <include refid="articleFileds"/>  
  15.         from article  
  16.         where  
  17.             article_id = #{id}  
  18.     </select>  
  19.       
  20.     //mybatis中最关键的标签:resultMap  
  21.     <resultMap id="ArticleAndClassType" type="Article">  
  22.         <id property="article_id" column="article_id" />  
  23.         <result property="title" column="title" />  
  24.         <result property="author" column="author" />  
  25.         <result property="classid" column="classid" />  
  26.           
  27.         //一对一映射的关键:association  
  28.         //select:执行dao.ClassTypeDAO.getClassTypeById方法,并将返回值设置到peoperty.来完成一对一映射  
  29.         <association property="classtype" column="classid" select="dao.ClassTypeDAO.getClassTypeById"/>  
  30.     </resultMap>  
  31.       
  32.     //resultMap:通过此值指向resultMap,可以进行复杂的处理  
  33.     <select id="getArticleAndClassTypeById" resultMap="ArticleAndClassType" parameterType="int">  
  34.         select <include refid="articleFileds"/>  
  35.         from article  
  36.         where  
  37.             article_id = #{id}  
  38.     </select>  
  39. </mapper>  

ClassTypeMapper.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"   
  3. "http:///dtd/mybatis-3-mapper.dtd">  
  4.   
  5. <mapper namespace="dao.ClassTypeDAO" >  
  6.     <sql id="classtypeFileds">classid, classname</sql>  
  7.       
  8.     <select id="getClassTypeById" parameterType="int" resultType="Classtype">  
  9.         select <include refid="classtypeFileds"/>  
  10.         from classtype  
  11.         where  
  12.             classid = #{id}  
  13.     </select>  
  14. </mapper>  

/src/dao/domain

Article.java

  1. package dao.domain;  
  2.   
  3. public class Article {  
  4.     public int article_id;  
  5.     public String title;  
  6.     public String author;  
  7.     public int classid;  
  8.     public Classtype classtype;  
  9.   
  10.     public Classtype getClasstype() {  
  11.         return classtype;  
  12.     }  
  13.     public void setClasstype(Classtype classtype) {  
  14.         this.classtype = classtype;  
  15.     }  
  16.     public int getArticle_id() {  
  17.         return article_id;  
  18.     }  
  19.     public void setArticle_id(int article_id) {  
  20.         this.article_id = article_id;  
  21.     }  
  22.     public String getTitle() {  
  23.         return title;  
  24.     }  
  25.     public void setTitle(String title) {  
  26.         this.title = title;  
  27.     }  
  28.     public String getAuthor() {  
  29.         return author;  
  30.     }  
  31.     public void setAuthor(String author) {  
  32.         this.author = author;  
  33.     }  
  34.     public int getClassid() {  
  35.         return classid;  
  36.     }  
  37.     public void setClassid(int classid) {  
  38.         this.classid = classid;  
  39.     }  
  40.     @Override  
  41.     public String toString() {  
  42.         return "Article [article_id=" + article_id + ", title=" + title  
  43.                 + ", author=" + author + ", classid=" + classid  
  44.                 + ", classtype=" + classtype + "]";  
  45.     }  
  46. }  

Classtype.java

  1. package dao.domain;  
  2.   
  3. public class Classtype {  
  4.     public int classid;  
  5.     public String classname;  
  6.       
  7.     public int getClassid() {  
  8.         return classid;  
  9.     }  
  10.     public void setClassid(int classid) {  
  11.         this.classid = classid;  
  12.     }  
  13.     public String getClassname() {  
  14.         return classname;  
  15.     }  
  16.     public void setClassname(String classname) {  
  17.         this.classname = classname;  
  18.     }  
  19.     @Override  
  20.     public String toString() {  
  21.         return "Classtype [classid=" + classid + ", classname=" + classname  
  22.                 + "]";  
  23.     }  
  24. }  

/src/dao

ArticleDAO.java

  1. package dao;  
  2.   
  3. import java.util.HashMap;  
  4.   
  5. import dao.domain.Article;  
  6.   
  7. public interface ArticleDAO {  
  8.     public Article getArticleById(int id);  
  9.       
  10.     public Article getArticleAndClassTypeById(int id);  
  11. }  

ClassTypeDAO.java

  1. package dao;  
  2.   
  3. import dao.domain.Classtype;  
  4.   
  5. public interface ClassTypeDAO {  
  6.     public Classtype getClassTypeById(int id);  
  7. }  

/src/dao/impl

ArticleDAOImpl.java

  1. package dao.impl;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5. import java.util.HashMap;  
  6.   
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  11.   
  12. import dao.ArticleDAO;  
  13. import dao.domain.Article;  
  14.   
  15. public class ArticleDAOImpl implements ArticleDAO {  
  16.     private static SqlSession session = null;  
  17.     private static ArticleDAO mapper = null;  
  18.       
  19.     static{  
  20.         String resouce = "config/ibatisConfiguration.xml";  
  21.         Reader reader = null;  
  22.         try {  
  23.             reader = Resources.getResourceAsReader(resouce);  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.           
  28.         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);  
  29.         session = factory.openSession();  
  30.         mapper = session.getMapper(ArticleDAO.class);  
  31.           
  32.         try {  
  33.             reader.close();  
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.     }  
  38.   
  39.     @Override  
  40.     public Article getArticleById(int id) {  
  41.         return mapper.getArticleById(id);  
  42.     }  
  43.   
  44.     @Override  
  45.     public Article getArticleAndClassTypeById(int id) {  
  46.         return mapper.getArticleAndClassTypeById(id);  
  47.     }  
  48.   
  49. }  

ClassTypeDAOImpl.java

  1. package dao.impl;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5.   
  6. import org.apache.ibatis.io.Resources;  
  7. import org.apache.ibatis.session.SqlSession;  
  8. import org.apache.ibatis.session.SqlSessionFactory;  
  9. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  10.   
  11. import dao.ClassTypeDAO;  
  12. import dao.domain.Classtype;  
  13.   
  14. public class ClassTypeDAOImple implements ClassTypeDAO {  
  15.     private static SqlSession session = null;  
  16.     private static ClassTypeDAO mapper = null;  
  17.       
  18.     static{  
  19.         String resouce = "config/ibatisConfiguration.xml";  
  20.         Reader reader = null;  
  21.           
  22.         try {  
  23.             reader = Resources.getResourceAsReader(resouce);  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.           
  28.         SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);  
  29.         session = factory.openSession();  
  30.           
  31.         mapper = session.getMapper(ClassTypeDAO.class);  
  32.           
  33.         try {  
  34.             reader.close();  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.       
  40.     @Override  
  41.     public Classtype getClassTypeById(int id) {  
  42.         return mapper.getClassTypeById(id);  
  43.     }  
  44.   
  45. }  

/src/test

test.java

  1. package test;  
  2.   
  3. import dao.ArticleDAO;  
  4. import dao.ClassTypeDAO;  
  5. import dao.impl.ArticleDAOImpl;  
  6. import dao.impl.ClassTypeDAOImple;  
  7.   
  8. public class Test {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         ArticleDAO dao = new ArticleDAOImpl();  
  15.           
  16.         System.out.println(dao.getArticleById(3));  
  17.           
  18.         System.out.println(dao.getArticleAndClassTypeById(2));  
  19.           
  20.         ClassTypeDAO classtypeDAO = new ClassTypeDAOImple();  
  21.           
  22.         System.out.println(classtypeDAO.getClassTypeById(1));  
  23.     }  
  24.   
  25. }  

运行结果:

Article [article_id=3, title=title, author=test, classid=1, classtype=null]
Article [article_id=2, title=title, author=test, classid=1, classtype=Classtype [classid=1, classname=test]]
Classtype [classid=1, classname=test]

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多