【Mybatis】—Basic Exercises (3)

I. Overview:

Mybatis:

Realize the information query of users and their addresses.

(1) There is a one-to-many relationship between users and addresses, and one- to-many entity association configuration is required for them.

(2) Implemented in two ways: XML and annotations

 Description: database springdb, the data table is t_user and t_address

 

 

 Persistence class:

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

    UserMapper.java

    AddressMapper.java

    UserMapper.xml

     AddressMapper.xml

    testing method:

    Test Results:

2. Annotation method:

    UserMapper.java

    AddressMapper.java

    Usersql.java

    Two mapper configuration files were deleted, and other codes were not changed:    

    test:

 

Guess you like

Origin blog.csdn.net/m0_56233309/article/details/123601618