ssm框架学习---mybatis中处理一对一对应关系

1,对于一对一对应关系,有两种方式来实现

(1)一种是使用resultType,这个有一个要求就是查询出来的列和返回的java对象的列的名称需要对应,这里我们想要实现依据订单来查询对应订单的下单用户的信息的查询,因此需要们自己建立pojo,首先给出客户,订单,商品的定义类如下:

package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-19.
 */
public class Customer {
    private int cno;
    private String cname;
    private String csex;
    private String cphone;

    public int getCno() {
        return cno;
    }

    public void setCno(int cno) {
        this.cno = cno;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCsex() {
        return csex;
    }

    public void setCsex(String csex) {
        this.csex = csex;
    }

    public String getCphone() {
        return cphone;
    }

    public void setCphone(String cphone) {
        this.cphone = cphone;
    }

    public String getCaddress() {
        return caddress;
    }

    public void setCaddress(String caddress) {
        this.caddress = caddress;
    }

    private String caddress;

}
package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-19.
 */
public class Orders {
    private int ono;
    private String otime;
    private int cno;
    private int gno;

    public int getOno() {
        return ono;
    }

    public void setOno(int ono) {
        this.ono = ono;
    }

    public String getOtime() {
        return otime;
    }

    public void setOtime(String otime) {
        this.otime = otime;
    }

    public int getCno() {
        return cno;
    }

    public void setCno(int cno) {
        this.cno = cno;
    }

    public int getGno() {
        return gno;
    }

    public void setGno(int gno) {
        this.gno = gno;
    }

    @Override
    public String toString() {
        return "Orders{" +
                "ono=" + ono +
                ", otime='" + otime + '\'' +
                ", cno=" + cno +
                ", gno=" + gno +
                '}';
    }
}
package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-19.
 */
public class Goods {
    private int gno;
    private String gname;

    public int getGno() {
        return gno;
    }

    public void setGno(int gno) {
        this.gno = gno;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public String getGtype() {
        return gtype;
    }

    public void setGtype(String gtype) {
        this.gtype = gtype;
    }

    public float getGprice() {
        return gprice;
    }

    public void setGprice(float gprice) {
        this.gprice = gprice;
    }

    private String gtype;
    private float gprice;

}
下面我们主查询是订单,并附属查询用户的姓名和电话,因此定义自己的pojo如下:继承Orders类

package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-19.
 */
public class OrdersCustomer extends Orders {
    private String cname;

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getCphone() {
        return cphone;
    }

    public void setCphone(String cphone) {
        this.cphone = cphone;
    }

    private String cphone;

    @Override
    public String toString() {
        return "OrdersCustomer{" +
                "ono='" + this.getOno() + '\'' +
                "otime='" + this.getOtime() + '\'' +
                "cno='" + this.getCno() + '\'' +
                "gno='" + this.getGno() + '\'' +
                "cname='" + cname + '\'' +
                ", cphone='" + cphone + '\'' +
                '}';
    }
}
然后写我们的mapper.xml如下:




    
    

它所对应的mapper接口定义如下:

package com.ajin.mybatis.mapper;

import com.ajin.mybatis.model.OrdersCustomer;

/**
 * Created by ajin on 16-12-19.
 */
public interface OrdersCustomerMapper {
    public OrdersCustomer selectOrdersCustomerById(int id);
}
然后将我们的mapper.xml文件加到mybatis的配置文件中,最后测试代码如下:

package com.ajin.mybatis.mapper;

import com.ajin.mybatis.model.OrdersCustomer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

import static org.junit.Assert.*;

/**
 * Created by ajin on 16-12-19.
 */
public class OrdersCustomerMapperTest {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void setUp() throws Exception {
        String resource = "config/SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }


    @Test
    public void selectOrdersCustomerById() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            OrdersCustomerMapper ordersCustomerMapper = sqlSession.getMapper(OrdersCustomerMapper.class);
            OrdersCustomer ordersCustomer=ordersCustomerMapper.selectOrdersCustomerById(2);
            System.out.println(ordersCustomer);
        }finally {

        }
    }

}

(2)第二种方式,使用resultMap,这种方式就是需要定义resultMap,并适用与需要延迟加载的情况

主要就是resultmap的编写如下:




    
    
        
        
            
    
    
    
        
        
        
        
        
        
        
            
            
            
        
    
    


返回类型定义如下:

package com.ajin.mybatis.model;

/**
 * Created by ajin on 16-12-19.
 */
public class OrdersCustomerMap extends Orders {
    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}
对应的接口和测试如下:

package com.ajin.mybatis.mapper;

import com.ajin.mybatis.model.OrdersCustomerMap;
import com.ajin.mybatis.model.OrdersCustomer;

/**
 * Created by ajin on 16-12-19.
 */
public interface OrdersCustomerMapper {
    public OrdersCustomer selectOrdersCustomerById(int id);
    public OrdersCustomerMap selectOrdersCustomerMapById(int id);
}

@Test
    public void selectOrdersCustomerMapById() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            OrdersCustomerMapper ordersCustomerMapper = sqlSession.getMapper(OrdersCustomerMapper.class);
            OrdersCustomerMap ordersCustomer=ordersCustomerMapper.selectOrdersCustomerMapById(5);
            System.out.println(ordersCustomer);
        }finally {
            sqlSession.close();
        }
    }

一对一对应关系resultmap中,主要使用到了association这个标签,



你可能感兴趣的:(SSM,Mybatis)