MyBatis:多对一的处理(狂神)

do.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=&useUnicode=true&characterEncoding=utf-8
username=root
password=123456

 mybatis-config.xml:

<?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核心配置文件-->
<configuration>

<!--    引入外部配置文件-->
    <properties resource="db.properties">
        <property name="username" value="root"/>
        <!--默认优先使用外部的db.properties配置文件中的-->
        <property name="password" value="111"/>
    </properties>


<!--    设置日志-->
    <settings>
        <!-- 标准的日志工厂实现:STDOUT_LOGGING控制台输出0-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <!--log4j日志-->
<!--     <setting name="logImpl" value="LOG4J"/>-->
    </settings>


    
<!--    可以给实体类起别名-->
    <typeAliases>
        <typeAlias type="com.kuang.pojo.Student" alias="Student"></typeAlias>
        <typeAlias type="com.kuang.pojo.Teacher" alias="Teacher"></typeAlias>
    </typeAliases>

    <environments default="development"><!--default:默认的环境这里可以配置:多个环境-->
        <environment id="development">
            <!--事务管理-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

<!-- 映射器   每一个Mapper.xml文件,都需要在Mybatis核心配置文件中注册-->
    <mappers>
        <mapper class="com.kuang.dao.TeacherMapper"/>
        <mapper class="com.kuang.dao.StudentMapper"/>
    </mappers>


</configuration>

 Student:使用Lombok

package com.kuang.pojo;

import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;
    //学生需要关联一个老师
    private Teacher teacher;
}

Teacher:

package com.kuang.pojo;

import lombok.Data;

@Data
public class Teacher {
    private int id;
    private String name;
}

 TeacherMapper:使用注解

package com.kuang.dao;

import com.kuang.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface TeacherMapper {

    @Select("select * from teacher where id=#{tid}")
    Teacher getTeacher(@Param("tid") int id);
}

StudentMapper:

package com.kuang.dao;

import com.kuang.pojo.Student;

import java.util.List;

public interface StudentMapper {
    //查询所有的学生信息,以及所有的老师的信息
    public List<Student> getStudent();

    public List<Student> getStudent2();
}

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.kuang.dao.StudentMapper">
    <!--使用insert,update,delete,select标签来写sql语句-->
    
    <!--
       思路:
       1.查询所有的学生信息
       2.根据查询出来的学生的id,查询对应的老师
    -->
    <select id="getStudent" resultType="Student" resultMap="StudentTeacher">
        select * from  student1
    </select>
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!-- 复杂的属性,我们需要单独处理
              对象:association
              集合:collection
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    
    <select id="getTeacher" resultType="com.kuang.pojo.Teacher">
        select * from teacher where id=#{tid}
    </select>


<!-- ============================-->
<!--    按照结果集查询-->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
         from student1 s,teacher t
         where s.tid=t.id
    </select>
    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <!-- 复杂的属性,我们需要单独处理
              对象:association
              集合:collection
        -->
        <association property="teacher" javaType="Teacher">
            <result property="id" column="sid"/>
            <result property="name" column="tname"/>
        </association>
    </resultMap>
</mapper>

MyTest:

import com.kuang.dao.StudentMapper;
import com.kuang.dao.TeacherMapper;
import com.kuang.pojo.Student;
import com.kuang.pojo.Teacher;
import com.kuang.utils.MybaitsUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyTest {
    public static void main(String[] args) {
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);
        sqlSession.close();
    }
    //方法一 查询嵌套
    @Test
    public void testStudent(){
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        List<Student> studentList = mapper.getStudent();

        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();

    }
    //方法二 按照结果集查询
    @Test
    public void testStudent2(){
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        List<Student> studentList = mapper.getStudent2();

        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();

    }
}

 方法一

方法二 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值