mybatis一对一,一对多

一对一

实体类

dept类和emp类

package com.chen.bean;

public class Dept {
    private Integer did;
    private String dname;
    private String dinfo;

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getDinfo() {
        return dinfo;
    }

    public void setDinfo(String dinfo) {
        this.dinfo = dinfo;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "did=" + did +
                ", dname='" + dname + '\'' +
                ", dinfo='" + dinfo + '\'' +
                '}';
    }
}

package com.chen.bean;

import sun.print.PSPrinterJob;

public class Emp {
    private Integer eid;
    private String ename;
    private String job;
    private Double sal;
    private String phone;
    private String address;
    //一对一关系的定义:把外键的did 定义为相关联的 对象
    private Dept dept;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Double getSal() {
        return sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getAddress() {
        return address;
    }

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

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", dept=" + dept +
                '}';
    }
}

dao包接口和mapper文件

package com.chen.dao;

import com.chen.bean.Dept;

public interface DeptDao {
    Dept selectByDid(int did);
}

package com.chen.dao;

import com.chen.bean.Emp;

import java.util.List;

public interface EmpDao {
    Emp selectBuEid(int eid);

    List<Emp> selectAll();
}
<mapper namespace="com.chen.dao.DeptDao">
<select id="selectByDid" parameterType="int" resultType="com.chen.bean.Dept"> select * from dept where did=#{did}; </select>
</mapper>

<mapper namespace="com.chen.dao.EmpDao">
<!-- 定义手动映射关系:数据库中字段 和 类的属性 的映射 -->
<resultMap id="empMap" type="com.chen.bean.Emp">
<id property="eid" column="eid"/>
<result property="ename" column="ename"/>
<result property="job" column="job"/>
<result property="sal" column="sal"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
<!-- 一对一的映射配置association: 自动关联查询:根据外键did执行dept表的关联查询,把查询出来部门信息封装到emp的dept属性 -->
<association column="did" select="com.chen.dao.DeptDao.selectByDid" property="dept"> </association>
</resultMap>
<select id="selectBuEid" parameterType="int" resultMap="empMap"> select * from emp where eid=#{eid} </select>
<select id="selectAll" resultMap="empMap"> select * from emp </select>
</mapper>

测试类

package com.chen.test;

import com.chen.bean.Dept;
import com.chen.dao.DeptDao;
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.ArrayList;
import java.util.List;

public class DeptTest {
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    DeptDao deptDao=null;

    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        deptDao=sqlSession.getMapper(DeptDao.class);
    }

    @Test
    public void testSelectByDid(){
        Dept dept = deptDao.selectByDid(101);
        System.out.println(dept);
    }



    @After
    public void distroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}

package com.chen.test;

import com.chen.bean.Emp;
import com.chen.dao.EmpDao;
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 EmpTest {
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    EmpDao empDao=null;

    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        empDao=sqlSession.getMapper(EmpDao.class);
    }

    @Test
    public void testSelectByeid(){
        Emp emp = empDao.selectBuEid(2);
        Emp emp1 = empDao.selectBuEid(2);
        System.out.println(emp);
        System.out.println(emp1);
    }

    @Test
    public void testSelectAll(){
        List<Emp> empList = empDao.selectAll();
        for (Emp emp : empList) {
            System.out.println(emp);
        }
    }

    @After
    public void distroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}

一对多

实体类

dept类和emp类

package com.chen.bean;

import java.util.List;

public class Dept {
    private Integer did;
    private String dname;
    private String dinfo;
    //一个部门包含多个员工,一对多关系就是定义集合
    List<Emp> emps;

    public List<Emp> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    public String getDinfo() {
        return dinfo;
    }

    public void setDinfo(String dinfo) {
        this.dinfo = dinfo;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "did=" + did +
                ", dname='" + dname + '\'' +
                ", dinfo='" + dinfo + '\'' +
                ", emps=" + emps +
                '}';
    }
}
package com.chen.bean;

public class Emp {
    private Integer eid;
    private String ename;
    private String job;
    private Double sal;
    private String phone;
    private String address;
    private Integer did;

    public Integer getEid() {
        return eid;
    }

    public void setEid(Integer eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Double getSal() {
        return sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    public String getPhone() {
        return phone;
    }

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

    public String getAddress() {
        return address;
    }

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

    public Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "eid=" + eid +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", did=" + did +
                '}';
    }
}

dao包接口和mapper文件

package com.chen.dao;

import com.chen.bean.Dept;

public interface DeptDao {
    Dept selectByDid(int did);
}
package com.chen.dao;

import com.chen.bean.Emp;

import java.util.List;

public interface EmpDao {
    Emp selectByEid(int eid);

    List<Emp> selectByDid(int did);
}
<mapper namespace="com.chen.dao.DeptDao">
<resultMap id="deptMap" type="com.chen.bean.Dept">
<id property="did" column="did"/>
<result property="dname" column="dname"/>
<result property="dinfo" column="dinfo"/>
<!-- 配置一对多关系:自动根据did去关联查询emp表中该did的信息 -->
<collection column="did" select="com.chen.dao.EmpDao.selectByDid" property="emps"> </collection>
</resultMap>
<select id="selectByDid" parameterType="int" resultMap="deptMap"> select * from dept where did=#{did}; </select>
</mapper>
<mapper namespace="com.chen.dao.EmpDao">
<select id="selectByEid" parameterType="int" resultType="emp"> select * from emp where eid=#{eid} </select>
<select id="selectByDid" parameterType="int" resultType="emp"> select * from emp where did=#{did} </select>
</mapper>

测试类

package com.chen.test;

import com.chen.bean.Dept;
import com.chen.dao.DeptDao;
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;

public class DeptTest {
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    DeptDao deptDao=null;

    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        deptDao=sqlSession.getMapper(DeptDao.class);
    }

    @Test
    public void testSelectByDid(){
        Dept dept = deptDao.selectByDid(101);
        System.out.println(dept);
    }



    @After
    public void distroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}
package com.chen.test;

import com.chen.bean.Emp;
import com.chen.dao.EmpDao;
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 EmpTest {
    InputStream stream=null;
    SqlSessionFactoryBuilder builder=null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    EmpDao empDao=null;

    @Before
    public void init() throws IOException {
        stream= Resources.getResourceAsStream("mybatis.xml");
        builder=new SqlSessionFactoryBuilder();
        factory=builder.build(stream);
        sqlSession=factory.openSession();
        empDao=sqlSession.getMapper(EmpDao.class);
    }

    @Test
    public void testSelectByEid(){
        Emp emp = empDao.selectByEid(2);
        System.out.println(emp);
    }

    @After
    public void distroy() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        stream.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值