1. 程式人生 > >SSM學習筆記(一)——多表聯查

SSM學習筆記(一)——多表聯查

在剛剛接觸SSM的時候,最初遇到的難題便是多表聯查的問題。mybatis由於他的特性一般情況下對映的結果與pojo(entity)實體類裡面的屬性都是一一對應的。所以如果是多張表聯合查詢然後返回結果集的話,由於存在pojo裡面的屬性不全的問題無法完成對映,因為這個問題糾結了2天。

找了許多的網站和例子也沒有找到解決的方案,在一次偶然的嘗試中發現瞭解決的方法,因此在這裡做下筆記,作為留念。

需求:從Seorder,  Seorderentry,  Icitemcore獲取資料顯示到頁面上

要求顯示的資料

Seorder 表的 FBillNo

Seorderentry 表的 FMapNumber,FEntrySelfS0164

Icitemcore 表的 FNumber FShortNumbe FName

首先寫sql語句

SeorderKDao.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="cn.gsp.dao.SeorderKDao">
	<sql id="seorderwhere">
		<where>
			<if test="FBillNo!=null and FBillNo!=''">
				and t1.FBillNo=#{FBillNo}
			</if>
			<if test="FMapNumber!=null and FMapNumber!=''">
				and t2.FMapNumber=#{FMapNumber}
			</if>
			<if test="FEntrySelfS0164!=null and FEntrySelfS0164!=''">
				and t2.FEntrySelfS0164=#{FEntrySelfS0164}
			</if>
		</where>
	</sql>
	<select id="getByFBillNo" resultType="cn.gsp.pojo.SeorderK"  >
		SELECT
		t1.FBillNo,
		t2.FMapNumber,
		t2.FEntrySelfS0164,
		t3.FNumber,
		t3.FShortNumber,
		t3.FName
		FROM
		SEOrder t1
		inner join SEOrderEntry t2 on t1.FInterID=t2.FInterID
		inner join t_ICItemCore t3 on t2.FItemID=t3.FItemID
		<include refid="seorderwhere"></include>
	</select>
</mapper>
然後根據xml寫出對應的dao介面,dao中的方法名字與xml中select的id要相同

SeorderKDao.java

package cn.gsp.dao;

import java.util.List;

import cn.gsp.pojo.SeorderK;

public interface SeorderKDao {
		/**
		 * 通過查詢條件來查詢資料
		 */
	public List<SeorderK> getByFBillNo()throws Exception;

}

之後是重點,實體類

這裡要寫4個實體類,3個是資料庫中對應的表的實體類,第四個則是對映結果集的實體類,這點非常重要。

1.Seorder.java

package cn.gsp.pojo;

public class Seorder {
		private String FBillNo;
		private int FInterID;
		private Seorderentry seorderentry;
		private Icitemcore icitemcore;
		
		public Icitemcore getIcitemcore() {
			return icitemcore;
		}
		public void setIcitemcore(Icitemcore icitemcore) {
			this.icitemcore = icitemcore;
		}
		public Seorderentry getSeorderentry() {
			return seorderentry;
		}
		public void setSeorderentry(Seorderentry seorderentry) {
			this.seorderentry = seorderentry;
		}
		public String getFBillNo() {
			return FBillNo;
		}
		public void setFBillNo(String fBillNo) {
			FBillNo = fBillNo;
		}
		public int getFInterID() {
			return FInterID;
		}
		public void setFInterID(int fInterID) {
			FInterID = fInterID;
		}
		@Override
		public String toString() {
			return "Seorder [FBillNo=" + FBillNo + ", FInterID=" + FInterID + ", seorderentry=" + seorderentry + "]";
		}
		

		
}

2.Seorderentry.java
package cn.gsp.pojo;

public class Seorderentry { 
		private String FMapNumber;
		private String FEntrySelfS0164;
		private int FInterID;
		private int FItemID;
		private Icitemcore icitemcore;
		private Seorder seorder;
		
		public Seorder getSeorder() {
			return seorder;
		}

		public void setSeorder(Seorder seorder) {
			this.seorder = seorder;
		}

		public Icitemcore getIcitemcore() {
			return icitemcore;
		}

		public void setIcitemcore(Icitemcore icitemcore) {
			this.icitemcore = icitemcore;
		}

		public String getFMapNumber() {
			return FMapNumber;
		}

		public void setFMapNumber(String fMapNumber) {
			FMapNumber = fMapNumber;
		}

		public String getFEntrySelfS0164() {
			return FEntrySelfS0164;
		}

		public void setFEntrySelfS0164(String fNewNo) {
			FEntrySelfS0164 = fNewNo;
		}

		public int getFInterID() {
			return FInterID;
		}

		public void setFInterID(int fInterID) {
			FInterID = fInterID;
		}

		public int getFItemID() {
			return FItemID;
		}

		public void setFItemID(int fItemID) {
			FItemID = fItemID;
		}

		@Override
		public String toString() {
			return "Seorderentry [FMapNumber=" + FMapNumber + ", FEntrySelfS0164=" + FEntrySelfS0164 + ", FInterID="
					+ FInterID + ", FItemID=" + FItemID + ", icitemcore=" + icitemcore + "]";
		}

	
		
		
}

3.Icitemcore.java
package cn.gsp.pojo;

public class Icitemcore {
		private String FNumber;
		private String FShortNumber;
		private String FName;
		private int FItemID;
		private Seorderentry seorderentry;
		private Seorder seorder;


	

		public Seorder getSeorder() {
			return seorder;
		}

		public void setSeorder(Seorder seorder) {
			this.seorder = seorder;
		}

		public Seorderentry getSeorderentry() {
			return seorderentry;
		}

		public void setSeorderentry(Seorderentry seorderentry) {
			this.seorderentry = seorderentry;
		}

		public String getFNumber() {
			return FNumber;
		}

		public void setFNumber(String fNumber) {
			FNumber = fNumber;
		}

		public String getFShortNumber() {
			return FShortNumber;
		}

		public void setFShortNumber(String fShortNumber) {
			FShortNumber = fShortNumber;
		}

		public String getFName() {
			return FName;
		}

		public void setFName(String fName) {
			FName = fName;
		}

		public int getFItemID() {
			return FItemID;
		}

		public void setFItemID(int fItemID) {
			FItemID = fItemID;
		}

		@Override
		public String toString() {
			return "Icitemcore [FNumber=" + FNumber + ", FShortNumber=" + FShortNumber + ", FName=" + FName
					+ ", FItemID=" + FItemID + "]";
		}
		
		
		
}
4.SeorderK.java   這個正是對映結果的實體類
package cn.gsp.pojo;


public class SeorderK extends Seorder{
	private String FMapNumber;
	private String FEntrySelfS0164;
	private String FNumber;
	private String FShortNumber;
	private String FName;
	public String getFMapNumber() {
		return FMapNumber;
	}
	public void setFMapNumber(String fMapNumber) {
		FMapNumber = fMapNumber;
	}
	public String getFEntrySelfS0164() {
		return FEntrySelfS0164;
	}
	public void setFEntrySelfS0164(String fEntrySelfS0164) {
		FEntrySelfS0164 = fEntrySelfS0164;
	}
	public String getFNumber() {
		return FNumber;
	}
	public void setFNumber(String fNumber) {
		FNumber = fNumber;
	}
	public String getFShortNumber() {
		return FShortNumber;
	}
	public void setFShortNumber(String fShortNumber) {
		FShortNumber = fShortNumber;
	}
	public String getFName() {
		return FName;
	}
	public void setFName(String fName) {
		FName = fName;
	}


}

寫完實體類後,開始業務層程式碼,這裡只是簡單的寫一個顯示列表的程式碼

SeorderService.java   Service的介面

package cn.gsp.service;

import java.util.List;

import cn.gsp.pojo.SeorderK;

public interface SeorderService {

	List<SeorderK> getByFBillNo() throws Exception;
}

接著寫介面對應的實現方法

SeoderServiceImpl.java

package cn.gsp.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gsp.dao.SeorderKDao;
import cn.gsp.pojo.SeorderK;

@Service
@Transactional(readOnly = true)
public class SeoderServiceImpl implements SeorderService{

	@Autowired
	private SeorderKDao seorderkDao;

	@Override
	public List<SeorderK> getByFBillNo() throws Exception {

		List<SeorderK> list=seorderkDao.getByFBillNo();
		return list;
	}
	

}

接下來是controller層
package cn.gsp.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.gsp.pojo.SeorderK;
import cn.gsp.service.SeorderService;

@Controller
@RequestMapping("/seorder")
public class SeorderController {
		
		@Autowired
		private SeorderService seorderService;
	
		@RequestMapping("/list")
		public String list(Model model) throws Exception{		
			//查詢資料列表和資料總數
			List<SeorderK> resutList = seorderService.getByFBillNo();
			model.addAttribute("resutList", resutList);
			return "seorder";
			
		}
}

根據最後的return"seorder" 再寫一個返回的頁面就可以了

seorder.jsp 的主要部分

<div class="resutList">
				<div class="col-lg-12">
					<div class="panel panel-default">
						<div class="panel-heading">資訊列表</div>
						<!-- /.panel-heading -->
							<table class="table table-bordered table-striped">
								<thead>
									<tr>
										<th>訂單編號</th>
										<th>客戶程式碼</th>
										<th>新編碼</th>
										<th>GSP長程式碼</th>
										<th>GSP短程式碼</th>
										<th>GSP名稱</th>
									</tr>
								</thead>
							<tbody>
								<c:forEach items="${resutList}" var="resutList">
									<tr>
										<td>${resutList.FBillNo}</td>
										<td>${resutList.FMapNumber}</td>
										<td>${resutList.FEntrySelfS0164}</td>
										<td>${resutList.FNumber}</td>
										<td>${resutList.FShortNumber}</td>
										<td>${resutList.FName}</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>

以上的內容全為初學者的一己之見,如有問題和錯誤,感謝提出寶貴的建議,謝謝!!!