(四)MyBatis关系映射

第一节:一对一关系实现

需要实现一对一的关系,首先我们有两张表,t-addree和t_student。

1 CREATE TABLE `t_address` (
2   `id` int(11) NOT NULL AUTO_INCREMENT,
3   `sheng` varchar(20) DEFAULT NULL,
4   `shi` varchar(20) DEFAULT NULL,
5   `qu` varchar(20) DEFAULT NULL,
6   PRIMARY KEY (`id`)
7 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
1 insert  into `t_address`(`id`,`sheng`,`shi`,`qu`) values (1,'江苏省','苏州市','姑苏区'),(2,'江苏省','南京市','鼓楼区');
1 CREATE TABLE `t_student` (
2   `id` int(11) NOT NULL AUTO_INCREMENT,
3   `name` varchar(20) DEFAULT NULL,
4   `age` int(11) DEFAULT NULL,
5   `addressId` int(11) DEFAULT NULL,
6   PRIMARY KEY (`id`)
7 ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
1 insert  into `t_student`(`id`,`name`,`age`,`addressId`) values (1,'张三',10,1),(2,'李四',11,2),(3,'李四',11,2),(4,'李四',11,2),(5,'李四',11,2),(6,'李四',11,2),(7,'李四',11,2);

 

然后写model层

Address.java

 1 package com.javaxk.model;
 2 
 3 public class Address {
 4 
 5     private Integer id;
 6     private String sheng;
 7     private String shi;
 8     private String qu;
 9     
10     public Integer getId() {
11         return id;
12     }
13     public void setId(Integer id) {
14         this.id = id;
15     }
16     public String getSheng() {
17         return sheng;
18     }
19     public void setSheng(String sheng) {
20         this.sheng = sheng;
21     }
22     public String getShi() {
23         return shi;
24     }
25     public void setShi(String shi) {
26         this.shi = shi;
27     }
28     public String getQu() {
29         return qu;
30     }
31     public void setQu(String qu) {
32         this.qu = qu;
33     }
34     @Override
35     public String toString() {
36         return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
37                 + ", qu=" + qu + "]";
38     }
39     
40     
41 }

 

Student.java

 1 package com.javaxk.model;
 2 
 3 public class Student {
 4 
 5     private Integer id;
 6     private String name;
 7     private Integer age;
 8     private Address address;
 9     
10     public Student() {
11         super();
12         // TODO Auto-generated constructor stub
13     }
14     
15     public Student(Integer id, String name, Integer age) {
16         super();
17         this.id = id;
18         this.name = name;
19         this.age = age;
20     }
21 
22     public Student(String name, Integer age) {
23         super();
24         this.name = name;
25         this.age = age;
26     }
27 
28     public Integer getId() {
29         return id;
30     }
31     public void setId(Integer id) {
32         this.id = id;
33     }
34     public String getName() {
35         return name;
36     }
37     public void setName(String name) {
38         this.name = name;
39     }
40     public Integer getAge() {
41         return age;
42     }
43     public void setAge(Integer age) {
44         this.age = age;
45     }
46     public Address getAddress() {
47         return address;
48     }
49 
50 
51     public void setAddress(Address address) {
52         this.address = address;
53     }
54 
55 
56 
57 
58     @Override
59     public String toString() {
60         return "Student [id=" + id + ", name=" + name + ", age=" + age
61                 + ", address=" + address + "]";
62     }
63 
64 
65 
66     
67     
68     
69 }

 

mappers映射类

AddressMapper.java

1 package com.javaxk.mappers;
2 
3 import com.javaxk.model.Address;
4 
5 public interface AddressMapper {
6 
7     public Address findById(Integer id);
8 
9 }

StudentMapper.java

 1 package com.javaxk.mappers;
 2 
 3 import java.util.List;
 4 
 5 import com.javaxk.model.Student;
 6 
 7 public interface StudentMapper {
 8 
 9     
10     public Student findStudentWithAddress(Integer id);
11 }

 

 

主程序运行类

StudentTest3.java

 1 package com.javaxk.service;
 2 
 3 import java.util.List;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.log4j.Logger;
 7 import org.junit.After;
 8 import org.junit.Before;
 9 import org.junit.Test;
10 
11 import com.javaxk.mappers.StudentMapper;
12 import com.javaxk.model.Student;
13 import com.javaxk.util.SqlSessionFactoryUtil;
14 
15 public class StudentTest3 {
16 
17     private static Logger logger=Logger.getLogger(StudentTest.class);
18     private SqlSession sqlSession=null;
19     private StudentMapper studentMapper=null;
20     
21     /**
22      * 测试方法前调用
23      * @throws Exception
24      */
25     @Before
26     public void setUp() throws Exception {
27         sqlSession=SqlSessionFactoryUtil.openSession();
28         studentMapper=sqlSession.getMapper(StudentMapper.class);
29     }
30 
31     /**
32      * 测试方法后调用
33      * @throws Exception
34      */
35     @After
36     public void tearDown() throws Exception {
37         sqlSession.close();
38     }
39 
40     @Test
41     public void testFindStudentWithAddress() {
42         logger.info("查询学生(带地址)");
43         Student student=studentMapper.findStudentWithAddress(1);
44         System.out.println(student);
45     }
46     
47 }

 

 

 Mapper映射文件的不同写法。。。

AddressMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.AddressMapper">
 6 
 7     <resultMap type="Address" id="AddressResult">
 8         <result property="id" column="id"/>
 9         <result property="sheng" column="sheng"/>
10         <result property="shi" column="shi"/>
11         <result property="qu" column="qu"/>
12     </resultMap>
13     
14     <select id="findById" parameterType="Integer" resultType="Address">
15         select * from t_address where id=#{id}
16     </select>
17 
18 </mapper> 

 

 

第一种方式:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.StudentMapper">
 6 
 7     <resultMap type="Student" id="StudentResult">
 8         <id property="id" column="id"/>
 9         <result property="name" column="name"/>
10         <result property="age" column="age"/>
11         
12         <result property="address.id" column="addressId"/>
13         <result property="address.sheng" column="sheng"/>
14         <result property="address.shi" column="shi"/>
15         <result property="address.qu" column="qu"/>
16     </resultMap>
17     <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
18         select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
19     </select>
20 </mapper> 

 

 

第二种方式:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.StudentMapper">
 6 <resultMap type="Address" id="AddressResult">
 7         <result property="id" column="id"/>
 8         <result property="sheng" column="sheng"/>
 9         <result property="shi" column="shi"/>
10         <result property="qu" column="qu"/>
11 </resultMap>
12     
13 <resultMap type="Student" id="StudentResult">
14         <id property="id" column="id"/>
15         <result property="name" column="name"/>
16         <result property="age" column="age"/>
17         <association property="address" resultMap="AddressResult"/>
18 </resultMap>
19 
20  
21 <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
22         select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
23     </select>
24 </mapper> 

 

 

第三种方式:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.StudentMapper">
 6      <resultMap type="Student" id="StudentResult">
 7         <id property="id" column="id"/>
 8         <result property="name" column="name"/>
 9         <result property="age" column="age"/>
10         <association property="address" javaType="Address">
11             <result property="id" column="id"/>
12             <result property="sheng" column="sheng"/>
13             <result property="shi" column="shi"/>
14             <result property="qu" column="qu"/>
15         </association>
16     </resultMap> 
17 
18     <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
19         select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
20     </select>
21 
22 </mapper> 

 

 

第四种方式:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.StudentMapper">
 6     <resultMap type="Student" id="StudentResult">
 7         <id property="id" column="id"/>
 8         <result property="name" column="name"/>
 9         <result property="age" column="age"/>
10         <association property="address" column="id" select="com.javaxk.mappers.AddressMapper.findById"></association>
11     </resultMap>
12     
13     <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
14         select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
15     </select>
16 
17 </mapper> 

 

 

 

 

 

 

第二节:一对多关系实现

StudentMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.StudentMapper">
 6 
 7     <resultMap type="Student" id="StudentResult">
 8         <id property="id" column="id"/>
 9         <result property="name" column="name"/>
10         <result property="age" column="age"/>
11         <association property="address" column="addressId" select="com.javaxk.mappers.AddressMapper.findById"></association>
12         <association property="grade" column="gradeId" select="com.javaxk.mappers.GradeMapper.findById"></association>
13     </resultMap>
14     
15     <select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">
16         select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}
17     </select>
18 
19 </mapper> 

 

 

AddressMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.AddressMapper">
 6 
 7     <resultMap type="Address" id="AddressResult">
 8         <result property="id" column="id"/>
 9         <result property="sheng" column="sheng"/>
10         <result property="shi" column="shi"/>
11         <result property="qu" column="qu"/>
12     </resultMap>
13     
14     <select id="findById" parameterType="Integer" resultType="Address">
15         select * from t_address where id=#{id}
16     </select>
17 
18 </mapper> 

 

 

GradeMapper.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="com.javaxk.mappers.GradeMapper">
 6 
 7     <resultMap type="Grade" id="GradeResult">
 8         <result property="id" column="id"/>
 9         <result property="gradeName" column="gradeName"/>
10         
11     </resultMap>
12     
13     <select id="findById" parameterType="Integer" resultType="Grade">
14         select * from t_grade where id=#{id}
15     </select>
16 
17 </mapper> 

 

 

测试主类:

1     
2     @Test
3     public void testFindStudentWithGrade(){
4         logger.info("查询学生(带年级)");
5         Student student=studentMapper.findStudentWithAddress(1);
6         System.out.println(student);
7     }

 

posted @ 2017-06-26 19:02  kika  阅读(198)  评论(0编辑  收藏  举报