mybatis學習七 單多表關聯

我的以爲這個功能其實能夠不用的,徹底能夠用鏈接操做,本身的邏輯清晰就行了 ###1.建立實體類java

public class Ticket {
	private Integer ticketId;  
	private String ticketAddress;  
	private Integer ticketPrice;  
	private Integer ticketCId;  
	private Customer customer;//使用一個customer來表示顧客  
    ....

public class Customer {
	private Integer customerId;
	private String customerName;
	private Integer customerTel;
	private List<Ticket> tickets;//使用一個List來表示車票
    ....

###2.編寫xml文件mysql

<?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="com.mucfc.model.CustomerMapper">
 <!-- 定義數據庫字段與實體對象的映射關係 -->
	<resultMap type="Customer" id="customerBean">
		<id column="customerId" property="customerId"/>
		<result column="customerName" property="customerName"/>
		<result column="customerTel" property="customerTel"/>		
		<!-- 一對多的關係 -->
		<!-- property: 指的是集合屬性的值, ofType:指的是集合中元素的類型 -->
		<collection property="tickets" ofType="Ticket">
			<id column="ticketId" property="ticketId"/>
			<result column="ticketAddress" property="ticketAddress"/>
			<result column="ticketPrice" property="ticketPrice"/>
			<result column="ticketCId" property="ticketCId"/>
		</collection>
	</resultMap>	
	
	<!-- 根據id查詢Person, 關聯將Orders查詢出來 -->
	<select id="selectCustomerByName" parameterType="string" resultMap="customerBean">
		select c.*,t.* from t_customer c,t_ticket t  where  c.customerId=t.ticketCId and c.customerName =#{customerName};
	</select>
	
 </mapper>
<?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="com.mucfc.model.TicketMapper">
	<!-- 定義數據庫字段與實體對象的映射關係  -->
	<resultMap type="Ticket" id="ticketBean">
		<id column="ticketId" property="ticketId" />
		<result column="ticketAddress" property="ticketAddress" />
		<result column="ticketPrice" property="ticketPrice" />
		<result column="ticketCId" property="ticketCId" />
		<!-- 一對一的關係 -->
		<!-- property: 指的是屬性的值, javaType:指的是元素的類型 -->
		<association property="customer" javaType="Customer">
			<id column="customerId" property="customerId" />
			<result column="customerName" property="customerName" />
			<result column="customerTel" property="customerTel" />
		</association>
	</resultMap>
	<!-- 根據id查詢ticket, 關聯將Customer查詢出來 -->
	<select id="selectTicketById" parameterType="int" resultMap="ticketBean">
		select c.*,t.* from t_customer c,t_ticket t where
		c.customerId=t.ticketCId and t.ticketId =#{ticketId}
	</select>
</mapper>

###3.編寫總的xml文件 ``xmlsql

<?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">apache

<!-- 這是根標籤 -->session

<configuration> <!-- 設置別名 --> <typeAliases> <typeAlias alias="Customer" type="com.mucfc.model.Customer"/> <typeAlias alias="Ticket" type="com.mucfc.model.Ticket" /> </typeAliases>mybatis

<!-- 配置數據源相關的信息  -->
<environments default="development">
	<environment id="development">
		<transactionManager type="JDBC" />
		<dataSource type="POOLED">
		<property name="driver" value="com.mysql.jdbc.Driver"/>
		 <property name="url" value="jdbc:mysql://192.168.236.131:3306/test?characterEncoding=UTF-8"/> 
		<property name="username" value="root"/> 
		<property name="password" value="1994713"/>  
		</dataSource>
	</environment>
</environments>

<!-- 列出映射文件 -->
<mappers>
	<mapper resource="com/mucfc/model/Customer.xml" />
	<mapper resource="com/mucfc/model/Ticket.xml" /> 
</mappers>

</configuration> ``` ###4.編寫測試類 ```java package com.hsm;app

import java.io.Reader; import java.util.List;測試

import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder;ui

import com.mucfc.model.Customer; import com.mucfc.model.Ticket;

public class Test { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; static { try { reader = Resources.getResourceAsReader("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } }

/*
 * 一對一關聯查詢
 */
public static void selectTicketById(int id) {
	SqlSession session = null;
	try {
		session = sqlSessionFactory.openSession();
		Ticket ticket = (Ticket) session.selectOne(
				"com.mucfc.model.TicketMapper.selectTicketById", id);
		if (ticket == null)
			System.out.println("null");
		else {
			System.out.println(ticket);
			System.out.println(ticket.getCustomer());
		}
	} finally {
		session.close();
	}
}

/*
 * 一對多關聯查詢
 */
public static void selectCustomerByName(String string) {
	SqlSession session = null;
	try {
		session = sqlSessionFactory.openSession();
		Customer customer = (Customer) session
				.selectOne(
						"com.mucfc.model.CustomerMapper.selectCustomerByName",
						string);
		if (customer == null)
			System.out.println("null");
		else {
			System.out.println(customer);
			List<Ticket> tickets = customer.getTickets();
			for (Ticket ticket : tickets) {
				System.out.println(ticket);
			}
		}
	} finally {
		session.close();
	}
}

public static void main(String[] args) {
	System.out.println("==============一對一查詢,根據車票來查顧客===============");
	selectTicketById(1);
	System.out.println("==============多對一查詢,根據顧客來查車票===============");
	selectCustomerByName("小王");

}

}

相關文章
相關標籤/搜索