MyBatis筆記四之一對一關聯對映
阿新 • • 發佈:2018-12-12
一對一關係推薦使用唯一主外來鍵關聯,即兩個表使用外來鍵關聯,由於是一對一關聯,因此還需要給外來鍵增加unique唯一約束。
下面我們以每個人只有一張身份證,一個身份證對應一個人為例。
1、在資料庫mybatis中新建兩張表,並插入一條資料,表結構如下:
表資料如下:
2、在mybatis專案中的pojo目錄下新建與兩個表相對應的pojo物件。
package pojo; import java.io.Serializable; public class Card implements Serializable{ private Integer id; private String code; public Card(){} public Card(String code){ this.code = code; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
package pojo; import java.io.Serializable; public class Person implements Serializable{ private Integer id; private String name; private Card card; public Person(){} public Person(String name,Card card){ this.name = name; this.card = card; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } }
3、在mapping目錄下新建兩個物件的對映檔案
mapping/CardMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapping.CardMapper"> <select id="selectCardById" parameterType="int" resultType="pojo.Card"> select*from card where id = #{id} </select> </mapper>
mapping/PersonMapper.xml,在這裡使用了<association/>元素對映一對一的關聯關係,select屬性表示會使用column屬性的card_id值作為引數執行CardMapper中定義的selectCardById查詢對的Card資料,查詢出的資料將封裝到property表示的card物件當中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapping.PersonMapper">
<!-- 設定返回的物件 -->
<resultMap type="pojo.Person" id="personResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<!-- 一對一關聯 select的值對應CardMapper.xml的名稱空間+select的id-->
<association property="card" column="card_id" javaType="pojo.Card" select="mapping.CardMapper.selectCardById"/>
</resultMap>
<!-- 查詢person -->
<select id="selectPersonById" parameterType="int" resultMap="personResult">
select*from person where id = #{id}
</select>
</mapper>
4、編寫mapper介面,與mapper對映檔案在同級目錄下。注意,這裡的類名需與配置檔案中的namespace一致,而方法名和引數也必須與xml檔案中<select/>標籤的id屬性和parameterType屬性一致,
package mapping;
import pojo.Person;
public interface PersonMapper {
Person selectPersonById(Integer id);
}
5、在mybatis-config配置檔案下宣告剛剛建立的兩個對映檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 指定所有日誌的具體實現 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!-- 環境配置 -->
<environments default="mysql">
<environment id="mysql">
<!-- 指定事務管理型別 -->
<transactionManager type="JDBC" />
<!-- 配置資料來源,POOLED是JDBC連線物件的資料來源連線池的實現 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!-- mappers告訴mybatis找持久化類的對映檔案 -->
<mappers>
<mapper resource="mapping/UserMapper.xml"/>
<mapper resource="mapping/PersonMapper.xml"/>
<mapper resource="mapping/CardMapper.xml"/>
</mappers>
</configuration>
6、編寫一個工廠類用於讀取mybatis-config配置檔案並生成對應的SqlSession
package factory;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyFactory {
private static SqlSessionFactory ssf = null;
static{
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
ssf = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//獲取SqlSession的靜態方法
public static SqlSession getSqlSession(){
return ssf.openSession();
}
//獲取SqlSessionFactory的靜態方法
public static SqlSessionFactory getSqlSessionFactory(){
return ssf;
}
}
7、編寫一對一的測試類
package reflectTest;
import org.apache.ibatis.session.SqlSession;
import factory.MyFactory;
import mapping.PersonMapper;
import pojo.Person;
public class OneToOne {
public static void main(String[] args) {
// TODO Auto-generated method stub
SqlSession session = MyFactory.getSqlSession();
PersonMapper pm = session.getMapper(PersonMapper.class); //通過代理物件訪問
Person p = pm.selectPersonById(1); //查詢id為1的person資訊
System.out.println(p.getId()+":"+p.getName());
System.out.println(p.getCard().getId()+":"+p.getCard().getCode());
session.commit();
session.close();
}
}
執行結果:
注意:mapper的介面代理物件一定要與對映檔案處於同一級目錄,否則會報錯