亲宝软件园·资讯

展开

MyBatis一对多查询和多对一查询

小罗要有出息 人气:1

1、多对一

1、1环境搭建

数据库

CREATE TABLE teacher (
id INT(10) NOT NULL,
NAME VARCHAR(64) DEFAULT NULL,

PRIMARY KEY (id),
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO teacher (id ,NAME) VALUES (1,'罗老师');

CREATE TABLE student (
id INT(10) NOT NULL,
NAME VARCHAR(64) DEFAULT NULL,
tid INT(10) DEFAULT NULL,
PRIMARY KEY (id),
KEY fktid (tid),
CONSTRAINT fktid FOREIGN KEY (tid) REFERENCES teacher (id)
)
ALTER TABLE student ENGINE=INNODB DEFAULT CHARSET=utf8;

INSERT INTO student (id ,NAME ,tid) VALUES (1,'小明',1);
INSERT INTO student (id ,NAME ,tid) VALUES (2,'小红',1);
INSERT INTO student (id ,NAME ,tid) VALUES (3,'小张',1);
INSERT INTO student (id ,NAME ,tid) VALUES (5,'小罗',1);

MyBatis.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>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/><!--日记log4j-->
    </settings>
    <typeAliases>
        <package name="com.Google.pojo"/><!--给实体类取别名-->
    </typeAliases>
    <!--<typeAliases>
        <typeAlias type="com.Google.pojo.User" alias="user"/>
    </typeAliases>-->
    <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=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/><!--连接数据库-->
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!--注册接口-->
    <mappers>
        <mapper resource="com/Google/Dao/StudentMapper.xml"/>
        <mapper resource="com/Google/Dao/TeacherMapper.xml"/>
    </mappers>
</configuration>

1、2编写实体类、

学生·

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

老师·

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

1、3编写接口方法

public interface  StudentMapper {
    List<Student> getStudentList();
    List<Student> getStudentList1();
}

1、4编写Mapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Google.Dao.StudentMapper">
    <!--按照查询嵌套处理-->
    <select id="getStudentList" resultMap="StudentMap">
        select * from student
    </select>
    <resultMap id="StudentMap" type="Student" >
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>
    <select id="getTeacher" resultType="Teacher">
        select * from teacher
    </select>
    <!--按照结果嵌套处理-->
    <select id="getStudentList1" resultMap="StudentMap1">
        select s.id sid,s.name sname,t.name tname
        from Student s,Teacher t
        where s.tid=t.id
    </select>
    <resultMap id="StudentMap1" type="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <association property="teacher" javaType="Teacher" >
            <result property="name" column="tname"/>
        </association>
    </resultMap>
</mapper>

1、5实现

package com.Google.Dao;

import com.Google.pojo.Student;
import com.Google.units.sqlSessionFactory;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class StudentMapperText {
    @Test
    public void getStudent(){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudentList();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }
    @Test
    public void getStudent1(){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList = mapper.getStudentList1();
        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();
    }
}

1、6运行结果

Student(id=1, name=小明, teacher=Teacher(id=0, name=罗老师))
Student(id=2, name=小红, teacher=Teacher(id=0, name=罗老师))
Student(id=3, name=小张, teacher=Teacher(id=0, name=罗老师))
Student(id=4, name=小王, teacher=Teacher(id=0, name=罗老师))
Student(id=5, name=小罗, teacher=Teacher(id=0, name=罗老师))

2、一对多

2、1环境搭建和一对多一样

2、2编写实体类

@Data
public class Student {
    private int id;
    private String name;
    private int tid;
}
@Data
public class Teacher {
    private int id;
    private String name;
    //一个老师拥有多个学生,给老师创建一个学生集合
    private List<Student> student;
}

2、3编写接口的方法

public interface TeacherMapper {
    Teacher getTeacher(@Param("tid") int id);

    Teacher getTeacher1(@Param("tid") int id);
}

2、4编写Mapper配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Google.Dao.TeacherMapper">

    <!--按结果嵌套查询-->
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.id sid, s.name sname, t.id tid, t.name tname
        from student s,
             teacher t
        where s.tid = t.id
          and t.id = #{tid}
    </select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
        <!--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="getTeacher1" resultMap="TeacherStudent1">
        select * from teacher where id=#{tid}
    </select>
    <resultMap id="TeacherStudent1" type="Teacher">
        <result property="id" column="id"/>
        <collection property="student" javaType="ArrayList" ofType="Student" select="getStudetByID" column="id"/>
    </resultMap>
    <select id="getStudetByID" resultType="Student">
        select * from student where tid=#{tid}
    </select>
</mapper>

2、5实现

public class TeacherMapperText {
    @Test
    public void getTeacher(){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);
        sqlSession.close();
    }
    @Test
    public void getTeacher1(){
        SqlSession sqlSession = sqlSessionFactory.getsqlSession();
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Teacher teacher1 = mapper.getTeacher1(1);
        System.out.println(teacher1);
        sqlSession.close();
    }
}

2、6运行结果

Teacher(id=1, name=罗老师, student=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小王, tid=1), Student(id=5, name=小罗, tid=1)])

加载全部内容

相关教程
猜你喜欢
用户评论