Mybatis系列课程-一对一

目标

学会使用 assocation的select 与column 属性

select:设置分步查询的唯一标识
column:将查询出的某个字段作为分步查询的下一个查询的sql条件

 

步骤

第一步:修改Student.java 增加 

private Grade grade;  // 如果之前已经增加过, 跳过这步

 第二步:修改StudentMapper.java  增加

/**
 * 查询学生信息的同时,查询出来 年级名称
 * @return
 */
List<Student> queryWithGrade2();
package mapper;

import entity.Student;

import java.util.List;

public interface StudentMapper {
    /**
     * 查询全部
     * @return  返回数据库中所有学习信息
     */
    List<Student> findAll();

    /**
     * 按照主键查询学生信息
     * @param id  学生编号
     * @return   学生信息
     */
    Student findById(int id);

    /**
     * 按照 姓名及年级编号查询学生信息
     * @param name  姓名
     * @param gid  年级编号
     * @return   学生信息
     */
    List<Student> findBy(String name,int gid);

    /**
     * 多条件查询
     * @param student  多条件
     * @return
     */
    List<Student> find(Student student);

    /**
     * 按照姓名进行模糊查询
     * @param name
     * @return
     */
    List<Student> findByName(String name);

    /**
     * 按照姓名及电话查询
     * @param name
     * @param phone
     * @return
     */
    List<Student> query(String name,String phone);

    /**
     * 更新
     * @param student
     */
    void update(Student student);

    /**
     * 按照学号 进行多条删除
     * @param ids
     */
    void delByIds(int[] ids);

    /**
     * 查询学生信息的同时,查询出来 年级名称
     * @return
     */
    List<Student> queryWithGrade();
    /**
     * 查询学生信息的同时,查询出来 年级名称
     * @return
     */
    List<Student> queryWithGrade2();
}

第三步: 编写 GradeMapper.java 增加

Grade findById(int id);
package mapper;

import entity.Grade;

import java.util.List;

public interface GradeMapper {

    /**
     * 查询全部年级信息
     * @return
     */
    List<Grade> findAll();

    Grade findById(int id);
}

第四步: 编写GradeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.GradeMapper">

    <resultMap id="gradeMap" type="grade">
        <id column="gid" property="id"/>
        <result column="gname" property="name"/>
    </resultMap>
    <!-- 查询全部-->
    <select id="findAll"  resultMap="gradeMap">
        select * from grade
    </select>
    
    <select id="findById" parameterType="int" resultMap="gradeMap">
        select * from grade where gid=#{id}
    </select>

</mapper>

说明:

第5步: 编写 StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.StudentMapper">

    <!-- 查询全部-->
    <select id="findAll" resultType="student">
        select * from student
    </select>


    <!--按照主键查询-->
    <select id="findById" parameterType="int" resultType="student">
        select * from student where sid=#{id}
    </select>

    <!--按照姓名及年级编号查询-->
    <select id="findBy"  resultType="student">
        select * from student where gid=#{arg1} and sname=#{arg0}
    </select>

    <!--按照多条件查询-->
    <select id="find"  resultType="student" parameterType="student">
        select * from student where gid=#{gid} and sname=#{sname} and phone=#{phone}
    </select>

    <!--按照姓名进行模糊查询-->
   <!-- <select id="findByName" parameterType="string" resultType="student">
        select * from student where sname like concat('%',#{sname},'%')
    </select>-->

    <select id="findByName" parameterType="string" resultType="student">
        select * from student where sname like #{sname}
    </select>


   <!-- <select id="query" resultType="student">
        select * from student
        <where>
            <if test="param1 !=null and param1!=''"> and sname like concat('%',#{param1},'%')  </if>
            <if test="param2 !=null and param2!=''"> and phone like concat('%',#{param2},'%')  </if>
        </where>
    </select>-->

    <select id="query" resultType="student">
        select * from student
        <trim prefix="where" prefixOverrides="and">
            <if test="param1 !=null and param1!=''"> and sname like concat('%',#{param1},'%')  </if>
            <if test="param2 !=null and param2!=''"> and phone like concat('%',#{param2},'%')  </if>
        </trim>
    </select>



 <!--   <update id="update" parameterType="student">
        update student
        <set>
            <if test="phone!=null and phone!=''"> phone=#{phone}, </if>
            <if test="address!=null and address!=''"> address=#{address}, </if>
        </set>
       where sid=#{sid}
    </update>-->
    <update id="update" parameterType="student">
        update student
        <trim prefix="set" suffixOverrides=",">
            <if test="phone!=null and phone!=''"> phone=#{phone}, </if>
            <if test="address!=null and address!=''"> address=#{address}, </if>
        </trim>
        where sid=#{sid}
    </update>






    <delete id="delByIds" parameterType="int[]">
        delete from student
        <where>
            <if test="array !=null and array.length>0">
                <foreach collection="array" open=" sid in ( " close=" ) " item="id" separator=",">

                    #{id}
                </foreach>
            </if>
        </where>
    </delete>


    <resultMap id="queryGradeMap" type="student">
       <id column="sid" property="sid"/>
        <result column="sname" property="sname"/>
        <result column="born" property="born"/>
        <result column="gender" property="gender"/>
        <result column="phone" property="phone"/>
        <result column="address" property="address"/>
        <result column="gid" property="gid"/>
        <association property="grade" javaType="grade" >
            <id column="tid" property="id"/>
            <result column="gname" property="name"/>
        </association>
    </resultMap>
    <select id="queryWithGrade" resultMap="queryGradeMap">
        select s.*,g.gid as tid,g.gname from student s,grade g where g.gid=s.gid
    </select>

    <resultMap id="queryGradeMap2" type="student">
        <id column="sid" property="sid"/>
        <result column="sname" property="sname"/>
        <result column="born" property="born"/>
        <result column="gender" property="gender"/>
        <result column="phone" property="phone"/>
        <result column="address" property="address"/>
        <result column="gid" property="gid"/>
        <association property="grade"  column="gid" select="mapper.GradeMapper.findById" >
        </association>
    </resultMap>
    <select id="queryWithGrade2" resultMap="queryGradeMap2">
        select * from student
    </select>

</mapper>

最后 编写测试类

@Test
    public void test2_1() throws IOException {
        //获得SqlSession
        SqlSession session = sqlSessionFactory.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        List<Student> list = mapper.queryWithGrade2();
        list.forEach((e)->System.out.println(e));
    }

总结

上节课 内容 成为 连接查询, 本节课内容为嵌套查询

区别:

总结:
     嵌套与 关联查询的区别:

     共同点:    均需要为 实体类 增加 对应的属性  ,  
                           例如 Student.java  增加  Grade grade属性
                均需要配置 association 标签

     不同点:
         sql: 嵌套查询使用的  单表   , 列不需要 重命名
              关联查询使用的  多表   , 如果 出现 重复列, 必须 对重复的列进行 重命名

                嵌套查询 配置 assocation的 select ,column 属性,

                  通过select 调用其他映射文件的方法,通过column传递参数

                关联查询  配置 assocation的 property, javaType属性

关联查询 一次性 获取数据 ,只有一条sql

嵌套查询: 懒加载方式, 会有多条sql执行

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

射手座的程序媛

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值