Java Learning MyBatis (2)


First attach the learning address Nan Ge teach you to learn java


1. Continue to learn MyBatis with a learning heart today, and then follow the blogger to learn one-to-many cascading query (find class by students). Come on, attach the code to record your study notes.

  • First attach the file structure:
    File structure
  • New tables have been added to the database:
    class table
    student table

This is a one-to-many relationship query, using two tables.

  • The next step is to define the data of the table in the corresponding database!
    • Student class:
package com.southwind.entity;

import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;
    private Classer classer;

    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;
    }
}

  • Next is the Classer class:
package com.southwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Classer {
    private int id ;
    private String name;
    private List<Student> students;

    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;
    }
}

  • Create an interface StudentRepository
package com.southwind.repository;

import com.southwind.entity.Student;

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

  • Create an XML file to operate:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 一对多的级联查询 -->
<mapper namespace="com.southwind.repository.StudentRepository">
<!--    查询语句-->
    <select id="findById" parameterType="int" resultMap="studentMap">
        select s.id,s.name,c.id as cid,c.name as cname
        from student s,class c
        where s.id = #{id} and s.cid = c.id
    </select>
<!--    结果集映射 间接映射 专门处理不能直接映射的情况-->
<!--    column 数据库字段 -->
<!--    property 类里的属性-->
    <resultMap id="studentMap" type="com.southwind.entity.Student">
        <id column="id" property="id"></id>
        <result column="name" property="name"></result>
        <association property="classer" javaType="com.southwind.entity.Classer">
            <id column="cid" property="id"></id>
            <result column="cname" property="name"></result>
        </association>
    </resultMap>
</mapper>
  • Then create a test class:
package com.southwind.test;

import com.southwind.repository.StudentRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test_3 {
    public static void main(String[] args) {
        //        加载配置文件
        InputStream inputStream = Test.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));
        sqlSession.close();
    }
}

  • Don't forget to register the XML file
     <mapper resource="com/southwind/repository/StudentRepositoryMapper.xml"></mapper>

Attach the running result graph:

Insert picture description here

Next is a many-to-one query:

  • The first is the ClassesRepository interface:
package com.southwind.repository;

import com.southwind.entity.Classer;

public interface ClassesRepository {
    public Classer findById(int id);
}

  • Next is the ClassesRepository.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 多对一 的级联查询 -->
<mapper namespace="com.southwind.repository.ClassesRepository">
    <!--    查询语句-->
    <select id="findById" parameterType="int" resultMap="classesMap">
        select s.id,s.name,c.id as cid,c.name as cname
        from student s,class c
        where c.id = #{id} and s.cid = c.id
    </select>
    <!--    结果集映射 间接映射 专门处理不能直接映射的情况-->
    <!--    column 数据库字段 -->
    <!--    property 类里的属性-->
    <resultMap id="classesMap" type="com.southwind.entity.Classer">
        <id column="cid" property="id"></id>
        <result column="cname" property="name"></result>
<!--        ofType 指的是它的泛型-->
        <collection property="students" ofType="com.southwind.entity.Student">
            <id column="id" property="id"></id>
            <result column="name" property="name"></result>
        </collection>
    </resultMap>
</mapper>
  • Then the test class Test_4:
package com.southwind.test;

import com.southwind.repository.ClassesRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test_4 {
    public static void main(String[] args) {
        //        加载配置文件
        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        ClassesRepository classesRepository = sqlSession.getMapper(ClassesRepository.class);
        System.out.println(classesRepository.findById(1));
        sqlSession.close();
    }
}

  • Paste the running result:
    Insert picture description here

The data also has many operation statements, well, it needs to strengthen its SQL knowledge.


Published 20 original articles · praised 4 · visits 612

Guess you like

Origin blog.csdn.net/qq_45031575/article/details/105027068