mybatis下的ResultMap配置一对一以及一对多

一对一:

在数据库里面有这样的一个主外键关系的表:

 

 我需要查找身份证的号码就要知道这个人的姓名(通过一个SQL语句要查到两个实体类里面的信息):

SELECT c.*,p.* FROM idcard c LEFT JOIN person p  ON c.pid=p.pid

 建立两个实体类:

建立映射器的接口:

1 public interface IDCardMapper {
2 
3     //根据身份证号来查询身份信息
4     public IDCardEntity queryCard(String cno);
5 }

映射器xml里面的配置:

 1 <!--mybatis的mapper的根节点-->
 2 <mapper namespace="com.lv.study.mapper.IDCardMapper">
 3 
 4     <resultMap id="cardMap" type="com.lv.study.entity.IDCardEntity">
 5         <id property="cid" column="cid"></id>
 6         <result property="cno" column="cno"></result>
 7 
 8         <!--association是一个实体类里面还有一个实体类
 9             javaType指定我们pe属性的实体类是什么
10         -->
11         <association property="pe" javaType="com.lv.study.entity.PersonEntity">
12             <id property="pid" column="pid"></id>
13             <result property="pname" column="pname"></result>
14 
15         </association>
16     </resultMap>
17 
18     <select id="queryCard" resultMap="cardMap">
19 
20         SELECT c.*,p.* FROM idcard c LEFT JOIN person p  ON c.pid=p.pid where c.cno=#{cno}
21 
22     </select>
23 
24 </mapper>

测试类:

 1 public class TsetOneToOne {
 2     public static void main(String[] args) {
 3 
 4 
 5         try {
 6             String resource="mybatis-config.xml";//这个代表我们编译的根目录
 7             InputStream is = null;//把配置文件弄成流的形式
 8             is = Resources.getResourceAsStream(resource);
 9             SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
10             SqlSession sqlSession=sqlSessionFactory.openSession(true);
11             IDCardMapper mapper=sqlSession.getMapper(IDCardMapper.class);
12 
13             IDCardEntity id = mapper.queryCard("150");
14             System.out.println(id);
15 
16         } catch (IOException e) {
17             e.printStackTrace();
18         }
19 
20 
21 
22     }
23 
24 }

结果:

一对多:当我们要查询一颗数的时候我们想知道他有哪些叶子

数据表:

 树和叶子的实体类:

 1 public class TreeEntity {
 2 
 3     private int tid;
 4     private String tname;
 5 
 6     //在这一棵树里面有多片叶子
 7     private List<LeafEntity> leafs;
 8 
 9     public List<LeafEntity> getLeafs() {
10         return leafs;
11     }
12 
13     public void setLeafs(List<LeafEntity> leafs) {
14         this.leafs = leafs;
15     }
16 
17     public int getTid() {
18         return tid;
19     }
20 
21     public void setTid(int tid) {
22         this.tid = tid;
23     }
24 
25     public String getTname() {
26         return tname;
27     }
28 
29     public void setTname(String tname) {
30         this.tname = tname;
31     }
32 }
 1 public class LeafEntity {
 2     private  int lid;
 3     private  String lname;
 4     private  String ldesc;
 5 
 6 
 7     //一片叶子对应一棵树
 8     private  TreeEntity tree;
 9 
10     public String getLdesc() {
11         return ldesc;
12     }
13 
14     public void setLdesc(String ldesc) {
15         this.ldesc = ldesc;
16     }
17 
18     public TreeEntity getTree() {
19         return tree;
20     }
21 
22     public void setTree(TreeEntity tree) {
23         this.tree = tree;
24     }
25 
26     public int getLid() {
27         return lid;
28     }
29 
30     public void setLid(int lid) {
31         this.lid = lid;
32     }
33 
34     public String getLname() {
35         return lname;
36     }
37 
38     public void setLname(String lname) {
39         this.lname = lname;
40     }
41 }

树的映射器接口:

public interface TreeMapper {

    //根据数的id查询数的信息
    public TreeEntity queryTreeById(int id);

    //查询所有的数的信息
    public List<TreeEntity> queryTreeList();

}

  树的xml配置:

 1 <!--mybatis的mapper的根节点-->
 2 <mapper namespace="com.lv.study.mapper.TreeMapper">
 3 
 4     <!--因为现在找的是树的信息所以需要树的entity-->
 5     <resultMap id="treeMap" type="com.lv.study.entity.TreeEntity">
 6         <id property="tid" column="tid"></id>
 7         <result property="tname" column="tname"></result>
 8 
 9         <!--一对一用的是Javatype  一对多用的是ofType-->
10         <!--一个实体类里面有多个实体类-->
11         <collection property="leafs" ofType="com.lv.study.entity.LeafEntity">
12             <!--不是说实体类里面有什么你就要写上面而是你写什么我们就查什么-->
13             <id property="lid" column="lid"></id>
14             <result property="lname" column="lname"></result>
15 
16             <!--association是一个实体类里面有另外一个实体类-->
17             <association property="tree" javaType="com.lv.study.entity.TreeEntity">
18                 <id property="tid" column="tid"></id>
19                 <result property="tname" column="tname"></result>
20             </association>
21 
22         </collection>
23     </resultMap>
24     <!-- #{id}是传的id  sql语句里面不可以写注释不然会报异常-->
25     <!--查询一个数的实体类-->
26     <select id="queryTreeById" resultMap="treeMap">
27 
28         SELECT t.*,l.* FROM tree t LEFT JOIN leaf l ON t.tid=l.tid WHERE t.tid = #{id}
29     </select>
30 
31     <!--查询所有的数的实体类-->
32     <select id="queryTreeList" resultMap="treeMap">
33         SELECT t.*,l.* FROM tree t LEFT JOIN leaf l ON t.tid=l.tid
34     </select>

测试类:

 1   public static void main(String[] args) {
 2 
 3 
 4         try {
 5             String resource="mybatis-config.xml";//这个代表我们编译的根目录
 6             InputStream is = null;//把配置文件弄成流的形式
 7             is = Resources.getResourceAsStream(resource);
 8             SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
 9             SqlSession sqlSession=sqlSessionFactory.openSession(true);
10             TreeMapper mapper=sqlSession.getMapper(TreeMapper.class);
11 
12             TreeEntity treeEntity = mapper.queryTreeById(2);
13 
14             List<TreeEntity> list = mapper.queryTreeList();
15 
16             System.out.println();
17 
18         } catch (IOException e) {
19             e.printStackTrace();
20         }

结果:

原文地址:https://www.cnblogs.com/dabu/p/13154614.html