一对一【Java实现多表操作笔记三】

一对一

原则:一方存多方的集合,多方存一方的对象

但现在双方都是一方数据,此时原则为:一方存另一方的对象

 

实例:妻子和丈夫的关系,模拟一对一的实现过程

 

实现过程:

1、javabean

数据库中使用外键列保证两表关系,实体类中使用属性保证两表关系

[src.bean.Husband.java]
public class Husband {
    private int husId;
    private String husName;
    private int wid;

    //一方存另一方的对象
    private Wife wife;
    public Wife getWife() {return wife;}
    public void setWife(Wife wife) {this.wife = wife;}

    public int getHusId() {return husId;}
    public void setHusId(int husId) {this.husId = husId;}
    public String getHusName() {return husName;}
    public void setHusName(String husName) {this.husName = husName;}
    public int getWid() {return wid;}
    public void setWid(int wid) {this.wid = wid;}
}
[src.bean.Wife.java]
public class Wife {
    private int wifeId;
    private String wifeName;

    //一方存另一方的对象
    private Husband husband;
    public Husband getHusband() {return husband;}
    public void setHusband(Husband husband) {this.husband = husband;}

    public int getWifeId() {return wifeId; }
    public void setWifeId(int wifeId) {this.wifeId = wifeId;}
    public String getWifeName() {return wifeName;}
    public void setWifeName(String wifeName) {this.wifeName = wifeName;}
    public Husband getHusband() {return husband;}
    public void setHusband(Husband husband) {this.husband = husband;}
}

 

2、dao  实现对应数据关系

/*接口:[src.dao.WifeDao.java(interface)]*/
public interface WifeDao {
    //查询某位妻子(包含丈夫的信息)
    public Wife findByWifeId(int wid);
    //查询某位丈夫(包含妻子的信息)
    public Husband findByHid(int hid);
}

/*实现接口:[src.dao.impl.WifeDaoImpl.java]*/
@Override
public Wife findByWifeId(int wid) {
    Wife wife = new Wife();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = getConnection();
        preparedStatement = connection.prepareStatement("select * from wife w,husband h where w.wifeid=h.wid and w.wifeid=?");
        preparedStatement.setInt(1,wid);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            //1.存各自的信息
            wife.setWifeName(resultSet.getString("wifename"));
            Husband husband = new Husband();
            husband.setHusName(resultSet.getString("husname"));
            //2.建立两者关系(将丈夫封装到妻子的对象中)
            wife.setHusband(husband);
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        close(connection,preparedStatement,resultSet);
    }
    return wife;
}
@Override
public Husband findByHid(int hid) {
    Husband husband = new Husband();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    try {
        connection = getConnection();
        preparedStatement = connection.prepareStatement("select * from wife w,husband h where w.wifeid=h.wid and h.husid=?");
        preparedStatement.setInt(1,hid);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            //1.存各自的信息
            Wife wife = new Wife();
            wife.setWifeName(resultSet.getString("wifename"));
            husband.setHusName(resultSet.getString("husname"));
            //2.建立两者关系(将妻子封装到丈夫的对象中)
            husband.setWife(wife);
        }
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        close(connection,preparedStatement,resultSet);
    }
    return husband;
}

 

3、Demo3.java  测试关系

public class Demo3 {
    public static void main(String[] args) {
        WifeDaoImpl wifeDao = new WifeDaoImpl();
        Wife wife = wifeDao.findByWifeId(1);
        System.out.println(wife.getWifeName()+"\t"+wife.getHusband().getHusName());
        Husband husband = wifeDao.findByHid(2);
        System.out.println(husband.getHusName()+"\t"+husband.getWife().getWifeName());
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值