首页 > 其他分享 >Mybatis多表联查(一对一)

Mybatis多表联查(一对一)

时间:2023-05-24 18:44:21浏览次数:46  
标签:多表 String bh stu Mybatis import 联查 stuid public

1、在搭建好Mybatis之后,建两个实体类(将从表实体类型建在主表实体类中)

点击查看代码
package com.bh.po;

public class Student1 {
    private int stuid;
    private String stuname;
   //从表类型
    private Studetail sd;

    public Studetail getSd() {
        return sd;
    }

    public void setSd(Studetail sd) {
        this.sd = sd;
    }

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public Student1() {
    }

    public Student1(int stuid, String stuname, Studetail sd) {
        this.stuid = stuid;
        this.stuname = stuname;
        this.sd = sd;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "stuid=" + stuid +
                ", stuname='" + stuname + '\'' +
                ", sd=" + sd +
                '}';
    }
}

点击查看代码
package com.bh.po;

public class Studetail {
    private int stuid;
    private String stuaddress;
    private String stuage;

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuaddress() {
        return stuaddress;
    }

    public void setStuaddress(String stuaddress) {
        this.stuaddress = stuaddress;
    }

    public String getStuage() {
        return stuage;
    }

    public void setStuage(String stuage) {
        this.stuage = stuage;
    }

    public Studetail() {
    }

    public Studetail(int stuid, String stuaddress, String stuage) {
        this.stuid = stuid;
        this.stuaddress = stuaddress;
        this.stuage = stuage;
    }

    @Override
    public String toString() {
        return "Studetail{" +
                "stuid=" + stuid +
                ", stuaddress='" + stuaddress + '\'' +
                ", stuage='" + stuage + '\'' +
                '}';
    }
}

2、写dao接口
点击查看代码
package com.bh.dao;

import com.bh.po.Student;
import com.bh.po.Student1;

import java.util.List;

public interface IStudent1DAO {
    public List<Student1> findAll();


}

3、写mapper.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.bh.dao.IStudent1DAO">
    <resultMap id="studentMap" type="com.bh.po.Student1">
        <!--column代表数据表=库表的列名-->
        <id property="stuid" column="stuid"/>
        <result property="stuname" column="stuname"/>

        <!-- studentdetail -->
        <!-- 这里需要一个 一对一的对象 -->
        <association property="sd" javaType="com.bh.po.Studetail" column="stuid"><!--这里的column是两个表的关联字段-->
            <id property="stuid" column="stuid"/>
            <result property="stuaddress" column="stuaddress"/>
            <result property="stuage" column="stuage"/>
        </association>

    </resultMap>
<!--多表查询用resultMap-->
    <select resultMap="studentMap" id="findAll">
        select s.*, sd.* from student s , studentdetail sd where s.stuid = sd.stuid
    </select>
    


</mapper>

4、导入mybatis.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>
    <!--DB的四大步骤-->
    <properties>
        <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/class?characterEncoding=utf8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="jdbc"></transactionManager>
            <dataSource type="POOLED">
                <property value="${driver}" name="driver"/>
                <property value="${url}" name="url"/>
                <property value="${username}" name="username"/>
                <property value="${password}" name="password"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>

    <!--<mapper resource="mapper/StudentMapper.xml"/>-->
    <!--<mapper resource="mapper/Student1Mapper.xml"/>-->
    <mapper resource="mapper/DeptMapper.xml"/>
    <!-- <mapper resource="mapper/TableMapper.xml" />-->
    </mappers>
</configuration>

5、测试
点击查看代码
package com.bh.Test;

import com.bh.dao.IDeptDAO;
import com.bh.dao.IStudent1DAO;
import com.bh.dao.IStudentDAO;
import com.bh.po.Dept;
import com.bh.po.Emp;
import com.bh.po.Student;
import com.bh.po.Student1;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

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

public class Test {
    public static void main(String[] args) {
        System.out.println("start--------");
        //读取配置文件
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis.xml");
        //获取工厂的builder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //创建工厂
        SqlSessionFactory sqlSessionFactory = builder.build(in);
        //创建mybatis的sqlSqlsession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        /***********************************单表*********************************************/

        //IStudentDAO studentDAO = sqlSession.getMapper(IStudentDAO.class);
       /* List<Student> all = studentDAO.findAll();
        for (int i = 0; i < all.size(); i++) {
            Student stu = all.get(i);
            System.out.println(stu.getId() + "_" + stu.getName());
        }*/
        //Student stu = new Student();
        //stu.setName("kuan");
        //stu.setId(23);
        /*studentDAO.save(stu);*/
        /*studentDAO.modify(stu);*/
       // studentDAO.remove(stu);
        //sqlSession.commit();
        //System.out.println("end---------");

        /***********************************多表联查一对一*********************************************/
       /* IStudent1DAO student1DAO = sqlSession.getMapper(IStudent1DAO.class);
        List<Student1> all = student1DAO.findAll();
        for (Student1 stu:all) {
            System.out.println(stu.getStuid() +"----"+ stu.getStuname()+"----"+stu.getSd().getStuaddress()+"---"+stu.getSd().getStuage());
        }
        System.out.println("end---------");*/
        /***********************************多表联查一对多*********************************************/
        IDeptDAO mapper = sqlSession.getMapper(IDeptDAO.class);
        List<Dept> all = mapper.findAllWithEmp();
        for (Dept dep:all) {
            Set<Emp> emps = dep.getEmps();
            for (Emp em:emps) {
                System.out.println(em.getDeptno()+"---"+em.getEmpno()+"---"+em.getEname()+"---"+em.getJob()+"---"+dep.getDname());
            }
        }
    }
}

标签:多表,String,bh,stu,Mybatis,import,联查,stuid,public
From: https://www.cnblogs.com/liangkuan/p/17429233.html

相关文章

  • 多表查询
    1.定义多表查询,也称为关联查询,指两个或更多个表一起完成查询操作前提条件:这些一起查询的表之间是有关系的(一对一、一对多),它们之间一定是有关联字段,这个关联字段可能建立了外键,也可能没有建立外键。比如:员工表和部门表,这两个表依靠“部门编号”进行关联1.1笛卡尔积的错误SE......
  • Maven+Eclipse+SpringMVC+MyBatis整合
    Maven+Eclipse+SpringMVC+MyBatis整合参考:SpringMVC3.0.5+Spring3.0.5+MyBatis3.0.4全注解实例详解(一):[url]http://www.blogjava.net/bolo/archive/2011/05/23/349655.html[/url]Maven最佳实践:版本管理:[url]http://juvenshun.iteye.com/blog/376422[/......
  • Spring MVC +MyBatis3全注解实例详解
    SpringMVC3.0.5+Spring3.0.5+MyBatis3.0.4全注解实例详解(一):[url]http://www.blogjava.net/bolo/archive/2011/05/23/349655.html[/url]如何配置Eclipse,Maven,Jetty并运行工程.如是使用Tomcat的话,改插件为:[url]http://tomcat.apache.org/maven-plugin......
  • mall学习01-整合mybatis
    1-mall学习01-概览学习链接2导入数据库#pwd#/Users/fwd/03-fwd_git/05-fly-higher-in-java/96-mall/note-mall#cp../opencode-mall/mall-learning/document/sql/mall.sql./安装mysql容器dockerrun--name=mall-mysql-it-p3306:3306-eMYSQL_ROOT_PASSWORD=root-dmys......
  • 编写javaweb用到的基本依赖,mybatis-config.xml代码,SqlSessionFactoryUtils.java
    这篇文章仅仅作为记录,供以后复制粘贴使用pom.xml<dependencies><!--Servlet--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</vers......
  • mybatis搭建
    1、导入依赖点击查看代码<!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.2.8</version></dependency>2......
  • MyBatis框架
    Mybatis使用流程1.引入mybatis.jar和mysql-connector.jar包先创建项目,然后引入jar包,方便接下来的操作2.XML配置文件书写参考文献:mybatis–MyBatis3|入门XML位置:在src目录下,与其他包平齐XML配置文件作用:包含MyBatis系统的核心设置,包括获取数据库连接实例的数据源(Dat......
  • Mybatis基本使用
    1、创建maven项目,导入mybatis依赖。<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.4.SNAPSHOOT</version></dependency>2、创建Mybati......
  • MyBatis-Plus 可视化代码生成器来啦,让你的开发效率大大提速!!
    前言在基于Mybatis的开发模式中,很多开发者还会选择Mybatis-Plus来辅助功能开发,以此提高开发的效率。虽然Mybatis也有代码生成的工具,但Mybatis-Plus由于在Mybatis基础上做了一些调整,因此,常规的生成工具生成的代码还有一些不太符合预期。而且对于多数据库的支持不是很好。因此,我们......
  • Mybatis源码分析
      代理模式 List<User>findByNameAndAge(@Param("name")Stringname,@Param("age")Integerage);<!--多个基本类型作为参数:方式1:arg*:arg0,arg1数字代表索引下标parameterType="list"方式2:param*:param1,par......