Mybaits之多表查询(联合查询、嵌套查询)

Mybatis之多表查询 MyBatis 是一个优秀的持久层框架,它提供了强大的支持来执行数据库操作,包括多表查询。多表查询是指从多个数据库表中检索数据的过程,这在实际的应用中非常常见。 阅读详情

多表联合查询

(一) 多表联合查询概述

在开发过程中单表查询不能满足项目需求分析功能,对于复杂业务来讲,关联的表有几张,甚至几十张并且表与表之间的关系相当复杂。为了能够实业复杂功能业务,就必须进行多表查询,在mybatis中提供了多表查询的结果时映射标签,可以实现表之间的一对一、一对多、多对一、多对多关系映射。

(二) MyBatis多表查询之一对一

1. 构建数据库表:person(个人表) IdCard(身份证表)
#person表
CREATE TABLE person(
p_id INT NOT NULL AUTO_INCREMENT,
p_name VARCHAR(30),
PRIMARY KEY(p_id)
);

#IdCard表
CREATE TABLE idcard(
c_id INT NOT NULL AUTO_INCREMENT,
c_cardno VARCHAR(18),
c_uselife DATE,
c_person_id INT NOT NULL,
PRIMARY KEY(c_id),
FOREIGN KEY(c_person_id) REFERENCES person(p_id),
UNIQUE KEY(c_cardno),
UNIQUE KEY(c_person_id)
);

INSERT INTO person(p_name) VALUES('张三'),('李四');

INSERT INTO idcard(c_cardno,c_uselife,c_person_id)
VALUES('110112199012127821','2029-10-10',1);
INSERT INTO idcard(c_cardno,c_uselife,c_person_id)
VALUES('120114199911103491','2030-12-01',2);

2.准备项目环境
1) 构建maven项目,添加依赖
<dependencies>
<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.4.6</version>
</dependency>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.17</version>
</dependency>
</dependencies>
2)编写框架配置文件sqlMapConfig.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>
<!-- 配置 mybatis的环境 -->
<environments default="development">
<!-- 配置环境 -->
<environment id="development">
   <!-- 配置事务的类型 -->
   <transactionManager type="JDBC"></transactionManager>
   <!-- 配置连接数据库的信息:用的是数据源【连接池】-->
   <dataSource type="POOLED">
       <property name="driver" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/mybatis002"/>
       <property name="username" value="root"/>
       <property name="password" value="root"/>
   </dataSource>
</environment>
</environments>
<mappers>
<package name="cn.offcn.dao"></package>
</mappers
</configuration>
3.方式一:嵌套结果方式
1)创建数据模型Person对象,IdCard对象
public class Person {
    private Integer id;
    private String name;
    private Idcard idcard;
}

public class Idcard {
    private Integer id;
    private String cardno;
    private Date uselife;
}
2)定义持久层接口PersonMapper
public interface PersonDao{

/**
    * 根据id查询person对象
    */
    Person getPersonById1(int i);
}
3)定义 PersonMapper.xml 文件中的查询配置信息
<resultMap id="BaseResultMap" type="Person">
        <id column="p_id" property="id"></id>
        <result column="p_name" property="name"></result>
    </resultMap>

    <resultMap id="PersonResultMap1" type="Person" extends="BaseResultMap">
        <association property="idcard" javaType="Idcard">
            <id column="c_id" property="id"></id>
            <result column="c_cardno" property="cardno"></result>
            <result column="c_uselife" property="uselife"></result>
        </association>
    </resultMap>

     <!--  根据部门id查找部门  以及所在部门所有员工的所有信息【嵌套结果查询,联合查询】  -->
    <select id="getPersonById1" parameterType="int" resultType="Person" resultMap="PersonResultMap1">
        select p.*,c.* from person p,idcard c where p.p_id=c.c_person_id and p.p_id=#{id}
    </select>
4)创建 PersonTest 测试类
 @Test
    // 根据人的id查询人与信息 和 身份证信息 【嵌套结果查询,连表查询】
    public void getPersonById1(){
        SqlSession session = MyBatisUtils.getSession();
        PersonMapper mapper = session.getMapper(PersonMapper.class);
        Person person =  mapper.getPersonById1(1);
        // 输出
        System.out.println(person);
        System.out.println(person.getIdcard().toString());
        MyBatisUtils.close(session);
    }
4.方式二:嵌套查询方式
1)创建数据模型Person对象,IdCard对象
public class Person {
    private Integer id;
    private String name;
    private Idcard idcard;
}

public class Idcard {
    private Integer id;
    private String cardno;
    private Date uselife;
}
2)定义持久层接口PersonMapper
public interface IdCardDao{
    /**
    * 根据c_person_id查询IdCard对象
    */
   Person getPersonById2(int i);
}
3)定义 PersonMapper.xml 文件中的查询配置信息
<!--  根据人的id查询人与信息 和 身份证信息 【嵌套查询,分表查询】  -->
    <select id="getPersonById2" parameterType="int" resultType="Person" resultMap="PersonResultMap2">
        select * from person where p_id=#{id}
    </select>

    <resultMap id="BaseResultMap" type="Person">
        <id column="p_id" property="id"></id>
        <result column="p_name" property="name"></result>
    </resultMap>
    
    <resultMap id="PersonResultMap2" type="Person" extends="BaseResultMap">
        <association property="idcard" javaType="Idcard" column="p_id"
                     select="com.chenshuang.mapper.IdcardMapper.getIdcardById">
        </association>
    </resultMap>
4)定义持久层接口IdcardMapper
public interface IdcardMapper {
    Idcard getIdcardById(int id);
}
5)定义 IdcardMapper.xml 文件中的查询配置信息
<mapper namespace="com.chenshuang.mapper.IdcardMapper">

    <resultMap id="BaseResultMap" type="Idcard">
        <id column="c_id" property="id"></id>
        <result column="c_cardno" property="cardno"></result>
        <result column="c_uselife" property="uselife"></result>
    </resultMap>
    <select id="getIdcardById" parameterType="int" resultType="Idcard" resultMap="BaseResultMap">
        select * from idcard where c_id=#{id}
    </select>
</mapper>
6)PersonTest 类中加入测试方法
 @Test
    // 根据人的id查询人与信息 和 身份证信息 【嵌套查询,分表查询】
    public void getPersonById2(){
        SqlSession session = MyBatisUtils.getSession();
        PersonMapper mapper = session.getMapper(PersonMapper.class);
        Person person =  mapper.getPersonById2(1);
        System.out.println(person);
        System.out.println(person.getIdcard().toString());
        MyBatisUtils.close(session);
    }

(三) MyBatis实现一对多查询

1.创建数据库表:department(部门表),employee(员工表)同时设定部门和员工表的关系
CREATE TABLE department(
d_id INT NOT NULL AUTO_INCREMENT,
d_name VARCHAR(100),
PRIMARY KEY(d_id)
);

CREATE TABLE employee(
e_id INT NOT NULL AUTO_INCREMENT,
e_name VARCHAR(30),
e_gender VARCHAR(6),
e_age INT,
e_depart_id INT,
PRIMARY KEY(e_id),
FOREIGN KEY(e_depart_id) REFERENCES department(d_id)
);
2.方式一:嵌套结果,连表查询的方式
1)创建数据模型department和employee
public class Department {
    private Integer id;
    private String name;
    private List<Employee> employees;
}

public class Employee {
    private Integer id;
    private String name;
    private String gender;
    private Integer age;
    private Department depart;
}
2)部门持久层DepartmentMapper 接口中加入查询方法
public interface DepartmentMapper {	
    Department getDepartmentById1(int id);
    Department getDepartmentById2(int id);
}
3)部门持久层 DepartmentMapper.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="cn.offcn.mapper.DepartmentDao">
 <select id="getDepartmentById" resultMap="DepartemntResultMap2">
     SELECT d.*,e.* FROM department d, employee e WHERE
     e.e_depart_id=d.d_id AND d.d_id=#{id}
 </select>
<resultMap id="DepartemntResultMap" type="Department">
    <id column="d_id" property="id"></id>
    <result column="d_name" property="name"></result>
    <collection property="emps" ofType="Employee">
         <id column="e_id" property="id"></id>
         <result column="e_name" property="name"></result>
         <result column="e_gender" property="gender"></result>
         <result column="e_age" property="age"></result>
    </collection>
</resultMap>
collection:当属性为集合时,使用collection标签进行映射。
4)测试方法
 @Test
 public void testGetDepartentById() throws  Exception{
     //获取SqlSession对象
     SqlSession session = MyBatisUtils.getSession();
     //调用SqlSession 创建 UserDao接口的代理对象
     DepartmentDao departmentDao = session.getMapper(DepartmentDao.class);
     Department dept=departmentDao.getDepartentById(1);
     //打印
     System.out.println(dept);
     //提交事务
     session.commit();
     //关闭连接
     MyBatisUtils.close(session);
 }
3.方式二:嵌套查询,分表查询的方式
1)定义员工的持久层接口EmployeeMapper
public interface EmployeeDao {
public List<Employee> getEmployeeByDepartId(int id);
}
2)定义DepartmentMapper.xml 文件中的查询配置信息
<select id="getEmployeeByDepartId" resultType="Employee">
    select e_id id,e_name name,e_gender gender,e_age age
    from employee where e_depart_id=#{id}
</select>
3)定义DepartmentMapper.xml配置文件
<select id="getDepartmentById" resultMap="DepartemntResultMap">
    select * from department where d_id=#{id}
 </select>
<resultMap id="DepartemntResultMap" type="Department">
     <id column="d_id" property="id"></id>
     <result column="d_name" property="name"></result>
     <collection property="emps" ofType="Employee" column="d_id"
              select="cn.offcn.mapper.EmployeeMapper.getEmployeeByDepartId">
     </collection>
</resultMap>
此处变为单表查询,使分表查询方式进行查询。
4)定义 EmployeeMapper.xml 文件中的查询配置信息
public interface EmployeeMapper {
    List<Employee> getEmployeeById(int i);
}
5)定义 EmployeeMapper.xml 配置文件
    <resultMap id="BaseResultMap" type="employee">
        <id column="e_id" property="id"></id>
        <result column="e_name" property="name"></result>
        <result column="e_gender" property="gender"></result>
        <result column="e_age" property="age"></result>
    </resultMap>
    
<!--
        property 设置本类型中的复杂属性
        javaType 复杂数据的数据类型
        column 表示取sql结果集中的 指定字段
        select 表示调查的查询 如果本次查询和调查的查询没有在同一个命令空间中,调用时候一定要加命名空间
-->
    <resultMap id="EmployeeResultMap1" type="employee" extends="BaseResultMap">
        <association property="depart" javaType="Department" column="e_depart_id"
                     select="com.chenshuang.mapper.DepartmentMapper.getDepartmentById"></association>
    </resultMap>
    <!-- 根据员工id查找员工以及所在部门的所有信息【嵌套查询,分表查询】-->
    <select id="getEmployeeById2" parameterType="int" resultType="Employee" resultMap="EmployeeResultMap1">
        select * from employee where e_id=#{id}
    </select>
6)测试方法
@Test
public void testGetDepartentById() throws  Exception{
//获取SqlSession对象
SqlSession session = MyBatisUtils.getSession();
//调用SqlSession 创建 UserDao接口的代理对象
DepartmentDao departmentDao = session.getMapper(DepartmentDao.class);
Department dept=departmentDao.getDepartentById(1);
//打印
System.out.println(dept);
//提交事务
session.commit();
//关闭连接
MyBatisUtils.close(session);
}

(四) MyBatis实现多对一查询

1.创建数据库表:department(部门表),employee(员工表)同时设定部门和员工表的关系
CREATE TABLE department(
d_id INT NOT NULL AUTO_INCREMENT,
d_name VARCHAR(100),
PRIMARY KEY(d_id)
);

CREATE TABLE employee(
e_id INT NOT NULL AUTO_INCREMENT,
e_name VARCHAR(30),
e_gender VARCHAR(6),
e_age INT,
e_depart_id INT,
PRIMARY KEY(e_id),
FOREIGN KEY(e_depart_id) REFERENCES department(d_id)
);
2.方式一:嵌套结果,连表查询的方式
1)创建数据模型department和employee
public class Department {
    private Integer id;
    private String name;
    private List<Employee> employees;
}

public class Employee {
    private Integer id;
    private String name;
    private String gender;
    private Integer age;
    private Department depart;
}
2)员工持久层EmployeeMapper接口中加入查询方法
public interface EmployeeMapper {
    Employee getEmployeeById1(int i);
}
3)部门持久层 EmployeeMapper.xml映射文件配置
  <resultMap id="BaseResultMap" type="employee">
        <id column="e_id" property="id"></id>
        <result column="e_name" property="name"></result>
        <result column="e_gender" property="gender"></result>
        <result column="e_age" property="age"></result>
    </resultMap>

<!--  property 设置本类型中的复杂属性
        javaType 复杂数据的数据类型   -->
    <resultMap id="EmployeeResultMap" type="employee" extends="BaseResultMap">
        <association property="depart" javaType="Department">
            <id column="d_id" property="id"></id>
            <result column="d_name" property="name"></result>
        </association>
    </resultMap>

    <!-- 根据员工id查找员工以及所在部门的所有信息【嵌套结果查询,联合查询】   -->
    <select id="getEmployeeById1" parameterType="int" resultType="Employee" resultMap="EmployeeResultMap">
        SELECT e.*,d.* from employee e,department d WHERE e.e_depart_id = d.d_id AND e.e_id=#{id}
    </select>
4)测试方法
@Test
//  根据员工id查找员工以及所在部门的所有信息【嵌套结果查询,联合查询】
public void getEmployeeById1(){
    SqlSession session = MyBatisUtils.getSession();
    EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
    Employee employee = mapper.getEmployeeById1(1);
    System.out.println(employee.toString()+"部门---:"+employee.getDepart().toString());
    MyBatisUtils.close(session);
}
3.方式二:嵌套查询,分表查询的方式
1)定义员工的持久层接口DepartmentMapper
public interface DepartmentMapper {
    Department getDepartmentById(int id);
}
2)员工持久层EmployeeMapper接口中加入查询方法
public interface EmployeeMapper {

    Employee getEmployeeById2(int i);

    List<Employee> getEmployeeById(int i);
}
3)定义EmployeeMapper.xml配置文件
    <resultMap id="BaseResultMap" type="employee">
        <id column="e_id" property="id"></id>
        <result column="e_name" property="name"></result>
        <result column="e_gender" property="gender"></result>
        <result column="e_age" property="age"></result>
    </resultMap>

<!--
        property 设置本类型中的复杂属性
        javaType 复杂数据的数据类型
        column 表示取sql结果集中的 指定字段
        select 表示调查的查询 如果本次查询和调查的查询没有在同一个命令空间中,调用时候一定要加命名空间
-->
    <resultMap id="EmployeeResultMap1" type="employee" extends="BaseResultMap">
        <association property="depart" javaType="Department" column="e_depart_id"
                     select="com.chenshuang.mapper.DepartmentMapper.getDepartmentById"></association>
    </resultMap>
    <!-- 根据员工id查找员工以及所在部门的所有信息【嵌套查询,分表查询】-->
    <select id="getEmployeeById2" parameterType="int" resultType="Employee" resultMap="EmployeeResultMap1">
        select * from employee where e_id=#{id}
    </select>
4)定义 DepartmentMapper文件中的查询配置信息
public interface DepartmentMapper {
    Department getDepartmentById(int id);
}
5)定义DepartmentMapper.xml 配置文件
    <resultMap id="BaseResultMap" type="Department">
            <id column="d_id" property="id"></id>
            <result column="d_name" property="name"></result>
        </resultMap>

    <select id="getDepartmentById" parameterType="int" resultType="Department" resultMap="BaseResultMap">
        select * from department where d_id=#{id}
    </select>
6)测试方法
 @Test
    // 根据员工id查找员工以及所在部门的所有信息【嵌套查询,分表查询】
    public void getEmployeeById2(){
        SqlSession session = MyBatisUtils.getSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee employee = mapper.getEmployeeById2(1);
        System.out.println(employee.toString()+"部门---:"+employee.getDepart().toString());
        MyBatisUtils.close(session);
    }

(五) MyBatis实现多对多查询

1.创建数据库表:student(学生表),teacher(教师表) 同时设定教师和学生表的关系 多对多
CREATE TABLE teacher(
		t_id INT NOT NULL auto_increment,
		t_name VARCHAR(30),
		PRIMARY KEY(t_id)
);

create TABLE student_teacher(
	st_sid INT not NULL,
	st_tid INT NOT NULL,
	PRIMARY KEY(st_sid,st_tid),
	FOREIGN KEY(st_sid) REFERENCES student(s_id),
	FOREIGN KEY(st_tid) REFERENCES teacher(t_id)
);
2.方式一:嵌套结果,连表查询的方式
1)创建数据模型Teacher和 Student 和 中间类 studentTeacher
public class Student {
    private Integer id;
    private  String name;
    private String gender;
    private Integer age;
    private String birthday;
    private List<StudentTeacher> studentTeachers;
}

public class Teacher {
    private Integer id;
    private String name;
    private List<StudentTeacher> studentTeachers;
}

public class StudentTeacher {
    private Integer sid;
    private Integer tid;
    private Student student;
    private Teacher teacher;
}
2)学生持久层StudentMapper接口中加入查询方法
public interface StudentMapper {

    Student getStudentById1(int i);

}
3)学生持久层StudentMapper.xml映射文件配置
   <!--  根据学生id查 学生信息 和老师的信息 【嵌套结果查询,联合查询】  -->
    <select id="getStudentById1" parameterType="int" resultType="Student" resultMap="StudentResultMap1">
        SELECT s.*,t.*,st.*
        FROM student s,teacher t,student_teacher st
        where s.id=st.st_sid AND t.t_id=st.st_tid AND s.id=#{id}
    </select>

    <resultMap id="StudentResultMap1" type="Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="gender" property="gender"></result>
        <result column="age" property="age"></result>
        <result column="s_birthday" property="birthday"></result>
        <collection property="studentTeachers" ofType="StudentTeacher" >
            <result column="st_sid" property="sid"></result>
            <result column="st_tid" property="tid"></result>
            <association property="teacher" javaType="Teacher">
                <id column="t_id" property="id"></id>
                <result column="t_name" property="name"></result>
            </association>
        </collection>
    </resultMap>
4)测试方法
@Test
    // 根据学生id查 学生信息 和老师的信息 【嵌套结果查询,联合查询】
    public void getStudentById1(){
        SqlSession session = MyBatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.getStudentById1(3);

        System.out.println("=========   输出:  ==================");
        System.out.println(student);
        int i=1;
        for (StudentTeacher st : student.getStudentTeachers()) {
            System.out.println("老师"+i+":"+st.getTeacher().getName());
            i++;
        }
        System.out.println("=====================================");
        MyBatisUtils.close(session);
    }
3.方式二:嵌套查询,分表查询的方式
1)创建数据模型Teacher和 Student 和 中间类 studentTeacher
public class Student {
    private Integer id;
    private  String name;
    private String gender;
    private Integer age;
    private String birthday;
    private List<StudentTeacher> studentTeachers;
}

public class Teacher {
    private Integer id;
    private String name;
    private List<StudentTeacher> studentTeachers;
}

public class StudentTeacher {
    private Integer sid;
    private Integer tid;
    private Student student;
    private Teacher teacher;
}
2)学生持久层StudentMapper接口中加入查询方法
public interface StudentMapper {

    Student getStudentById2(int i);

}
3)定义StudentMapper.xml配置文件
     <!--  根据学生id查 学生信息 和老师的信息 【嵌套查询,分表查询】  -->
    <select id="getStudentById2" parameterType="int" resultType="Student" resultMap="StudentResultMap2">
        select * from student where id=#{id}
    </select>

    <resultMap id="BaseResultMap" type="Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <result column="gender" property="gender"></result>
        <result column="age" property="age"></result>
        <result column="s_birthday" property="birthday"></result>
    </resultMap>

    <resultMap id="StudentResultMap2" type="Student" extends="BaseResultMap">
        <collection property="studentTeachers" ofType="StudentTeacher" column="id"
                    select="com.chenshuang.mapper.StudentTeacherMapper.getStudentTeacherBySid"></collection>
    </resultMap>
4)定义 StudentTeacherMapper文件中的查询配置信息
public interface StudentTeacherMapper {
    StudentTeacher getStudentTeacherBySid(int sid);
}
5)定义StudentTeacherMapper.xml 配置文件
<resultMap id="BaseResultMap" type="StudentTeacher">
        <result column="st_sid" property="sid"></result>
        <result column="st_tid" property="tid"></result>
</resultMap>

<resultMap id="StudentTeacherResultMap" type="StudentTeacher" extends="BaseResultMap">
        <association property="teacher" javaType="Teacher" column="st_tid"
                     select="com.chenshuang.mapper.TeacherMapper.getTeacherById">
        </association>
</resultMap>

<select id="getStudentTeacherBySid" parameterType="int" resultType="StudentTeacher" resultMap="StudentTeacherResultMap">
        select * from student_teacher where st_sid=#{id}
</select>
6)定义 TeacherMapper文件中的查询配置信息
public interface TeacherMapper {
    Teacher getTeacherById(int id);
}
7)定义 TeacherMapper.xml 配置文件
<resultMap id="BaseResultMap" type="Teacher">
    <id column="t_id" property="id"></id>
    <result column="t_name" property="name"></result>
</resultMap>
    
<select id="getTeacherById" resultType="Teacher" parameterType="int" resultMap="BaseResultMap">
   select * from teacher where t_id=#{id}
</select>
6)测试方法
@Test
    // 根据学生id查 学生信息 和老师的信息 【嵌套查询,分表查询】
    public void getStudentById2(){
        SqlSession session = MyBatisUtils.getSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.getStudentById2(3);

        System.out.println("=========   输出:  ==================");
        System.out.println(student);
        int i=1;
        for (StudentTeacher st : student.getStudentTeachers()) {
            System.out.println("老师"+i+":"+st.getTeacher().getName());
            i++;
        }
        System.out.println("=====================================");
        MyBatisUtils.close(session);
    }

MyBatis 多表关联查询 MyBatis 多表关联查询:一对多查询, 多对一查询, 自连接查询: 使用多对一的方式实现自连接, 使用一对多的方式实现自连接, 多对多查询 阅读详情

相关推荐

SSM开发(十一) mybatis关联关系多表查询(嵌套查询,举例说明)

当对数据库的操作涉及到多张表,这在面向对象语言如Java中就涉及到了对象与对象之间的关联关系。针对多个表之间的操作,MyBatis提供了关联映射,通过关联映射就可以很好的处理对象与对象之间的关联关系。一对多,一对一,多对多(可以使用两个一对多实现)在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多;一对一:在任意一方引入对方主键作为外键;一对多:在“多”的一方,添加“一”的一方的主键作为外键;

我的E家 717

Mybatis---多表联合查询(1)

Mybatis---多表联合查询(1)前言测试准备一、一对一新建方法添加关联测试二、一对多新建实体类添加方法添加关联测试 前言 在开发过程中单表查询不能满足项目需求分析功能,对于复杂业务来讲,关联的表有几张,甚至几十张并且表与表之间的关系相当复杂。为了能够实业复杂功能业务,就必须进行多表查询,在mybatis中提供了多表查询的结果时映射标签,可以实现表之间的一对一、一对多、多对多关系映射 测试准备 案例所需数据库脚本 SET FOREIGN_KEY_CHECKS=0; -- -----------

weixin_46816383的博客 6657

C# 基于NetStandard.Opc.Ua实现高效OPC UA数据订阅与同步读写

本文详细介绍了在C#中利用NetStandard.Opc.Ua库高效实现OPC UA数据订阅与同步读写的方法。通过实战代码演示了如何建立连接、配置订阅管理器、优化监控项分组,并对比了同步与异步读写的适用场景,帮助工业软件开发者构建稳定可靠的数据采集客户端,提升工业物联网应用开发效率。

weixin_29201859的博客 227

Mybatis多表联查的几种办法

开发过程中,对数据库多表连接查询是常规操作。Mybatis(plus)在多表连接查询上提供了很好的支持。通常,表与表之间存在四种映射关系:一对一映射、多对一映射、一对多映射、多对多映射。在处理的过程中,多对一和一对一映射一般采用同一种操作方式,一对多和多对多映射一般采用同一种操作方式。而且,对于Mybatis的运用,我们通常使用xml的方式。因此,本文主要介绍xml方式下一对一多表联查和一对多多表联查。

weixin_51667449的博客 2万+

Virtualbox “Kernel driver not installed (rc=-1908)”问题全面解析与修复指南

本文全面解析了Virtualbox出现'Kernel driver not installed (rc=-1908)'错误的原因及修复方法。详细介绍了如何通过重新安装virtualbox-dkms、处理Secure Boot特殊情况等步骤解决该问题,并提供了预防措施与最佳实践,帮助用户避免类似错误再次发生。

weixin_30352645的博客 528

“迎圣诞,拿大奖” 活动赛题 Web-SQLi

1. php中sprintf()函数漏洞原理及其利用; 2. mysql盲注语法及python脚本; 3. sqlmap的需要进一步学习;

qq_34106499的博客 674

MyBatis嵌套查询解析

Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-many、many-to-one、one-to-one。而是只有两种association(一)、collection(多),表现很简洁。下面通过一个实例,来展示一下Mybatis对于常见的一对多和多对一关系复杂映射是怎样处理的。以最简单的用户表订单表这个最简单的一对多做示例:对应的JavaBean:User:publ...

qq541005640的博客 353

【ESP32-IDF笔记】20-配置以太网网络(W5500)

基于ESP32IDF和官方的ethernet驱动,配置W5500以太网驱动芯片,实现ESP32的有线网络

别看了,就这些信息 3471

MyBatis关联查询简单实验:一对一与一对多与多对一详解

本文介绍了MyBatis中处理三种关联查询的方法:一对一、一对多和多对一。对于一对一关系(如用户与详细信息),使用<association>标签实现表关联;一对多关系(如用户与借阅图书)通过<collection>标签处理;多对一关系(如学生与班级)同样使用<association>。文章详细说明了XML映射文件的两种写法(嵌套和关联),并提供了完整的Java类定义、SQL语句示例和测试代码。特别强调了字段别名的重要性以避免主键冲突,并建议为不同查询创建独立SqlSess

H972002的博客 600

Python入门之机器学习(非常详细)篇幅拉满,一般人看不完!

什么是机器学习?监督学习 supervised learning;非监督学习 unsupervised learning;半监督学习 semi-supervised learning;强化学习 reinforcement learning;监督学习是不断向计算机提供数据(特征),并告诉计算机对应的值(标签),最后通过大量的数据,让计算机自己学会判断和识别。例如Google Photo,你在APP中输入狗,这时就会弹出与狗相关的图片,这背后使用的算法就是监督学习。

xiaolinyui的博客 2万+

杰理AC696N系列芯片,烧录的时候烧录器连接上芯片之后电脑显示无法识别该设备,该怎么解决?

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1705

Mybaits-plus实战(三)

1. Mybaits-plus实战(三) 1.1. 特殊使用规则 1.1.1. Model逻辑删除 数据库对应实体类,继承Model类可以实现AR模式的sql语句操作,但这里需要注意的是,对逻辑删除,官方说明需要实现如下重写方法才能生效,也就是使用实例化对象.deleteById() @Override protected Serializable pkVal() { ...

weixin_34283445的博客 567

【EMC专题】为什么芯片规格书ESD性能中看不到MM(机器放电模型)

器件级ESD包括三种类型,HBM人体放电模型、MM机械放电模型和CDM元件充电模型。但在芯片规格书中为什么只见到HBM模型与CDM模型,却没有看到MM模型的数据?

一点硬件 450

MyBatis详解(5)--MyBatis 高级查询

MyBatis详解5--MyBatis 高级查询需求缘起1.一对一查询1.1 创建一个订单表1.2 创建订单实体类1.3 编写接口 OrdersMapper.java1.3 编写 OrdersMapper.xml1.4 在配置文件 mybatis-configuration.xml 中注册 OrderMapper.xml 文件1.5 测试2.一对多查询2.1 添加 orders 属性2.2 创建 ...

kk鲁 342

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

mybatis 级联查询

文章目录Association元素select方式结果集嵌套式Collection元素 Association元素 Association元素处理“has-one”(一对一)这种类型关系。联合映射与其它的结果集映射工作方式差不多,指定property、column、javaType(通常MyBatis会自动识别)、jdbcType(如果需要)、typeHandler。 不同的地方是您需要告诉MyBatis 如何加载一个联合查询。MyBatis使用两种方式来加载: ·Nested Select:通过执行另一个

oThrowsException的博客 421

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

MyBatis笔记06 - 一对多关联查询

6.2. 一对多关联 1). 提出需求 根据classId查询对应的班级信息,包括学生,老师 2). 创建表和数据:CREATE TABLE student( s_id INT PRIMARY KEY AUTO_INCREMENT, s_name VARCHAR(20), class_id INT ); INSERT INTO student(s_name, cla

一转身就是一辈子 357

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

关于MyBatis你需要了解的知识

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。它使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJO(Plain Old Java Object)映射成数据库中的记录。例如,在一个简单的用户管理系统中,我们可以使用 MyBatis 通过一个简单的接口方法来查询用户信息,而在后台它会自动将数据库表中的字段映射到 Java 的用户对象的属性上。

qq_65709394的博客 827

Mybatis关联查询(嵌套查询)

上一篇文章介绍了基于Mybatis对数据库的增、删、改、查。这一篇介绍下关联查询(join query)。 三张表:user article blog 表的存储sql文件: /* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50620 Source ...

weixin_30362233的博客 90

mybatis 详解(七)------一对一、一对多、多对多

前面几篇博客我们用mybatis能对单表进行增删改查操作了,也能用动态SQL书写比较复杂的sql语句。但是在实际开发中,我们做项目不可能只是单表操作,往往会涉及到多张表之间的关联操作。那么我们如何用 mybatis 处理多表之间的关联操作呢?请看本篇博客详解。  本篇详细代码:http://pan.baidu.com/s/1eSzmst8 密码:3n3o1、一对一  我们以用户表 user 和订单...

SKlonglong的博客 295

mybatis ---- 级联查询 一对多 (集合映射)

关联有嵌套查询和嵌套结果两种方式,本文是按照嵌套结果这种方式来说明的 上一章介绍了多对一的关系,用到了,这是一个复杂类型的关联。我们选择一个示例来回顾下,比如:一个博客有一个用户,关联映射就工作于这种结果之上。首先看下,我们在本文中要用到的表结构字段: [sql] view plain copy 博客   blog :  id  titl

热爱我的热爱 519

Mybatis

目录 一、初识Mybatis (一)Mybatis的原理 (二)Mybatis的配置 二、基于SqlSession的单表增删改查 (一)无参数的 (二)有参数的 (三)SqlSession总结 三、基于Sql动态代理的单表增删改查 四、MybatisSql语句的动态拼接 五、Automapping自动注入和自定义注入 (一)Automapping自动注入 (二)自定义注入 ...

Silly 1125

mybatis foreach嵌套if_MyBatis常见,常用知识点

阅读文本大概需要15分钟。1、什么是Mybatis?(1)Mybatis是一个半ORM(对象关系映射)框架,它内部封装了JDBC,开发时只需要关注SQL语句本身,不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。程序员直接编写原生态sql,可以严格控制sql执行性能,灵活度高。(2)MyBatis 可以使用 XML 或注解来配置和映射原生信息,将 POJO映射...

weixin_39584549的博客 1142

【mybatis】mybatis面试题

【mybatis】mybatis面试题 mybatis的基本工作流程 1.读取配置文件,配置文件右数据库连接信息和Mapper映射文件或者Mapper包 2.有了这些信息就能创建SqlSessionFactorySqlSessionFactory的生命周期是程序级,程序运行的时候建立起来,程序结束的时候消亡 3.SqlSessionFactory建立S...

weixin_33949359的博客 2668

mybatis面试题

1.Mybatis比IBatis比较大的几个改进是什么         a.有接口绑定,包括注解绑定sql和xml绑定Sql ,         b.动态sql由原来的节点配置变成OGNL表达式,         c. 在一对一,一对多的时候引进了association,在一对多的时候引入了collection            节点,不过都是在resultMap里面配置 2

andysylar的博客 636
上一篇: SpringMVC
下一篇: 什么是依赖注入(DI)
皮卡丘不断更
博客等级 码龄5年 111粉丝 141原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮卡丘不断更

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值