(三)Mybatis介绍

(一)Mybatis介绍
(二)Mybatis介绍

Pojo包装类型

1、输入参数的类型为map
这里写图片描述
主类测试:
这里写图片描述
注意:为什么要写jdbcType?什么时候写jdbcType?
当为参数传递空值时,你需要指定jdbcType。
即使值本身为NULL,某些数据库也需要知道值的类型。出于这个原因,为了获得最大的可移植性,JDBC规范本身需要指定类型,MyBatis需要将它传递,因为它是在JDBC之上构建的。

从MyBatis文档:

JDBC类型只有在插入,更新或删除时才需要可空列。这是一个JDBC要求,而不是MyBatis。所以,即使你直接编码JDBC,你也需要指定这个类型 - 但只能用于空值。
大多数情况下,您不需要将jdbcType指定为MyBatis就足够聪明,可以从您正在使用的对象中找出类型。但是,如果您将参数发送到HashMap中的MyBatis语句,例如,其中一个参数为null,MyBatis将无法通过查看HashMap来确定参数的类型,因为HashMap只是一个通用的容器和null本身没有类型信息。在这一点上,提供jdbcType是一个好主意,以便稍后切换数据库实现不会导致null值的任何问题。

2、包装类型
包装类型里面的对象必须有get,set方法。
举例:查询班级为1班的姓张学生的信息
(1)创建包装类心pojo(必须有get,set方法)

public class StudentClass {

    //学生类型对象
    private Student stu;

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

}

(2)编写StudentMapper.xml(当类中属性是对象的时候的该如何书写)

    <!--  查询1班中 姓张的同学-->
    <!-- #{id}:id就是map中键的名称 -->
    <!-- resultType是别名 -->
    <select id="findStudentByMap" parameterType="map" resultType="student">
        select * from student where name like #{name,jdbcType=VARCHAR} and class_id = #{class_id}
    </select>

输出映射

1、resultType

  • 使用resultType进行输出映射,只有查询出来的列名和pojo中的属性名一致,这时,该列才可以映射成功
  • 如果查询出来的列名和pojo中的属性名全部不一致,那么不会创建pojo的对象
  • 只要查询出的列名和pojo中的属性有一个一致,就会创建pojo对象

2. resultMap

  • mybatis中可以使用resultMap来完成高级输出结果映射
  • 如果查询出来的列名和pojo的属性名不一致,通过定义一个resultMap来对列名和属性名之间做一个映射
<select id="findStudentResultMap" parameterType="StudentClass" resultMap="studentMap">
        select id id_,name name_,age age_,class_id class_id_ from student where name like #{stu.name} and class_id = #{stu.class_id}
</select>

单看sql语句,可以知道查询结果的列名分别为id_,name_,age_,class_id_;
问题是,没有哪个实体类是与这些列名对应的,所以用到了resultMap来实现让结果的列名与实体类的属性一一对应。

<!-- type的值是实体类的类名,填全路径名或者别名 -->
<resultMap type="student" id="studentMap">
        <id column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="age_" property="age"/>
        <result column="class_id_" property="class_id"/>
</resultMap>

一对一查询

需求:查询学生信息,关联查询班级信息

  • (1) 主查询表:学生表
  • (2)关联查询表:班级表

1、使用resultType的方式

方法一:创建pojo,将sql查询的结果集映射到pojo

方法二:扩展类

<select id="findStudentAndClass" resultType="studentvo">
        select s.*,c.* from student s,class c where s.class_id = c.cid;
</select>

附上StudentVo类代码(主要是set,get方法)

public class StudentVO extends Student{

    //扩展学生的信息
    private String Cname;
    private int cid;
    private int scount;


    public String getCname() {
        return Cname;
    }
    public void setCname(String cname) {
        Cname = cname;
    }
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public int getScount() {
        return scount;
    }
    public void setScount(int scount) {
        this.scount = scount;
    }

    @Override//如果没加toString,那么system.out.println(StudentVo对象)的时候会调用父类的toString
    public String toString() {
        return "StudentVO [Cname=" + Cname + ", cid=" + cid + ", scount=" + scount + "]";
    }

}

2、使用resultMap方式
(1)在Student类中加入一个Class对象

public class Student {

    private int id;
    private int age;
    private int class_id;
    private String name;

    private Class cla;//学生与班级是一对一,变量名不能用关键字
    //说明每个学生都有一个班级属性


    public int getId() {
        return id;
    }
    public Class getCla() {
        return cla;
    }
    public void setCla(Class cla) {
        this.cla = cla;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getClass_id() {
        return class_id;
    }
    public void setClass_id(int class_id) {
        this.class_id = class_id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", age=" + age + ", class_id=" + class_id + ", name=" + name + ", cla=" + cla
                + "]";
    }
}
<resultMap type="student" id="studentMap">
        <!-- 学生信息 -->
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="class_id" property="class_id"/>


        <!-- 班级信息(是个对象) 因为是一对一的关系,所以用association--> 
        <!-- property:是学生类中班级(属性)对象的名字 -->
        <association property="cla" javaType="class">
            <id column="cid" property="cid"/>
            <result column="cname" property="cname"/>
            <result column="scount" property="scount"/>
        </association>
</resultMap>

    <!-- 使用ResultMap 一对一-->
<select id="findStudentAndClassResultMap" resultMap="studentMap">
        select s.*,c.* from student s,class c where s.class_id = c.cid;
</select> 

多对一查询

ResultMap方式
(1)Class类有一个Student类的集合,类代码如下

public class Class {

    private int cid;
    private String cname;
    private int scount;

    private List<Student> sList;//class 是一对多

    public List<Student> getsList() {
        return sList;
    }
    public void setsList(List<Student> sList) {
        this.sList = sList;
    }
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public int getScount() {
        return scount;
    }
    public void setScount(int scount) {
        this.scount = scount;
    }
    @Override
    public String toString() {
        return "Class [cid=" + cid + ", cname=" + cname + ", scount=" + scount + ", sList=" + sList + "]";
    }
}
    <resultMap type="class"  id="classMap">
        <id column="cid" property="cid"/>
        <result column="cname" property="cname"/>
        <result column="scount" property="scount"/>

        <!-- 学生的信息 -->
        <!-- property:班级类中学生集合对象的名字(属性名字)  -->
        <collection property="sList" ofType="student">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="age" property="age"/>
            <result column="class_id" property="class_id"/>
        </collection>
    </resultMap>

    <!-- 一对多 一个班级对应多个学生 -->
    <select id="findClassAndStudentMap" resultMap="classMap">
        select s.*,c.* from student s,class c where s.class_id = c.cid;
    </select>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值