Java教程

Mybatis学习(2)

本文主要是介绍Mybatis学习(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

级联查询

        一对多(多对多同理)

        以学生-班级关系为例,一个班级对应多个学生

1.先分别写实体类

Students.java

package org.demo01.entity;

import lombok.Data;

@Data
public class Students
{
    private long id;
    private String name;
    private Classes classes;
}

Classes.java

import lombok.Data;

import java.util.List;

@Data
public class Classes
{
    private long id;
    private String name;
    private List<Students> students;
}

2.分别创建接口

StudentRepository.java

package org.demo01.repository;

import org.demo01.entity.Students;

public interface StudentRepository
{
    public Students findById(long id);
}

ClassesRepository

package org.demo01.repository;

import org.demo01.entity.Classes;

public interface ClassesRepository
{
    public Classes findById(long id);
}

3.分别写mapper文件

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">
<mapper namespace="org.demo01.repository.StudentRepository">

    <resultMap id="studentMap" type="org.demo01.entity.Students">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="classes" javaType="org.demo01.entity.Classes">
            <id column="cid" property="id"/>
            <result column="cname" property="name"/>
        </association>
    </resultMap>

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

ClassesRepository.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="org.demo01.repository.ClassesRepository">

    <resultMap id="classesMap" type="org.demo01.entity.Classes">
        <id column="cid" property="id"/>
        <result column="cname" property="name"/>

        <collection property="students" ofType="org.demo01.entity.Students">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
        </collection>
    </resultMap>

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

</mapper>

注意上面两个xml文件中一些细微的区别

比如StudentRepository.xml中用的<association/>块

<association property="classes" javaType="org.demo01.entity.Classes">
    <id column="cid" property="id"/>
    <result column="cname" property="name"/>
</association>

而ClassesRepository.xml中用的<collection/>块

<collection property="students" ofType="org.demo01.entity.Students">
    <id column="id" property="id"/>
    <result column="name" property="name"/>
</collection>

这里的column表示数据库中的列名,property表示对应实体类中想要映射的对象

这时要注意两个实体类中的信息:

//一个是Classes类对象
private Classes classes;

//一个是Students的集合对象
private List<Students> students;

由此可知,如果只是把Classes中的两个信息整合在一起,要用<association/>块,且此时后面跟的是javaType;如果需要的是一个Students集合,则要用<collection/>块,且此时后面跟的是ofType

可以粗浅理解为一对多中的“一”用<collection/>块,“多”用<association/>块

多对多时两个实体类中都是List集合对象,所以xml文件中都用<collection/>块

4.注意在config.xml中添加注册Mapper.xml

<mappers>
    <!-- 之前的一些xml -->
    <mapper resource="org/demo01/repository/StudentRepository.xml"/>
    <mapper resource="org/demo01/repository/ClassesRepository.xml"/>
</mappers>

这篇关于Mybatis学习(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!