zoukankan      html  css  js  c++  java
  • mybatis一对多操作数据库

     

    TeacherMapper.java

    package dao;
    
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    import pojo.Teacher;
    
    import java.util.List;
    
    public interface    TeacherMapper {
    
        //test环境
    //    public List<Teacher> getTeacher();
    
        //获取指定老师下的学生
    
        public Teacher getTeacher(@Param("tid") int id);
    
    
        public Teacher getTeacher2(@Param("tid") int id);
    }

    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">
    <!--namespace=绑定一个对应的Dao/Mapper接口-->
    <mapper namespace="dao.TeacherMapper">
    
        <!--第一种-->
        <!--按结果嵌套查询-->
        <resultMap id="TS" type="Teacher">
            <result property="id" column="tid"/>
            <result property="name" column="tname"/>
            <!--一对多使用collection-->
            <!--集合使用ofType-->
            <collection property="student" ofType="Student">
                <result property="id" column="sid"/>
                <result property="name" column="sname"/>
                <result property="tid" column="tid"/>
    
            </collection>
        </resultMap>
    
        <select id="getTeacher" resultMap="TS">
            select s.id sid,s.name sname,t.name tname,t.id tid from student s,teacher t where s.tid=t.id and t.id=#{tid}
    
        </select>
        <!--==================================================-->
    
        <resultMap id="TS2" type="Teacher">
            <collection property="student"  javaType="ArrayList" ofType="Student" select="getStudent" column="id">
    
            </collection>
    
        </resultMap>
    
        <select id="getTeacher2" resultMap="TS2">
            select * from teacher where id = #{tid}
    
        </select>
        <select id="getStudent" resultType="Student">
            select * from  student where tid = #{id}
        </select>
    
    
    </mapper>

    MyTset.java

    package dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    import pojo.Teacher;
    import utils.MybatisUtils;
    
    import java.util.List;
    
    public class MyTest {
        @Test
        public void getTeacher()
        {
            SqlSession sqlSession= MybatisUtils.getSqlSession();
            TeacherMapper teacherMapper=sqlSession.getMapper(TeacherMapper.class);
            Teacher teacher=teacherMapper.getTeacher(1);
            System.out.println(teacher);
            sqlSession.close();
        }
        @Test
        public void getTeacher2()
        {
            SqlSession sqlSession= MybatisUtils.getSqlSession();
            TeacherMapper teacherMapper=sqlSession.getMapper(TeacherMapper.class);
            Teacher teacher=teacherMapper.getTeacher2(1);
            System.out.println(teacher);
            sqlSession.close();
        }
    }

    今天的第一种和昨天的第二种是同一类型的查询方式,个人比较喜欢第二种,喜欢SQL语句复杂一些,而不是套娃复杂一些

  • 相关阅读:
    正向代理和反向代理
    负载测试和压力测试
    cs 与 bs 架构
    什么是amcl
    一个故事告诉你比特币的原理及运作机制
    Tor Browser(洋葱浏览器)——一款使你匿名上网的浏览器
    CAS3.5.x(x>1)支持OAuth2 server
    帮你深入理解OAuth2.0协议
    使用Spring MVC统一异常处理实战
    tcpdump非常实用的抓包实例
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14682700.html
Copyright © 2011-2022 走看看