我是靠谱客的博主 着急野狼,最近开发中收集的这篇文章主要介绍mybatis中集合嵌套查询和集合嵌套结果区别,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

嵌套查询是多条sql语句分开写并配置,嵌套结果是一条sql语句关联查询并配置,实质效果是一样的。嵌套语句的查询会导致数据库访问次数不定,进而有可能影响到性能。

1.创建2张表,建立主外键关系
在这里插入图片描述

2.建立实体类

package com.yw.test06;  
  
  
public class Class  
{  
    private int id;  
    private String name;  
      
      
    public int getId()  
    {  
        return id;  
    }  
    public void setId(int id)  
    {  
        this.id = id;  
    }  
    public String getName()  
    {  
        return name;  
    }  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
    @Override  
    public String toString()  
    {  
        return "Class [id=" + id + ", name=" + name + "]";  
    }  
      
      
}  
[html]  view plain  copy

package com.yw.test06;  
  
public class Student  
{  
    private int id;  
    private String name;  
    private int age;  
    private Class c;  
      
    public int getId()  
    {  
        return id;  
    }  
    public void setId(int id)  
    {  
        this.id = id;  
    }  
    public String getName()  
    {  
        return name;  
    }  
    public void setName(String name)  
    {  
        this.name = name;  
    }  
    public int getAge()  
    {  
        return age;  
    }  
    public void setAge(int age)  
    {  
        this.age = age;  
    }  
    public Class getC()  
    {  
        return c;  
    }  
    public void setC(Class c)  
    {  
        this.c = c;  
    }  
    @Override  
    public String toString()  
    {  
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", c=" + c + "]";  
    }  
  
      
      
}  

3.修改配置文件

<?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>  
    <properties resource="config.properties">  
    </properties>  
    <typeAliases>  
        <!-- <typeAlias type="com.yw.test06.StudentMapper" alias="Student" />  
        <typeAlias type="com.yw.test06.ClassMapper" alias="Class" /> -->  
        <package name="com.yw.test06"/>  
    </typeAliases>  
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC" />  
            <dataSource type="POOLED">  
                <property name="driver" value="${driver}" />  
                <property name="url" value="${url}" />  
                <property name="username" value="${username}" />  
                <property name="password" value="${password}" />  
            </dataSource>  
        </environment>  
    </environments>  
    <mappers>  
        <!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/> -->  
        <mapper resource="com/yw/test06/StudentMapper.xml" />  
    </mappers>  
  
</configuration>  

4.建立映射文件
1)嵌套查询

<?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.yw.test07.StudentMapper">  
  
    <select id="selectStudent" resultMap="studentResult">  
        SELECT * FROM student s  
        WHERE s.ID = #{id}  
    </select>  
    <resultMap type="Student" id="studentResult">  
        <association property="c" column="c_id" javaType="Class"  
            select="selectClass" />  
    </resultMap>  
  
    <select id="selectClass" resultType="Class">  
        SELECT * FROM class WHERE ID= #{id}  
    </select>  
  
</mapper>  

2)嵌套结果

<?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.yw.test06.StudentMapper">  
  
      
    <resultMap id="studentResult" type="com.yw.test06.Student">  
        <id property="id" column="id"/>  
        <result property="name"  column="name"/>  
        <result property="age"  column="age"/>  
  
        <association property="c" resultMap="classResult"  
            javaType="Class"></association>  
  
    </resultMap>  
    <resultMap type="com.yw.test06.Class" id="classResult">  
        <id property="id" column="id"/>  
        <result property="name" column="name"/>  
    </resultMap>  
   
    <select id="selectStudent" resultMap="studentResult">   
        SELECT s.id,c.id,s.name,s.age,c.name from student s  left join class c on c.id=s.c_id where s.id=#{id}  
    </select>  
  
  
  
</mapper>  

5.创建测试类

package com.yw.test06;  
  
import java.io.IOException;  
import java.io.InputStream;  
  
import org.apache.ibatis.io.Resources;  
import org.apache.ibatis.session.SqlSession;  
import org.apache.ibatis.session.SqlSessionFactory;  
import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  
public class Test01  
{  
    public static void main(String[] args) throws IOException  
    {  
          
        String resource = "com/yw/test06/mybatis-config.xml";  
        InputStream inputStream = Resources.getResourceAsStream(resource);  
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  
          
        SqlSession session = sqlSessionFactory.openSession(false);  
        try {  
          Student user = (Student) session.selectOne("com.yw.test06.StudentMapper.selectStudent", 1);  
          System.out.println(user);           
              
        } finally {  
          session.close();  
        }  
    }  
}  

引用链接:https://blog.csdn.net/suchahaerkang/article/details/80262626

最后

以上就是着急野狼为你收集整理的mybatis中集合嵌套查询和集合嵌套结果区别的全部内容,希望文章能够帮你解决mybatis中集合嵌套查询和集合嵌套结果区别所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部