码迷,mamicode.com
首页 > 其他好文 > 详细

实现联表查询

时间:2019-08-31 21:15:11      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:padding   width   close   each   oftype   builder   ase   auto   iat   

一、一对一关联

1.在数据库中创建班级表和老师表

CREATE TABLE teacher(

t_id INT PRIMARY KEY AUTO_INCREMENT,

t_name VARCHAR(20)

);

CREATE TABLE class(

c_id INT PRIMARY KEY AUTO_INCREMENT,

c_name VARCHAR(20),

teacher_id INT

);

ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id)

REFERENCES teacher(t_id);

INSERT INTO teacher(t_name) VALUES(‘LS1‘);

INSERT INTO teacher(t_name) VALUES(‘LS2‘);

INSERT INTO class(c_name, teacher_id) VALUES(‘bj_a‘, 1);

INSERT INTO class(c_name, teacher_id) VALUES(‘bj_b‘, 2);

 

2.创建相应的实体类Classes和Teacher

 

3.配置映射文件ClassesMapper.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.zhiyou100.kfs.dao.ClassesMapper">

       <!-- 通过cid查询班级信息和对应的老师信息 -->

      

       <resultMap type="com.zhiyou100.kfs.bean.Classes" id="BaseMap">

              <!--

                     id标签数据库表主键对应实体类属性

                     result标签数据库表除了主键对应的实体类属性

                            column属性是对应数据库表的栏,property属性是对应实体类的属性

              -->

              <id column="c_id" property="cid"/>

              <result column="c_name" property="cname"/>

              <result column="teacher_id" property="teacherid"/>

              <!-- association标签是一对一关联返回的配置标签

                     property属性是实体类中的对象名,javaType是对象的全类名

               -->

              <association property="teacher" javaType="com.zhiyou100.kfs.bean.Teacher">

                     <id column="t_id" property="tid"/>

                     <result column="t_name" property="tname"/>

              </association>

       </resultMap>

       <!-- 1.直接用联表sql语句查询,返回结果集再配置一下 -->

       <select id="select1" resultMap="BaseMap">

              select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{cid}

       </select>

</mapper>

 

 

4.创建dao接口ClassesMapper.java

public interface ClassesMapper{

       /**

        * 一对一关联

        * @param cid

        * @return

        */

       public Classes select1(int cid);

}

 

5.创建单元测试

         @Test

         void testSelect1() throws IOException {

                   //1.获取配置文件config.xml加入到io流中

                   InputStream inputStream = Resources.getResourceAsStream("config.xml");

                   //2.通过SqlSessionFatoryBuilder的builder创建sqlsessionfatory

                   SqlSessionFactory sqlSessionFatory = new SqlSessionFactoryBuilder().build(inputStream);

                   //3.通过sqlSessionFactory的openSession方法创建SqlSession

                   SqlSession sqlSession=sqlSessionFatory.openSession();

                   //4.通过sqlSession的getMapper传入dao全类名的方法创建UserMapper对象

                   ClassesMapper um=sqlSession.getMapper(com.zhiyou100.kfs.dao.ClassesMapper.class);

                   //5.调用UserMapper的selectById来使用通过id查询一个用户

                   Classes classes=um.select1(1);

                   System.out.println(classes);

                   //最后一个页是很重要的一个:关闭资源

                   sqlSession.close();

         }

 

 

二、一对多关联

1.在数据库里创建学生表

CREATE TABLE student(

s_id INT PRIMARY KEY AUTO_INCREMENT,

s_name VARCHAR(20),

class_id INT

);

INSERT INTO student(s_name, class_id) VALUES(‘xs_A‘, 1);

INSERT INTO student(s_name, class_id) VALUES(‘xs_B‘, 1);

INSERT INTO student(s_name, class_id) VALUES(‘xs_C‘, 1);

INSERT INTO student(s_name, class_id) VALUES(‘xs_D‘, 2);

INSERT INTO student(s_name, class_id) VALUES(‘xs_E‘, 2);

INSERT INTO student(s_name, class_id) VALUES(‘xs_F‘, 2);

 

2.创建对应Student实体类

3.配置映射文件ClassesMapper.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.zhiyou100.kfs.dao.ClassesMapper">

       <!-- 通过cid查询班级信息和对应的学生信息 -->

      

       <resultMap type="com.zhiyou100.kfs.bean.Classes" id="BaseMap">

              <!--

                     id标签数据库表主键对应实体类属性

                     result标签数据库表除了主键对应的实体类属性

                            column属性是对应数据库表的栏,property属性是对应实体类的属性

              -->

              <id column="c_id" property="cid"/>

              <result column="c_name" property="cname"/>

              <result column="teacher_id" property="teacherid"/>

              <!-- collection标签是一对多关联的配置

                     property是实体类对应的集合名,ofType是集合类的全类名

               -->

              <collection property="list" ofType="com.zhiyou100.kfs.bean.Student">

                     <id column="s_id" property="sid"/>

                     <result column="s_name" property="sname"/>

                     <result column="class_id" property="classid"/>

              </collection>

       </resultMap>

       <!-- 1.直接用联表sql语句查询,返回结果集再配置一下 -->

       <select id="select2" resultMap="BaseMap">

              select * from class c,student s where c.c_id=s.class_id and c.c_id=#{cid}

       </select>

</mapper>

 

4.创建dao接口ClassesMapper.java

public interface ClassesMapper{

       /**

        * 一对多关联

        * @param cid

        * @return

        */

       public Classes select2(int cid);

}

5.创建单元测试来测试

         @Test

         void testSelect2() throws IOException {

                   //1.获取配置文件config.xml加入到io流中

                   InputStream inputStream = Resources.getResourceAsStream("config.xml");

                   //2.通过SqlSessionFatoryBuilder的builder创建sqlsessionfatory

                   SqlSessionFactory sqlSessionFatory = new SqlSessionFactoryBuilder().build(inputStream);

                   //3.通过sqlSessionFactory的openSession方法创建SqlSession

                   SqlSession sqlSession=sqlSessionFatory.openSession();

                   //4.通过sqlSession的getMapper传入dao全类名的方法创建UserMapper对象

                   ClassesMapper um=sqlSession.getMapper(com.zhiyou100.kfs.dao.ClassesMapper.class);

                   //5.调用UserMapper的selectById来使用通过id查询一个用户

                   Classes classes=um.select2(1);

                   System.out.println(classes);

                   //最后一个页是很重要的一个:关闭资源

                   sqlSession.close();

         }

实现联表查询

标签:padding   width   close   each   oftype   builder   ase   auto   iat   

原文地址:https://www.cnblogs.com/kfsrex/p/11439608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!