mybatis,简单一对一查询案例

目录结构
mybatis,简单一对一查询案例_第1张图片数据库表
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020042623435838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3mybatis,简单一对一查询案例_第2张图片
FxXzMzMzUxOTg2,size_16,color_FFFFFF,t_70)

1,写两个表的javabean实体类

package com.itheima.domain;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private Integer id;
    private String  username;
    private String address;
    private String sex;
    private Date birthday;

    public Integer getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getAddress() {
        return address;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", address='" + address + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}
package com.itheima.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private Integer uid;
    private double money;
    private User user;

    public User getUser() {
        return user;
    }

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

    public Integer getId() {
        return id;
    }

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

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}

2,写实体类的到dao类

package com.itheima.dao;

import com.itheima.domain.User;

import java.util.List;

/**
 * 用户的持久层
 */
public interface IUserDao {
    /*
    * 查询所有用户
    * */
    List<User> findAll();


    /*
    * 根据id查询用户信息
    * */
    User findById(Integer userId);

}
package com.itheima.dao;

import com.itheima.domain.Account;
import com.itheima.domain.AccountUser;

import java.util.List;

public interface IAccountDao {
    /**
     * 查询所有
     * @return
     */
    List<Account> findAll();
    List<AccountUser> findAllAccount();
}

接口Maper 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.IAccountDao">
<!--定义封装account 和 user 的 resultMap-->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!--一对一的映射关系,配置封装user的内容-->
        <!--property="user"表示要对应的主表,column="uid"表示附表的外键  javaType="user"封装到哪里去-->
        <association property="user" column="uid" javaType="user">
            <id property="id" column="id"></id>
            <result column="username" property="username"></result>
            <result column="address" property="address"></result>
            <result column="sex" property="sex"></result>
            <result column="birthday" property="birthday"></result>
        </association>
    </resultMap>

    <!--查询所有-->
    <select id="findAll" resultMap="accountUserMap" >
       select u.*,a.id as aid,a.uid,a.money from account a,user u where u.id=a.uid
    </select>
    <!--根据用户id查询-->
    <select id="findAllAccount"  resultType="AccountUser">
         select a.*, u.address,u.username from account a,user u where u.id=a.uid;
    </select>




</mapper>

测试类

package com.itheima.test;

import com.itheima.dao.IAccountDao;
import com.itheima.dao.IUserDao;
import com.itheima.domain.Account;
import com.itheima.domain.AccountUser;
import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class AccountTest {
    private InputStream in;
    private SqlSession sqlSession;
    private IAccountDao accountDao;
    @Before
    public void init() throws IOException {
       //1.读取配置文件
         in = Resources.getResourceAsStream("SqlMapConfig.xml");
       //2.根据配置文件来创建SqlSessionFactory
       SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
       //3.通过SqlSessionFactory来获取SqlSession会话
         sqlSession = ssf.openSession(true);
       //4.获取代理对象获得Mapper接口
        accountDao = sqlSession.getMapper(IAccountDao.class);

   }
    @After
   public void destroy () throws IOException {
        //sqlSession.commit();//用于设置自动提交事务

        sqlSession.close();
        in.close();

   }



    /*
    * 测试查询所有用户
    * */
    @Test
    public void testFindAll(){
        //执行查询所有方法
        List<Account> accounts= accountDao.findAll();
        for (Account account : accounts) {
            System.out.println("----------打印每个account-----------");
            System.out.println(account);
            System.out.println("----------打印每个user-----------");
            System.out.println(account.getUser());


        }

    };
    /*
    *测试查询所有账户,同时包含用户名称和地址
    *
    * */
    @Test
    public void testfindAllAccount(){
        //执行查询所有方法
        List<AccountUser> accountUsers = accountDao.findAllAccount();
        for (AccountUser accountUser : accountUsers) {
            System.out.println(accountUser);

        }


    };

}

结果
mybatis,简单一对一查询案例_第3张图片

你可能感兴趣的:(Mybatis)