myBatis: Cascade inquiry

Database mybatis unrelated name only, and mybatis framework has two tables associated with the lower classes and student, respectively, from below -many and many-to- associate inquiry under mybatis framework. Whether or many-to-many, need to configure resultMap in the .xml file.

  • New classes and class student class
package com.lmybatis.entity;

import lombok.Data;

import java.util.List;

@Data
public class Classes {
    private int id;
    private String name;
    private List<Student> students;
}
package com.lmybatis.entity;

import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;
    private Classes classes;
}

 

  • Create an interface ClassRepository for the Classes class, create an interface for the Student class StudentRepository
package com.lmybatis.repository;

import com.lmybatis.entity.Account;
import com.lmybatis.entity.Classes;

import java.util.List;

public interface ClassesRepository {
    public int save(Classes classes);
    public int update(Classes classes);
    public int deleteById(int id);  //删除
    public List<Classes> findAll(); //查询
    public Classes findById(int id);//查询
}
package com.lmybatis.repository;

import com.lmybatis.entity.Student;

public interface StudentRepository {
    public Student findById(int id);
}

 

  • Configuring ClassesRepository.xml and StudentRepository.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">
<!--namespace中放入接口路径-->
<mapper namespace="com.lmybatis.repository.ClassesRepository">
    <resultMap id="classesMap" type="com.lmybatis.entity.Classes">
        <id column="cid" property="id"></id>
        <result column="cname" property="name"></result>
        <collection property="students" ofType="com.lmybatis.entity.Student">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        </collection>
    </resultMap>
    <select id="findById" parameterType="int" resultMap="classesMap">
        select s.id,s.name,c.id as cid,c.name as cname from student s,classes c where c.id = #{id} and s.cid = c.id
    </select>
</mapper>
        <!--记住!要在config.xml中配置该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.lmybatis.repository.StudentRepository">

    <resultMap id="studentMap" type="com.lmybatis.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.lmybatis.entity.Classes">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </association>
    </resultMap>

    <select id="findById" parameterType="int" resultMap="studentMap">
        select s.id,s.name,c.id as cid,c.name as cname from student s,classes c where s.id = #{id} and s.cid = c.id
    </select>


    <resultMap id="studentMapLazy" type="com.lmybatis.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classes" javaType="com.lmybatis.entity.Classes" select="com.lmybatis.repository.ClassesRepository.findByIdLazy" column="cid"></association>
    </resultMap>

    <select id="findByIdLazy" parameterType="int" resultMap="studentMapLazy">
        select * from student where id = #{id}
    </select>

</mapper>
  •  Test, looking for all the id = 1 class in the classes table. Obviously, the output should Zhang and Li.
package com.lmybatis.test;

import com.lmybatis.entity.Account;
import com.lmybatis.entity.Classes;
import com.lmybatis.repository.AccountRepository;
import com.lmybatis.repository.ClassesRepository;
import com.lmybatis.repository.StudentRepository;
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;

public class test2 {
    public static void main(String[] args) {
        //mybatis核心接口和类
        InputStream inputStream = test1.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class);
//        System.out.println(studentRepository.findById(1));

        ClassesRepository classesRepository = sqlSession.getMapper(ClassesRepository.class);
        System.out.println(classesRepository.findById(1));
    }
}
  • Export

 

Published 126 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_36880027/article/details/104465430