当前位置:网站首页>SSM实现一对一查询实战详细教程(一)
SSM实现一对一查询实战详细教程(一)
2022-07-17 08:57:00 【逍遥游@】
引言: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 [[email protected]]
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、效果图

希望能帮到大家
边栏推荐
猜你喜欢
随机推荐
什么是内存溢出
MySQL8下的JSON
Support for multiple devices in (ghost engine)
数据库——sql-server
【LeetCode】通用操作总结
深度学习第四周Key Concepts on Deep Neural Networks习题整理
C# - this 的用法
深度学习第二周Neural Network Basics习题整理
单机项目进行微服务拆分
instanceof的另类写法示例说明
cut,sort,uniq,xargs
2022年夏令营难忘的一天
【CTF】pwn2_sctf_2016
gradle入门笔记
自定义MVC原理及简单实现
软件工程师视角的项目合同分析
JSON under mysql8
深度学习第一周Introduction to Deep Learning习题整理
[face recognition] face recognition based on histogram histogram with matlab code
Change the theme of hbuilderx into vscode









