【Mybatis】一对一实例

①创建数据库和表,数据库为mytest,表为father和child

 1 DROP TABLE IF EXISTS child;
 2 DROP TABLE IF EXISTS father;
 3 
 4 CREATE TABLE child(
 5     child_id INT PRIMARY KEY auto_increment NOT NULL,
 6     child_name VARCHAR(20)
 7 );
 8 
 9 CREATE TABLE father(
10     father_id INT PRIMARY KEY auto_increment NOT NULL,
11     father_name VARCHAR(20),
12     child_id INT
13 );
14 
15 ALTER TABLE father ADD CONSTRAINT fk_child_id FOREIGN KEY (child_id) REFERENCES child(child_id);
16 
17 INSERT INTO child(child_name) VALUES ("Guo Xiang");
18 INSERT INTO child(child_name) VALUES ("Zhang Wuji");
19 INSERT INTO father(father_name, child_id) VALUES ("Guo Jing", 1);
20 INSERT INTO father(father_name, child_id) VALUES ("Zhang Cuishan", 2);
father&child

创建Java工程,导入相应的jar

创建配置文件conf.xml和数据库配置文件db.properties

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4     
 5 <configuration>
 6 
 7     <properties resource="db.properties"></properties>
 8     <environments default="development">
 9         <environment id="development">
10             <transactionManager type="JDBC"></transactionManager>
11             <dataSource type="POOLED">
12                 <property name="driver" value="${driver}"/>
13                 <property name="url" value="${url}"/>
14                 <property name="username" value="${name}"/>
15                 <property name="password" value="${password}"/>
16             </dataSource>
17         </environment>
18     </environments>
19     
20     
21     <mappers>
22         <mapper resource="org/mybatis/mapping/fatherMapper.xml"/>
23     </mappers>
24 </configuration>
conf.xml
1 driver = com.mysql.jdbc.Driver
2 url = jdbc:mysql://localhost:3306/mytest
3 name = root
4 password = root
db.properties

创建实体类Father和Child

 1 package org.mybatis.domain;
 2 
 3 public class Child {
 4 
 5     private int child_id;
 6     private String child_name;
 7     
 8     public int getChild_id() {
 9         return child_id;
10     }
11     public void setChild_id(int child_id) {
12         this.child_id = child_id;
13     }
14     public String getChild_name() {
15         return child_name;
16     }
17     public void setChild_name(String child_name) {
18         this.child_name = child_name;
19     }
20     
21     @Override
22     public String toString() {
23         return "[Child = child_id:" + child_id + ", child_name:" + child_name + "]";
24     }
25     
26     
27 }
Child
 1 package org.mybatis.domain;
 2 
 3 public class Father {
 4 
 5     private int father_id;
 6     private String father_name;
 7     private Child child;
 8     
 9     public int getFather_id() {
10         return father_id;
11     }
12     public void setFather_id(int father_id) {
13         this.father_id = father_id;
14     }
15     public String getFather_name() {
16         return father_name;
17     }
18     public void setFather_name(String father_name) {
19         this.father_name = father_name;
20     }
21 
22     public Child getChild() {
23         return child;
24     }
25     public void setChild(Child child) {
26         this.child = child;
27     }
28     @Override
29     public String toString() {
30         return "[Father = father_id:" + father_id + ", father_name:" + father_name 
31                 + ", child:" + child + "]";
32     }
33     
34     
35 }
Father

⑤创建sql映射文件fatherMapper.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
 3     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 4     
 5 <mapper namespace="org.mybatis.mapping.fatherMapper">
 6     
 7     <!-- 方式一:嵌套结果 -->
 8 <!--     <select id="getChild" parameterType="int" resultMap="ChildMap"> -->
 9 <!--         select * from child c, father f where c.child_id = f.child_id and f.father_id=#{id} -->
10 <!--     </select> -->
11     
12 <!--     <resultMap type="org.mybatis.domain.Father" id="ChildMap"> -->
13 <!--         <id property="father_id" column="father_id"/> -->
14 <!--         <result property="father_name" column="father_name" /> -->
15 <!--         <association property="child" javaType="org.mybatis.domain.Child"> -->
16 <!--             <id property="child_id" column="child_id"/> -->
17 <!--             <result property="child_name" column="child_name"/> -->
18 <!--         </association> -->
19 <!--     </resultMap> -->
20     
21     <!-- 方式二:嵌套查询 -->
22     <select id="getChild" parameterType="int" resultMap="ChildMap">
23         select * from father where father_id=#{id}
24     </select>
25     
26     <resultMap type="org.mybatis.domain.Father" id="ChildMap">
27         <id property="father_id" column="father_id"/>
28         <result property="father_name" column="father_name" />
29         <collection property="child" column="child_id" select="getChildCollection">
30         </collection>
31     </resultMap>
32     
33     <select id="getChildCollection" resultType="org.mybatis.domain.Child">
34         select * from child where child_id=#{child_id}
35     </select>
36     
37 </mapper>
fatherMapper.xml

⑥向配置文件中注册 fatherMapper.xml 文件【已添加,查看第③步】

⑦测试类

 1 package org.mybatis.app;
 2 
 3 import java.io.InputStream;
 4 
 5 import org.apache.ibatis.session.SqlSession;
 6 import org.apache.ibatis.session.SqlSessionFactory;
 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 8 import org.junit.Before;
 9 import org.junit.Test;
10 import org.mybatis.domain.Father;
11 
12 public class TestOneToOne {
13 
14     SqlSession session;
15     
16     @Before
17     public void beforeLoad() {
18         InputStream inputStream = 
19                 TestOneToOne.class.getClassLoader().getResourceAsStream("conf.xml");
20         SqlSessionFactory sqlSessionFactory = 
21                 new SqlSessionFactoryBuilder().build(inputStream);
22         session = sqlSessionFactory.openSession();
23     }
24     
25     
26     @Test
27     public void testOneToOne() {
28         // 根据fatherID得到child
29         String statement = "org.mybatis.mapping.fatherMapper.getChild";
30         Father father = session.selectOne(statement,2);
31         session.close();
32         System.out.println(father.getFather_name() + "'s child is "
33                 + father.getChild().getChild_name());
34     }
35 }
TestOneToOne

⑧结构图

 fatherMapper.xml中有两种方式,两种方式的结果一样

原文地址:https://www.cnblogs.com/miantiaoandrew/p/8397942.html