5.多对一映射

部门表
这里写图片描述

员工表
这里写图片描述

目录结构
这里写图片描述

maven配置,pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cjq</groupId>
  <artifactId>cjq-mybatis1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cjq-mybatis1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

    <!--MyBatis相关-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.8</version>
    </dependency>

    <!--数据库相关-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.35</version>
    </dependency>

  </dependencies>
</project>

configuration.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/test"/>
                <property name="username" value="root"/>
                <property name="password" value="admin"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/EmpMapper.xml"/>
    </mappers>
</configuration>

Dept.java

package com.cjq.vo;

import java.util.List;

/**
 * Created by cjq on 2017/9/16.
 */
public class Dept {

    Integer deptno;
    String  dname;
    String  loc;

    List<Emp> emps;

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

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

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

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

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

    public String getLoc() {
        return loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }
}

Emp.java

package com.cjq.vo;

/**
 * Created by cjq on 2017/9/16.
 */
public class Emp {
    Dept dept;//多对一映射部门表

    public Dept getDept() {
        return dept;
    }

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

    public Emp() {
    }

    public Integer getEno() {
        return eno;

    }

    public Emp(Integer eno, String ename, String job, Double sal) {
        this.eno = eno;
        this.ename = ename;
        this.job = job;
        this.sal = sal;
    }

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

    public void setEno(Integer eno) {
        this.eno = eno;
    }

    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;
    }

    Integer eno;
    String ename;
    String job;
    Double sal;

}

EmpDao.java

package com.cjq.dao;

import com.cjq.vo.Emp;

import java.util.List;

/**
 * Created by cjq on 2017/9/16.
 */
public interface EmpDao {

    public List<Emp> getEmp();
}

SessionFactory .java

package com.cjq.dao;

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 java.io.IOException;
import java.io.InputStream;

/**
 * Created by cjq on 2017/9/16.
 */
public class SessionFactory {
    static SqlSessionFactory factory=null;
    static{
        //测试查询
        String resource="configuration.xml";
        //获取资源的流
        InputStream r= null;
        try {
            r = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获得session工厂
         factory=new SqlSessionFactoryBuilder().build(r);
    }
    public static SqlSession getSession(){
        //openSession默认事物是手动提交的
         return factory.openSession();
    }

}

映射文件EmpMapper.xml

<?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.cjq.dao.EmpDao">
     <!-- namespace的值对应接口-->
     <!--id对应接口的方法,resultType是返回值类型,parameterType方法传入的参数类型 -->

    <!--根据id删除-->
      <select id="getEmp" resultMap="myEmp">
          select eno,ename,job,sal,emp.deptno deptno,dname,loc from emp,dept where emp.deptno=dept.deptno
      </select>

      <!-- 自定义一个映射规则-->
      <resultMap id="myEmp" type="com.cjq.vo.Emp">
         <!--主键 列对应对象中的属性-->
         <id column="eno" property="eno"/>
          <result  column="ename" property="ename"/>
          <result  column="job" property="job"/>
          <result  column="sal" property="sal"/>
         <!--多对一映射(或者一对一映射用此标签)-->
          <association property="dept" javaType="com.cjq.vo.Dept">
              <id column="deptno" property="deptno"/>
              <result  column="dname" property="dname"/>
              <result  column="loc" property="loc"/>
          </association>
      </resultMap>
</mapper>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值