数据表和简单Java类映射转换

原则

数据表的实体类=一个Java的类
数据表的字段=类的属性
表中的一行记录=类的一个实例化对象
表的多行记录=对象数组;
表的外键关联=引用关联

步骤

先不管关联字段,写一个普通的对应类,然后再设计构造方法的时候要设计一个不包含关联字段的构造方法,以便对象可以正常创建,然后如果该对象包含多个外部对象实例就用外部对象的数组,一个就用一个表示就可以了。然后设置setter和getter,最后在创建对象以及配置实例化对象的时候我们就使用匿名对象的方式把已经被包含对象利用包含对象的setter放到包含对象中,完成关联配置。
下面的例子就是一个省里面有多个组织,一个城市有多个人,一个组织有多个人,一个人在一个城市,一个组织里:
Province类

import java.util.Arrays;
import java.util.List;

public class Province  {

    private Integer id;

    private String province;

    private String city;

    private Integer addedLocal;

    private Integer newLocalAsymptomatic;

    private Integer existingConfirmedDiagnosis;

    private Integer cumulativeDiagnosis;

    private Integer cumulativeCure;

    private Integer cumulativeDeaths;

    private User[] users;

    private Organization[] organizations;

    public Province() {
    }

    public Province(Integer id, String province, String city) {
        this.id = id;
        this.province = province;
        this.city = city;
    }

    public Province(Integer id, String province, String city, Integer addedLocal, Integer newLocalAsymptomatic, Integer existingConfirmedDiagnosis, Integer cumulativeDiagnosis, Integer cumulativeCure, Integer cumulativeDeaths, User[] users, Organization[] organizations) {
        this.id = id;
        this.province = province;
        this.city = city;
        this.addedLocal = addedLocal;
        this.newLocalAsymptomatic = newLocalAsymptomatic;
        this.existingConfirmedDiagnosis = existingConfirmedDiagnosis;
        this.cumulativeDiagnosis = cumulativeDiagnosis;
        this.cumulativeCure = cumulativeCure;
        this.cumulativeDeaths = cumulativeDeaths;
        this.users = users;
        this.organizations = organizations;
    }

    public Integer getId() {
        return id;
    }

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

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public Integer getAddedLocal() {
        return addedLocal;
    }

    public void setAddedLocal(Integer addedLocal) {
        this.addedLocal = addedLocal;
    }

    public Integer getNewLocalAsymptomatic() {
        return newLocalAsymptomatic;
    }

    public void setNewLocalAsymptomatic(Integer newLocalAsymptomatic) {
        this.newLocalAsymptomatic = newLocalAsymptomatic;
    }

    public Integer getExistingConfirmedDiagnosis() {
        return existingConfirmedDiagnosis;
    }

    public void setExistingConfirmedDiagnosis(Integer existingConfirmedDiagnosis) {
        this.existingConfirmedDiagnosis = existingConfirmedDiagnosis;
    }

    public Integer getCumulativeDiagnosis() {
        return cumulativeDiagnosis;
    }

    public void setCumulativeDiagnosis(Integer cumulativeDiagnosis) {
        this.cumulativeDiagnosis = cumulativeDiagnosis;
    }

    public Integer getCumulativeCure() {
        return cumulativeCure;
    }

    public void setCumulativeCure(Integer cumulativeCure) {
        this.cumulativeCure = cumulativeCure;
    }

    public Integer getCumulativeDeaths() {
        return cumulativeDeaths;
    }

    public void setCumulativeDeaths(Integer cumulativeDeaths) {
        this.cumulativeDeaths = cumulativeDeaths;
    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public Organization[] getOrganizations() {
        return organizations;
    }

    public void setOrganizations(Organization[] organizations) {
        this.organizations = organizations;
    }

    @Override
    public String toString() {
        return "Province{" +
                "id=" + id +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", addedLocal=" + addedLocal +
                ", newLocalAsymptomatic=" + newLocalAsymptomatic +
                ", existingConfirmedDiagnosis=" + existingConfirmedDiagnosis +
                ", cumulativeDiagnosis=" + cumulativeDiagnosis +
                ", cumulativeCure=" + cumulativeCure +
                ", cumulativeDeaths=" + cumulativeDeaths+
                '}';
    }
}

User类

import javax.crypto.Cipher;
import java.security.PrivateKey;
import java.time.LocalDateTime;
import java.util.PrimitiveIterator;


public class User  {


    private Integer id;

    private String no;

    private String password;

    private String name;

    private String phone;

    private Integer age;

    private Integer result;


    private LocalDateTime detectionTime;

    private String idNumber;

    private Organization organization;
    private LocalDateTime createTime;

    private LocalDateTime lastTime;

    private Province province;

    private String address;

    private Integer roleId;

    private String isvalId;

    public User() {
    }

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
        DataSource.users.add(this);
    }

    public User(Integer id, String no, String password, String name, String phone, Integer age, Integer result, LocalDateTime detectionTime, String idNumber, Organization organization, LocalDateTime createTime, LocalDateTime lastTime, Province province, String address, Integer roleId, String isvalId) {
        this.id = id;
        this.no = no;
        this.password = password;
        this.name = name;
        this.phone = phone;
        this.age = age;
        this.result = result;
        this.detectionTime = detectionTime;
        this.idNumber = idNumber;
        this.organization = organization;
        this.createTime = createTime;
        this.lastTime = lastTime;
        this.province = province;
        this.address = address;
        this.roleId = roleId;
        this.isvalId = isvalId;
    }

    public Integer getId() {
        return id;
    }

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

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getResult() {
        return result;
    }

    public void setResult(Integer result) {
        this.result = result;
    }

    public LocalDateTime getDetectionTime() {
        return detectionTime;
    }

    public void setDetectionTime(LocalDateTime detectionTime) {
        this.detectionTime = detectionTime;
    }

    public String getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getLastTime() {
        return lastTime;
    }

    public void setLastTime(LocalDateTime lastTime) {
        this.lastTime = lastTime;
    }

    public Province getProvince() {
        return province;
    }

    public void setProvince(Province province) {
        this.province = province;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public String getIsvalId() {
        return isvalId;
    }

    public void setIsvalId(String isvalId) {
        this.isvalId = isvalId;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", no='" + no + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                ", age=" + age +
                ", result=" + result +
                ", detectionTime=" + detectionTime +
                ", idNumber='" + idNumber + '\'' +
                ", organization=" + organization +
                ", createTime=" + createTime +
                ", lastTime=" + lastTime +
                ", province=" + province +
                ", address='" + address + '\'' +
                ", roleId=" + roleId +
                ", isvalId='" + isvalId + '\'' +
                '}';
    }
}

Organization

import java.util.Arrays;

public class Organization {

    private Integer id;

    private String organization;

    private String organizationId;

    private User[] users;

    private Province city;

    public Integer getId() {
        return id;
    }

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

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }

    public User[] getUsers() {
        return users;
    }

    public void setUsers(User[] users) {
        this.users = users;
    }

    public Province getCity() {
        return city;
    }

    public void setCity(Province city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Organization:" +
                "id=" + id +
                ", organization='" + organization + '\'' +
                ", organizationId='" + organizationId + '\'' +
                ", city=" + city ;
    }

    public Organization() {
    }

    public Organization(Integer id, String organization) {
        this.id = id;
        this.organization = organization;
    }

    public Organization(Integer id, String organization, String organizationId) {
        this.id = id;
        this.organization = organization;
        this.organizationId = organizationId;
    }

    public Organization(Integer id, String organization, String organizationId,User[] users, Province city) {
        this.id = id;
        this.organization = organization;
        this.organizationId = organizationId;
        this.users = users;
        this.city = city;
    }
}

关联配置测试

import java.util.Arrays;

public class TableTest {
    public static void main(String[] args) {
        Province province1 = new Province(1,"四川省","成都市");
        Province province2 = new Province(2,"四川省","乐山市");

        Organization organization1 = new Organization(1,"成都纺织高等专科学校");
        Organization organization2 = new Organization(2,"乐山师范学院");
        Organization organization3 = new Organization(3,"西南交通大学");

        User user1 = new User(1,"用户1");

        User user2 = new User(2,"用户2");

        User user3 = new User(3,"用户3");

        User user4 = new User(4,"用户4");
        User user5 = new User(5,"用户5");
        organization1.setCity(province1);
        organization2.setCity(province2);
        organization1.setUsers(new User[]{user1,user2});
        organization2.setUsers(new User[]{user3,user4});
        province1.setUsers(new User[]{user1,user2});
        province1.setOrganizations(new Organization[]{organization1,organization3});
        province2.setUsers(new User[]{user3,user4});
        province2.setOrganizations(new Organization[]{organization2});
        user1.setProvince(province1);
        user1.setOrganization(organization1);
        user2.setProvince(province1);
        user2.setOrganization(organization1);
        user3.setProvince(province2);
        user3.setOrganization(organization2);
        user4.setProvince(province2);
        user4.setOrganization(organization2);

//        System.out.println(user1.toString());
//        System.out.println(province1.toString());
//        System.out.println(organization1.toString());
        System.out.println(Arrays.toString(organization1.getUsers()));
        System.out.println(Arrays.toString(province1.getOrganizations()));
        System.out.println(Arrays.toString(province1.getUsers()));
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值