MyBatis中的多表操作情形一:一對一(方式1:一條sql語句查詢,MyBatis3.0可以用association和collection標籤)
阿新 • • 發佈:2018-12-09
- MyBatis支援多表操作,即可以將資料庫中多表的關係對映到物件之間的關係中
- 表與表之間的關係可以有:一對一,一對多,多對多
- 關係一演示案例:人和身份證是一對一的,分別建兩個表person和card,其中person的cardid欄位外來鍵關聯card的id,當需求是查詢某人姓名、年齡及身份證號時就需要外連線,查詢結果在MyBatis中是如何對映到物件中的呢?
- 備註:未用MyBatis框架前,我們通常將多表查詢的結果放在dto或vo包中的結果類裡進行封裝,有利有弊,程式碼多,而且碰到一對多的關係時會出現大量資料不可避免地重複封裝。 person表 card表 相應的兩個實體類
package com.yicha.entity;
//Card.java
public class Card {
private int id;
private String cardNum;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum (String cardNum) {
this.cardNum = cardNum;
}
}
package com.yicha.entity;
//Person.java
public class Person {
private int id;
private String name;
private int age;
private Card card;
public int getId() {
return id;
}
public void setId(int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Card getCard() {
return card;
}
public void setCard(Card card) {
this.card = card;
}
}
Person.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD SQL Map 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.yicha.entity.Person">
<!-- 對結果自定義對映方式 -->
<resultMap type="Person" id="myMap">
<result property="name" column="name"/>
<result property="age" column="age"/>
<!-- 一對一用association標籤 -->
<!-- 這種對映方式由於是執行一條sql語句findStudent,無法懶載入(也就是按需載入) -->
<association property="card" javaType="Card">
<result property="cardNum" column="cardnum"/>
</association>
</resultMap>
<!-- 對映結果為自定義Map -->
<select id="findPerson" resultMap="myMap">
SELECT a.name,a.age,b.cardnum
FROM person a
LEFT JOIN card b
ON a.cardid=b.id
WHERE a.id=1
</select>
</mapper>
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<!-- 取別名 -->
<typeAliases>
<typeAlias type="com.yicha.entity.Person" alias="Person"/>
<typeAlias type="com.yicha.entity.Card" alias="Card"/>
</typeAliases>
<!-- 配環境 -->
<environments default="e1">
<environment id="e1">
<!-- 先配事務 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配資料來源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root1205"/>
</dataSource>
</environment>
</environments>
<!-- 實體類對映檔案 -->
<mappers>
<mapper resource="com/yicha/entity/Person.xml"/>
</mappers>
</configuration>
測試類
package com.yicha.test;
//Demo1.java
import java.io.IOException;
import java.io.Reader;
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 Demo1 {
public static void main(String[] args) {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = null;
SqlSession session = null;
try {
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
factory = builder.build(reader);
session = factory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
session.selectOne("findPerson");
}
}
結果