引言:SSM中的級聯查詢非常重要,今天不說別的,我們就來研究一下多表查詢中的一對一查詢。我們一定要搞懂簡單的概念
比如,一個人只有對應的一個身份證,這個大家就好理解,但是(好多種)茶葉 茶葉分類(龍井、普洱),從字面上看,好像是一對多的關係,大家可能會混淆,但我們仔細來看,拿茶葉的各種牌子爲主,就是一對一的關係,也就是一個品牌的茶葉他對應的
要麼就是紅茶,要麼就是綠茶,而以茶葉分類爲主,對應着各種各樣牌子的茶葉那就是一對多的關係了,大家一定要區分。
1主類teas數據庫表
2不要顛倒了,teas-type (一對一),type-teas(一對多)
多表查詢sql語句通過teas的tId查詢出對應的類
select t1.teaName,t1.tCover,t1.price,t1.info,t1.count,t2.tName from teas t1 left join type t2 on t1.tId=t2.id
3我們討論的是teas(主)-type
首先我們在父類的Teas寫好實體類,要添加副類表type的實體類
package com.school.model;
public class Teas {
private Integer id;
private Type type;
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer gettId() {
return tId;
}
public void settId(Integer tId) {
this.tId = tId;
}
public String getTeaName() {
return teaName;
}
public void setTeaName(String teaName) {
this.teaName = teaName;
}
public String gettCover() {
return tCover;
}
public void settCover(String tCover) {
this.tCover = tCover;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
private Integer tId;
private String teaName;
private String tCover;
private String img;
@Override
public String toString() {
return "Teas [id=" + id + ", tId=" + tId + ", teaName=" + teaName + ", tCover=" + tCover + ", img=" + img
+ ", price=" + price + ", info=" + info + ", count=" + count + "]";
}
private double price;
private String info;
private Integer count;
}
4、副類type表
ublic class Type {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName == null ? null : tName.trim();
}
private String tName;
}
5、在主類teas寫一個接口
public interface TeasDao {
List<Teas> selectAll();//查詢所有
}
記得實現方法
6、TeasMapper.xml文件配置
<mapper namespace="com.school.dao.TeasDao">
<resultMap id="BaseResultMap" type="com.school.model.Teas">
<id column="id" jdbcType="INTEGER" property="id" />
<id column="tId" jdbcType="INTEGER" property="tId" />
<result column="teaName" jdbcType="VARCHAR" property="teaName" />
<result column="tCover" jdbcType="VARCHAR" property="tCover" />
<result column="img" jdbcType="VARCHAR" property="img" />
<result column="price" jdbcType="DOUBLE" property="price" />
<result column="info" jdbcType="VARCHAR" property="info" />
<result column="count" jdbcType="INTEGER" property="count" />
<!-- 一對一級聯,一個茶值對應一類 -->
<association property="type" javaType="com.school.model.Type">
<id column="id" jdbcType="INTEGER" property="id" />對應着type裏的字段
<result column="tName" property="tName"/>
</association>
</resultMap>
<select id="selectAll" parameterType="com.school.model.Teas" resultMap="BaseResultMap">
select t1.*,t2.tName from teas t1 left join type t2 on t1.tId=t2.id
</select>
</mapper>
7、TypeMapper.xml文件配置
<mapper namespace="com.school.dao.TypeDao">
<resultMap id="BaseResultMap" type="com.school.model.Type">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="tName" jdbcType="VARCHAR" property="tName" />
</resultMap>
<mapper>
8、mybatis-config記得配置
<mappers>
<mapper resource="com/school/mapper/TypeMapper.xml"/>
<mapper resource="com/school/mapper/TeasMapper.xml"/>
</mappers>
10、test測試
public static void main(String[] args) throws Exception {
String resource="mybatis-config.xml";
InputStream is=Resources.getResourceAsStream(resource);
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
TeasDao teasDao=session.getMapper(TeasDao.class);
List<Teas> list=teasDao.selectAll();
for(Teas t:list) {
System.out.print(t.getType().gettName());//有很多人不知道另一個表的字段如何打印出來,我們設置private Type type就起到這個作用,通過t.getType().gettName()獲取另一個字段
}
System.out.println(list.size());
}
19:57:05.774 [main] DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@21a947fe]
19:57:05.794 [main] DEBUG com.school.dao.TeasDao.selectAll - ==> Preparing: select t1.*,t2.tName from teas t1 left join type t2 on t1.tId=t2.id
19:57:05.849 [main] DEBUG com.school.dao.TeasDao.selectAll - ==> Parameters:
19:57:05.892 [main] DEBUG com.school.dao.TeasDao.selectAll - <== Total: 3
普爾茶普爾茶普爾茶3
11、jsp實現回顯
<c:forEach items="${ pageInfo.list}" var="teas">
<tr>
<td id="teasId">${teas.id}</td>
<td><img src="<%=path %>/${teas.tCover}" width="70px" height="70px"></td>
<td>${teas.teaName}</td>
<td>${teas.info}</td>
<td>${teas.price}</td>
<td>${teas.count}</td>
<td>${teas.type.tName}</td>//${tears.type.tName}這樣就可以了
</td>
</tr>
</c:forEach>
12、效果圖
希望能幫到大家