MyBatis开发入门二:一对多连表查询

1. 步骤:

  • (1). 加包
  • (2). 编写db.properties;编写conf.xml,将db.properties加入到conf.xml;引入别名
  • (3). 建立实体类
  • (4). 编写sql操作对应的***Mapper.xml文件
  • (5). 将sql操作对应的***Mapper.xml文件注册到conf.xml文件中
  • (6). 编写MyBaitsUtils
  • (7). 测试

2. 详细步骤

  • (1). 加包

  

  • (2). 编写db.properties;
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis1
username=root
password=lfdy
  • 编写conf.xml,将db.properties加入到conf.xml;
<!--  加载db.properties  这段必须放在上面-->
    <properties resource="db.properties"></properties>

<!-- 配置连接数据库信息 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property name="driver" value="${driverClass}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
        </environment>
    </environments>
  • 引入别名
<!-- 加入别名 -->
    <typeAliases>
        <package name="com.atguigu.mybatis.domain"/>
    </typeAliases>
  • (3). 建立实体类
package com.atguigu.mybatis.domain;

import java.util.List;

/**
 * @author hp
 *
 */
public class Classes {
    
    private int id;
    private String name;
    private Teacher teacher;
    private List<Student> students;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public Classes(int id, String name, Teacher teacher, List<Student> students) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
        this.students = students;
    }
    public Classes() {
        super();
    }
    @Override
    public String toString() {
        return "Classses [id=" + id + ", name=" + name + ", teacher=" + teacher + ", students=" + students + "]";
    }
    

}
View Code
package com.atguigu.mybatis.domain;

/**
 * @author hp
 *
 */
public class Teacher {

    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Teacher(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Teacher() {
        super();
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
    
}
View Code
package com.atguigu.mybatis.domain;

/**
 * @author hp
 *
 */
public class Student {
    
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Student(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Student() {
        super();
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + "]";
    }
    
}
View Code
  • (4). 编写sql操作对应的***Mapper.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.atguigu.mybatis.mapper.ClassMapper">

  <!-- 方式1:嵌套结果 --> <select id="getClass" parameterType="int" resultMap="ClassResultMap3"> select * from class c,teacher t,student s where c.teacher_id = t.t_id and c.c_id = s.class_id and c.c_id = #{id} </select> <resultMap type="Classes" id="ClassResultMap3"> <id property="id" column="c_id"/> <result property="name" column="c_name"/> <association property="teacher" column="teacher_id" javaType="Teacher"> <id property="id" column="t_id"/> <result property="name" column="t_name"/> </association> <collection property="students" ofType="Student"> <id property="id" column="s_id"/> <result property="name" column="s_name"/> </collection> </resultMap>

        

      <!-- 嵌套chaxun查询 -->
    <select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
        select * from class where c_id = #{id}
    </select>
    <resultMap type="Classes" id="ClassResultMap2">
      <id property="id" column="c_id"/>
    <result property="name" column="c_name"/>
    <association property="teacher" column="teacher_id" javaType="Teacher" select="getTeacher2"/>
    <collection property="students" column="c_id" ofType="Student" select="getStudent2"/>
    </resultMap>

    <select id="getTeacher2" parameterType="int" resultType="Teacher">
      select t_id id,t_name name from teacher where t_id=#{id}
    </select>

    <select id="getStudent2" parameterType="int" resultType="Student">
      select s_id id,s_name name from student where class_id=#{id}
    </select>

</mapper>
  • (5). 将sql操作对应的***Mapper.xml文件注册到conf.xml文件中
<!-- 注册sql映射的mapper文件 -->
    <mappers>
        <mapper resource="com/atguigu/mybatis/mapper/ClassMapper.xml" />
    </mappers>
  • (6). 编写MyBaitsUtils
package com.atguigu.mybatis.test;

import java.io.InputStream;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtils {
    
    public static SqlSessionFactory getFactory(){
        String resource = "config.xml";
        
        InputStream is = MyBatisUtils.class.getClassLoader().getResourceAsStream(resource);
        
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        
    
        return factory;
    }

}
  • (7). 测试
package com.atguigu.mybatis.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.atguigu.mybatis.domain.*;

/**
 * @author hp
 *
 */
public class MyBatisTest {
    
    public static void main(String[] args) {
        
        SqlSessionFactory factory = MyBatisUtils.getFactory();
        
        SqlSession session = factory.openSession();
        
        String statement = "com.atguigu.mybatis.mapper.ClassMapper.getClass";
    
        Classes c=  (Classes) session.selectOne(statement,1);
        System.out.println(c);
        
        session.close();
        
    }

}

 

转载于:https://www.cnblogs.com/lfdingye/p/6444189.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值