02 mybatis一对一

本文将阐述mybatis中的表是一对一关系的情况下如何完成查询操作。

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • jdk-8u162-windows-x64
  • mybatis3.2.8
  • mysql 6.5

2、前提约束

  • 完成idea中创建mybatis项目 https://www.jianshu.com/p/86db0901156f

3、操作步骤

  • 创建一个用户表和一个登录表
create table t_user(id int, name varchar(20));
insert into t_user(id,name) values(1,'ali');
insert into t_user(id,name) values(2,'zhangli');
create table t_login(id int, username varchar(20), password varvhar(20), userid int);
insert into t_login(id,username,password,userid) values(1,'aliuser','alipass',1);
insert into t_login(id,username,password,userid) values(2,'zhangliuser','zhanglipass',2);
  • 创建两张表对应的实体类
    User.java
package net.wanho.entity;

import java.io.Serializable;

public class User implements Serializable {
    private int id;
    private String name;

    public User() {
    }


    public User(String name)
    {
        this.name = name;
    }
    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Login.java

package net.wanho.entity;

import java.io.Serializable;

public class Login implements Serializable {
    private int id;
    private String username;
    private String password;

    private User user;

    public Login(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Login() {
    }


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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


    @Override
    public String toString() {
        return "Login{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
  • 在UserMapper.java中新增一个查询方法
List selectLogin();
  • 在UserMapper.xml中新增标签
    
    
        
        
        
        
            
            
        
    
  • 测试
//关键代码
String resource ="mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List loginList = userMapper.selectLogin();

通过单步调试,便能查看到loginList包含t_login以及t_user中的数据。

你可能感兴趣的:(02 mybatis一对一)