0


【Mybatis】—基础练习(3)

一. 概述:

Mybatis:

实现用户及其地址的信息查询。

(1)用户与地址是一对多的关系,需要对其进行一对多实体的关联配置

(2)分别使用XML和注解两种方式实现

说明:数据库springdb,数据表为t_user和t_address

** 持久化类:**

1. Address.java

package org.example.po;

public class Address {
    private int id;
    private String streetName;
    private String zipcode;
    private int userInfo_Id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getStreetName() {
        return streetName;
    }

    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }

    public int getUserInfo_Id() {
        return userInfo_Id;
    }

    public void setUserInfo_Id(int userInfo_Id) {
        this.userInfo_Id = userInfo_Id;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", streetName='" + streetName + '\'' +
                ", zipcode='" + zipcode + '\'' +
                ", userInfo_Id='" + userInfo_Id + '\'' +
                '}';
    }
}

2. User.java

package org.example.po;

import java.util.List;

public class User {
    private int id;
    private String loginName;
    private String password;
    private String realName;
    private List<Address> addresses;

    public List<Address> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Address> addresses) {
        this.addresses = addresses;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

//    @Override
//    public String toString() {
//        return "User{" +
//                "id=" + id +
//                ", loginName='" + loginName + '\'' +
//                ", password='" + password + '\'' +
//                ", realName='" + realName + '\'' +
//                '}';
//    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", loginName='" + loginName + '\'' +
                ", password='" + password + '\'' +
                ", realName='" + realName + '\'' +
                ", addresses=" + addresses +
                '}';
    }
}

1. XML方式:

** UserMapper.java**

** AddressMapper.java**

** UserMapper.xml**

**** ****AddressMapper.xml

** 测试方法:**

** 测试结果:**

2. 注解方式:

** UserMapper.java**

** AddressMapper.java**

** Usersql.java**

** 删除了两个mapper配置文件,其他代码没有改: **

** 测试:**


本文转载自: https://blog.csdn.net/m0_56233309/article/details/123601618
版权归原作者 小皮猪 所有, 如有侵权,请联系我们删除。

“【Mybatis】&mdash;基础练习(3)”的评论:

还没有评论