利用SSM(springmvc+spring+mybatis)實現多表聯合查詢
阿新 • • 發佈:2019-02-18
一、資料庫
Author表 (authorid主鍵) NewsInfo表(newsno主鍵) NewsType表(news_typeid主鍵)
二、實體類
package com.neusoft.po; import java.util.List; /** * 作者類 * @author hyc * */ public class Author { private String authorid;//作者編號 private String authorname;//作者名 public Author() { super(); } public Author(String authorid, String authorname) { super(); this.authorid = authorid; this.authorname = authorname; } public String getAuthorid() { return authorid; } public void setAuthorid(String authorid) { this.authorid = authorid; } public String getAuthorname() { return authorname; } public void setAuthorname(String authorname) { this.authorname = authorname; } }
Author類
NewsInfo類package com.neusoft.po; import java.util.Date; import java.util.List; /** * 新聞資訊類 * @author hyc * */ public class NewsInfo { private String newsno;//新聞編號 private String news_title;//新聞標題 private String news_author;//新聞作者 private int news_type;//新聞種類 private NewsType type; private Author author; public NewsInfo(String newsno, String news_title, String news_author, int news_type) { super(); this.newsno = newsno; this.news_title = news_title; this.news_author = news_author; this.news_type = news_type; } public NewsInfo() { super(); } public String getNewsno() { return newsno; } public void setNewsno(String newsno) { this.newsno = newsno; } public String getNews_title() { return news_title; } public void setNews_title(String news_title) { this.news_title = news_title; } public String getNews_author() { return news_author; } public void setNews_author(String news_author) { this.news_author = news_author; } public int getNews_type() { return news_type; } public void setNews_type(int news_type) { this.news_type = news_type; } }
NewsType類package com.neusoft.po; import java.util.List; /** * 新聞類別類 * @author hyc * */ public class NewsType { private int news_typeid;//類別編號 private String news_typename;//類別名稱 //一對多的關係 private List<NewsInfo> infoList; public List<NewsInfo> getInfoList() { return infoList; } public void setInfoList(List<NewsInfo> infoList) { this.infoList = infoList; } public NewsType() { super(); } public NewsType(int news_typeid, String news_typename, List<NewsInfo> infoList) { super(); this.news_typeid = news_typeid; this.news_typename = news_typename; this.infoList = infoList; } public int getNews_typeid() { return news_typeid; } public void setNews_typeid(int news_typeid) { this.news_typeid = news_typeid; } public String getNews_typename() { return news_typename; } public void setNews_typename(String news_typename) { this.news_typename = news_typename; } }
三、Mapper
<resultMap type="com.neusoft.po.NewsInfo" id="newsInfoMap">
<id column="newsno" property="newsno" />
<result column="news_title" property="news_title"/>
<association property="author" javaType="com.neusoft.po.Author">
<result column="authorname" property="authorname"/>
</association>
<association property="type" javaType="com.neusoft.po.NewsType">
<result column="news_typename" property="news_typename"/>
</association>
</resultMap>
<select id="findnewsInfoMap" resultMap="newsInfoMap">
select i.newsno,i.news_title,a.authorname,t.news_typename from news_info i,news_author a,news_type t where i.news_author=a.authorid and i.news_type=t.news_typeid
</select>
NewsInfoMapper.xml
package com.neusoft.mapper;
import java.util.List;
import com.neusoft.po.Author;
import com.neusoft.po.NewsInfo;
import com.neusoft.po.NewsType;
public interface NewsInfoMapper {
/* public void insertAuthor(Author author);
public void deleteAuthor(String authorid);
public void insertNewsType(NewsType newsType);
public void deleteType(int news_typeid);
public void insertNewsInfo(NewsInfo newsInfo);
public void deleteInfo(String userno);*/
public List<NewsInfo> findnewsInfoMap();
}
NewsInfoMapper.java 四、service
package com.neusoft.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.neusoft.po.Author;
import com.neusoft.po.NewsInfo;
import com.neusoft.po.NewsType;
public interface NewsInfoService {
public List<NewsInfo> allNewsInfo();
}
NewsInfoService.java
@Autowired
NewsInfoMapper newsInfoMapper;
@Override
public List<NewsInfo> allNewsInfo() {
List<NewsInfo> newsInfoList=new ArrayList<NewsInfo>();
newsInfoList=newsInfoMapper.findnewsInfoMap();
return newsInfoList;
}
NewsInfoServiceImpl.java
五、control
@Autowired
NewsInfoService newsInfoService;
@RequestMapping("searchInfo")
public String searchNewsInfo(Model model){
List<NewsInfo> newsList = newsInfoService.allNewsInfo();
model.addAttribute("newsList", newsList);
return "show";
}
NewsInfoController.java
六、jsp
<a href="searchInfo.action">查詢Info</a> ----index.jsp(我的首頁)
<c:forEach items="${newsList}" var="newsList"> ${newsList.newsno}<br> ${newsList.news_title}<br> </c:forEach>-------show.jsp(利用EL表示式,forEach迴圈 表示查詢的結果。注意不要忘記在jsp頭部寫下taglib引用c標籤)
七、總結
多表查詢主要是考究表與表之間的關係,這裡我用的是三表查詢;三表分別為:Author、NewsInfo、NewsType
Author : NewsInfo = 1 : 1
NewsType : NewsInfo = 1 : N
記住在po層裡的NewsType宣告一對多的關係時,別忘記在NewsInfo裡宣告多對一的關係
//一對多的關係(NewsType裡寫的) private List<NewsInfo> infoList;
//NewsInfo裡寫的
private NewsType type;
private Author author;