Mybatis多对一查询

配置数据源

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
                <property name="username" value="root"/>
                <property name="password" value="lz666."/>
            </dataSource>
        </environment>
    </environments>
    <!--每一个xml都需要单独写一次-->
    <mappers>
        <mapper class="com.li.mapper.StudentMapper"/>
        <mapper class="com.li.mapper.TeacherMapper"/>
    </mappers>
</configuration>

配置工具类

package com.li.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    public static SqlSessionFactory sqlSessionFactory;
    static {
        //构建 SqlSessionFactory对象
        try {
            String resource = "MybatisConfig.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
    // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession 实例来直接执行已映射的 SQL
    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession(true);
    }
}

根据数据库创建pojo对象

student对象

package com.li.pojo;

import lombok.Data;

@Data
public class Student {
    public int sid;
    public String sname;
    public Teacher teacher;
}

teacher对象

package com.li.pojo;

import lombok.Data;

@Data
public class Teacher {
    public String tid;
    public String tname;
}

创建mapper接口

StudentMapper

package com.li.mapper;

import com.li.pojo.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface StudentMapper {
    public List<Student> studentList(@Param("id") int id);
}

TeacherMapper

package com.li.mapper;

import com.li.pojo.Student;

import java.util.List;

public interface TeacherMapper {
    List<Student> teachertList();
}

resources目录

在resources文件下创建相同与mapper相同目录结构的目录

StudentMapper.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.li.mapper.StudentMapper">
    <select id="studentList" resultMap="studentMap" parameterType="int">
        select * from Student where sid=#{id}
    </select>

    <resultMap id="studentMap" type="com.li.pojo.Student">
        <association property="teacher" column="tid" javaType="com.li.pojo.Teacher" select="teacherList"></association>
    </resultMap>

    <select id="teacherList" resultType="com.li.pojo.Teacher">
        select * from Teacher where tid=#{tid}
    </select>
</mapper>

TeacherMapper.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.li.mapper.TeacherMapper">

</mapper>

测试

创建相同目录结构的测试类

package com.li.mapper;

import com.li.pojo.Student;
import com.li.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class test {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = mapper.studentList(123);
        for (Student student : students) {
            System.out.println(student);
        }
        sqlSession.close();
    }
}

重点

主要是在StudentMapper.xml配置的ResultMap映射

ResultMap

<resultMap id="studentMap" type="com.li.pojo.Student">
       <association property="teacher" column="tid" javaType="com.li.pojo.Teacher" >select="teacherList"></association>
   </resultMap>

property:表示pojo中对象的属性
column:表示数据库中要查询的字段名称,由column属性映射到property属性
javaType:指定返回值类型
select:表示要将column值传递到的地方

<select id="teacherList" resultType="com.li.pojo.Teacher">
        select * from Teacher where tid=#{tid}
</select>

id属性与select的值相同,也必须和TeacherMapper接口中的方法相同

附目录结构

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值