SSM实现一对一查询实战详细教程(一)

 

引言:SSM中的级联查询非常重要,今天不说别的,我们就来研究一下多表查询中的一对一查询。我们一定要搞懂简单的概念

比如,一个人只有对应的一个身份证,这个大家就好理解,但是(好多种)茶叶 茶叶分类(龙井、普洱),从字面上看,好像是一对多的关系,大家可能会混淆,但我们仔细来看,拿茶叶的各种牌子为主,就是一对一的关系,也就是一个品牌的茶叶他对应的

要么就是红茶,要么就是绿茶,而以茶叶分类为主,对应着各种各样牌子的茶叶那就是一对多的关系了,大家一定要区分。


1主类teas数据库表

SSM实现一对一查询实战详细教程(一)_第1张图片


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 selectAll();//查询所有
   }

记得实现方法


6、TeasMapper.xml文件配置


 
   
   
     
     
             
             
             
           
             
             
                 
             对应着type里的字段
                  
           

 
 

 


7、TypeMapper.xml文件配置


 
   
             
 


8、mybatis-config记得配置


 
  


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 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



        

SSM实现一对一查询实战详细教程(一)_第2张图片


        


11、jsp实现回显


                 
                  ${teas.id}
                 
                     ${teas.teaName}
                  ${teas.info}
                   ${teas.price}
                   ${teas.count}
                   ${teas.type.tName}//${tears.type.tName}这样就可以了
               
                 
         

 


12、效果图

SSM实现一对一查询实战详细教程(一)_第3张图片


希望能帮到大家

你可能感兴趣的:(ssm一系列,Java)