多表关联查询&动态sql

本文详细介绍了Mybatis中的多表关联查询,包括嵌套查询和注解方式的操作,如@Delete、@Select和@Insert。还深入探讨了Mybatis动态SQL,如if、where、trim、choose、set和foreach的用法。此外,文章讲解了特殊符号处理以及Mybatis的一级缓存和二级缓存机制,解释了缓存在提高查询性能中的作用,并展示了如何配置和使用二级缓存。
摘要由CSDN通过智能技术生成

多表关联查询

嵌套查询

将一次查询拆分成三次

查学生

StudentDao

public interface StudentDao {
   
  List<Student> findStudents();
  List<Student> findStudents1();
}

StudentMapper.xml

<resultMap id="studentmap1" type="Student">
    <id property="id" column="id"></id>
    <result property="number" column="no"></result>
    <result property="name" column="name"></result>
    <result property="gender" column="gender"></result>
    <result property="phone" column="phone"></result>
    <result property="address" column="address"></result>
    <result property="regTime" column="reg_time"></result>
    <association property="grade" javaType="Grade" select="findGradeById" column="gradeid">
    </association>
    <association property="user" javaType="User" select="findUserById" column="userid">
    </association>
</resultMap>
<select id="findStudents1" resultMap="studentmap1">
    SELECT s.id,
   s.no,
   s.name,
   s.gender,
   s.phone,
   s.address,
   s.reg_time,
   s.gradeid,
   s.userid
    FROM student s
</select>
<select id="findGradeById" parameterType="int" resultType="Grade">
        select id,name from grade where id=#{
   id}
</select>
<select id="findUserById" parameterType="int" resultType="User">
    select id,account from user where id=#{
   id}
</select>

TestStudent

public class TestStudent {
   
    public static void main(String[] args) {
   
        SqlSession sqlSession = mybatisUtil.getSqlSession();
        //生产接口的代理对象
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> students = studentDao.findStudents1();
        for(Student student:students)
        {
   
            System.out.println(student.getId());
            System.out.println(student.getGrade().getName());
            System.out.println(student.getUser().getAccount());
        }
        sqlSession.close();
    }
}

查年级

创建Grade

package mybatisPro.model;

import java.util.List;

public class Grade {
   
    private int id;
    private String name;
    private List<Student> students;//一个年级对应多个学生

    public  Grade()
    {
   
        System.out.println("Grade无参构造");
    }

    public List<Student> getStudents() {
   
        return students;
    }

    public void setStudents(List<Student> students) {
   
        this.students = students;
    }

    public int getId() {
   
        System.out.println("getId");
        return id;
    }

    public void setId(int id) {
   
        System.out.println("setId");
        this.id = id;
    }

    public String getName() {
   
        System.out.println("getName");
        return name;
    }

    public void setName(String name) {
   
        System.out.println("setName");
        this.name = name;
    }

    @Override
    public String toString() {
   
        return "Grade{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

创建GradeDao

package mybatisPro.Dao;

import mybatisPro.model.Grade;

import java.util.List;

public interface GradeDao {
   
        List<Grade> findGrades();//表一起查
    
    	List<Grade> findGrades1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值