当前位置:网站首页>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、效果图

希望能帮到大家
边栏推荐
- What is memory overflow
- 深度学习第一周Introduction to Deep Learning习题整理
- JSON under mysql8
- MySQL中查询一个字符串字段的值不为空到底该怎么写?
- 【Port 3000 is already in use,3000端口被占用问题解决方法】
- 深度学习第四周Key Concepts on Deep Neural Networks习题整理
- 单机项目进行微服务拆分
- [regression prediction] lithium ion battery life prediction based on particle filter with matlab code
- 【虹科】激光雷达安全系统:让世界更安全
- pygame中display模块
猜你喜欢

vscode下载历史版本

WPF 3D application building (Foundation)

Distributed transaction reliable message final consistency solution

LeetCode 0116.填充每个节点的下一个右侧节点指针

BCG 使用之CBCGPEdit控件

Sorting out of neural network basics exercises in the second week of in-depth study

Scratch reverse order output electronic society graphical programming scratch grade examination level 4 true questions and answers analysis June 2022
![[face recognition] face recognition based on histogram histogram with matlab code](/img/a6/ae776d084a207647501ca951e32000.png)
[face recognition] face recognition based on histogram histogram with matlab code

LeetCode 剑指 Offer II 041. 滑动窗口的平均值:低空间消耗解决
![[regression prediction] lithium ion battery life prediction based on particle filter with matlab code](/img/1d/5562934499edfb4781b1118d0c4f9c.png)
[regression prediction] lithium ion battery life prediction based on particle filter with matlab code
随机推荐
SDL图像显示
MySQL索引(二)
[Hongke] Introduction to genicam protocol
Cocos shader basics 7
Baidu Apollo
Magic Usage of mongodb $symbol +mongo data type
Translucent double glass side thickness
也挺难的,不光我自己写的很累
QT串口通信
JSON under mysql8
为什么别人游戏里的特效都非常柔和
QR分解求矩阵逆--c工程实现
Error received from peer ipv4/connection reset by peer paddleserving
Zero basic C language
mongodb $符号的神奇用法+mongo数据类型
Junit5
项目代码训练教程
2022年P气瓶充装考试练习题模拟考试平台操作
VMware中扩展硬盘
Redis