1、配置文件

db.properties

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=123456

SqlMapConfig.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6 
 7     <!-- 加载java的配置文件 -->
 8     <properties resource="config/db.properties"/>
 9     
10     <!-- 配置mybatis的环境信息,与spring整合,该信息由spring来管理 -->
11     <environments default="development">
12         <environment id="development">
13             <!-- 配置JDBC事务控制,由mybatis进行管理 -->
14             <transactionManager type="JDBC"></transactionManager>
15             <!-- 配置数据源,采用mybatis连接池 -->
16             <dataSource type="POOLED">
17                 <property name="driver" value="${db.driver}" />
18                 <property name="url" value="${db.url}" />
19                 <property name="username" value="${db.username}" />
20                 <property name="password" value="${db.password}" />
21             </dataSource>
22         </environment>
23     </environments>
24 
25     <!-- 加载映射文件 -->
26     <mappers>
27         <mapper resource="com/xiaostudy/oneTOone/mapper.xml" />
28     </mappers>
29     
30 </configuration>

mapper.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.xiaostudy.oneTOone.Mapper">
 6 
 7     <!-- ========================================================================================== -->
 8     <!-- 一对一映射之resultType,这种方法,数据库的字段要跟domain类的属性一致 -->
 9     <select id="findStudent" resultType="com.xiaostudy.oneTOone.Student" parameterType="int">
10         select * from t_student where sid=#{sid}
11     </select>
12     <!-- ========================================================================================== -->
13     
14    <!-- ========================================================================================== -->
15    <!-- 一对一之resultMap,这种方法,数据库字段和domain类的属性可以不一致 -->
16     <select id="findOrdersAndStudentRstMap" resultMap="OrdersAndStudentRstMap" >
17         select * from t_student s where s.sid=#{sid}
18     </select>
19     
20     <!-- OrdersAndUserRstMap -->
21     <resultMap type="com.xiaostudy.oneTOone.Student" id="OrdersAndStudentRstMap">
22         <id column="sid" property="sid" />
23         <result column="sname" property="sname" />
24     </resultMap>
25     <!-- ========================================================================================== -->
26     
27     <!-- ========================================================================================== -->
28     <!-- 一对一之resultMap2 -->
29     <select id="findOrdersAndSTRstMap" resultMap="OrdersAndSTRstMap">
30         select * from t_student where sid=#{sid}
31     </select>
32 
33     <resultMap type="com.xiaostudy.oneTOone.Student" id="OrdersAndSTRstMap">
34         <id column="sid" property="sid"/>
35         <result column="sname" property="sname"/>
36         <association property="teacher" javaType="com.xiaostudy.oneTOone.Teacher">
37             <id column="tid" property="tid"></id>
38             <result column="tname" property="tname"/>
39         </association>
40     </resultMap>
41    <!-- ========================================================================================== -->
42     
43     <!-- ========================================================================================== -->
44     <!-- 一对一之resultMap3 -->
45     <select id="findOrdersAndTeacher" resultMap="OrdersAndTeacher">
46         select * from t_student s, t_teacher t where t.tid=s.sid and tid=#{tid}
47     </select>
48 
49     <resultMap type="com.xiaostudy.oneTOone.Teacher" id="OrdersAndTeacher">
50         <id column="tid" property="tid"/>
51         <result column="tname" property="tname"/>
52         <association property="student" javaType="com.xiaostudy.oneTOone.Student">
53             <id column="sid" property="sid"></id>
54             <result column="sname" property="sname"/>
55         </association>
56     </resultMap>
57    <!-- ========================================================================================== -->
58     
59 </mapper>

2、domain类之Student.java

 1 package com.xiaostudy.oneTOone;
 2 
 3 public class Student {
 4     private int sid;
 5     private String sname;
 6     private Teacher teacher;
 7 
 8     public int getSid() {
 9         return sid;
10     }
11 
12     public void setSid(int sid) {
13         this.sid = sid;
14     }
15 
16     public String getSname() {
17         return sname;
18     }
19 
20     public void setSname(String sname) {
21         this.sname = sname;
22     }
23 
24     public Teacher getTeacher() {
25         return teacher;
26     }
27 
28     public void setTeacher(Teacher teacher) {
29         this.teacher = teacher;
30     }
31 
32     @Override
33     public String toString() {
34         return "Student [sid=" + sid + ", sname=" + sname + ", teacher=" + teacher + "]";
35     }
36 
37 }

Teacher.java

 1 package com.xiaostudy.oneTOone;
 2 
 3 public class Teacher {
 4     private int tid;
 5     private String tname;
 6     private Student student;
 7 
 8     public int getTid() {
 9         return tid;
10     }
11 
12     public void setTid(int tid) {
13         this.tid = tid;
14     }
15 
16     public String getTname() {
17         return tname;
18     }
19 
20     public void setTname(String tname) {
21         this.tname = tname;
22     }
23 
24     public Student getStudent() {
25         return student;
26     }
27 
28     public void setStudent(Student student) {
29         this.student = student;
30     }
31 
32     @Override
33     public String toString() {
34         return "Teacher [tid=" + tid + ", tname=" + tname + ", student=" + student + "]";
35     }
36 
37 }

3、代理类Mapper.java

 1 package com.xiaostudy.oneTOone;
 2 
 3 import java.util.List;
 4 
 5 public interface Mapper {
 6     // 一对一之resultType
 7     public Student findStudent(int id);
 8 
 9     // 一对一之resultMap
10     public List<Student> findOrdersAndStudentRstMap(Student student);
11 
12     // 一对一之resultMap2
13     public List<Student> findOrdersAndSTRstMap(Student student);
14     
15     // 一对一之resultMap3
16     public List<Teacher> findOrdersAndTeacher(int tid);
17 
18 }

4、测试类

 1 package com.xiaostudy.oneTOone;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.List;
 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 /**
13  * @desc 测试类
14  * @author xiaostudy
15  *
16  */
17 public class MybatisTest {
18 
19     public static void main(String[] args) throws IOException {
20         String resource = "config/SqlMapConfig.xml";
21         InputStream inputStream = Resources.getResourceAsStream(resource);
22 
23         // 创建SqlSessionFactory
24         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
25 
26         // 创建SqlSession
27         SqlSession sqlSession = sqlSessionFactory.openSession();
28 
29         // 获取一个代理dao实现
30         Mapper mapper = sqlSession.getMapper(Mapper.class);
31         
32         Student student = new Student();
33         student.setSid(4);
34         Teacher teacher = new Teacher();
35         teacher.setTid(3);
36 
37         //一对一之resultType
38 //        student = mapper.findStudent(2);
39         //一对一之resultMap
40 //        List<Student> list = mapper.findOrdersAndStudentRstMap(student);
41         //一对一之resultMap2
42 //        List<Student> list = mapper.findOrdersAndSTRstMap(student);
43         //一对一之resultMap3
44         List<Teacher> list = mapper.findOrdersAndTeacher(3);
45 
46         System.out.println(list);
47 //        System.out.println(student);
48 
49         sqlSession.close();
50 
51     }
52     
53 }

5、数据库

t_student表

t_teacher表