【Java闭关修炼】MyBatis-注解多表操作

注解多表操作-一对一的实现

在这里插入图片描述

  • CardMapper
package com.itheima.one_to_one;

import com.itheima.bean.Card;
import com.itheima.bean.Person;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CardMapper {
    @Select("SELECT * FROM card")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "number",property = "number"),
            @Result(
                    property = "p",// 被包含对象的变量名,
                    javaType = Person.class, // 被包含对象的实际数据类型
                    column = "pid",// 根据查询出的card表中的pid字符段来查询person表

                    // @One 一对一固定用法
                    // select属性:指定调用哪个接口中的哪个方法
                    one = @One(select = "com.itheima.one_to_one.PersonMapper.selectById")
            )
    })
    // 查询全部
    public abstract List<Card> selectAll();
}

  • PersonMapper
package com.itheima.one_to_one;

import com.itheima.bean.Person;
import org.apache.ibatis.annotations.Select;

public interface PersonMapper {
    // 根据id查询
    @Select("SELECT * FROM person WHERE id = #{id}")
    public abstract Person selectById(Integer id);
}
  • 测试类
package com.itheima.one_to_one;

import com.itheima.bean.Card;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {

    @Test
    public void selectAll() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取cardMapper接口的实现类对象
        CardMapper mapper = sqlSession.getMapper(CardMapper.class);
        // 调用实现类对象中的方法 接受结果

        List<Card> cards = mapper.selectAll();

        for (Card card : cards) {
            System.out.println(card);
        }

        sqlSession.close();
        is.close();
    }
}

注解操作一对多的实现

  • ClassesMapper
package com.itheima.one_to_many;

import com.itheima.bean.Classes;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ClassesMapper {
    @Select("SELECT * FROM classes")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(
                    property = "students",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid")
            )
    }
    )
    // 查询全部
    public abstract List<Classes> selectAll();
}
  • StudentMapper
package com.itheima.one_to_many;

import com.itheima.bean.Student;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

    // 根据cid查询student表
    @Select("SELECT * FROM student WHERE cid = #{cid}")
    public abstract List<Student> selectByCid(Integer cid);
}


  • 测试类
package com.itheima.one_to_many;

import com.itheima.bean.Card;
import com.itheima.bean.Classes;
import com.itheima.bean.Student;
import com.itheima.one_to_one.CardMapper;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test01 {

    @Test
    public void selectAll() throws IOException {
        // 加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");

        // 获取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);

        // 通过工厂对象获取sqlSession对象
        SqlSession sqlSession = build.openSession(true);

        // 获取cardMapper接口的实现类对象
        ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class);
        // 调用实现类对象中的方法 接受结果

        List<Classes> classes = mapper.selectAll();

        for (Classes aClass : classes) {
            List<Student> students = aClass.getStudents();

            for (Student student : students) {
                System.out.println(student);
            }

        }



        sqlSession.close();
        is.close();
    }
}


多对多注解操作

在这里插入图片描述

  • courseMapper
package com.itheima.many_to_many;

import com.itheima.bean.Course;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CourseMapper {

    // 根据学生Id查询所选课程
    @Select("SELECT c.id,c.name FROM stu_cr sc,course c WHERE sc.cid = c.id AND sc.sid = #{id}")
    public abstract List<Course> selectBySid(Integer id);

}


  • StudentMapper
package com.itheima.many_to_many;

import com.itheima.bean.Student;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentMapper {

    // 查询全部信息
    @Select("SELECT * FROM student")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "age",property = "age"),
            @Result(
                   property = "courses",// 被包含对象的变量名
                    javaType = List.class,// 被包含对象的实际数据类型
                    column = "id",// 根据查询出的studnet表的id来作为关联条件  去查询中间表和课程表
//                    select属性  指定调用哪个接口中的哪个查询方法
                    many = @Many(select = "com.itheima.many_to_many.CourseMapper.selectBySid")

            )
    })

    // 查询全部
    public abstract List<Student> selectAll();
}

小结

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

少写代码少看论文多多睡觉

求打赏,求关注,求点赞

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

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

打赏作者

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

抵扣说明:

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

余额充值