ssm系列之MyBatis(二)

输入参数

在Mybatis中输入参数parameterType有两种类型,分别是简单类型和对象类型。表达方式也分两种,分别是#{} 和 ${}。

#{}自动给String类型加上’’(自动类型转换)
${}原样输出,但是适合于 动态排序(动态字段),如果输入类型是String类型,需要在外部加单引号

简单类型(8个基本类型+Sring)使用#{}表示时可以在{}里面写任意值,使用${}表示时{}里面只能为value。

对象类型使用#{}和${}都需要在{}里面填写属性值
主要代码

//mapper.xml代码
<select id="queryStudentOrderByColumn" resultType="student" parameterType="string">
        select stuno,stuname,stuage from student order by ${value} desc
    </select>
    
//java代码
List<Student> student = studentMapper.queryStudentOrderByColumn("stuname");

在queryStudentOrderByColumn中写入要查询的字段名(只有数据库中存在的即可),然后会从数据库中获取数据并进行排序。这样不需要查询特定的字段名。

输出参数

输出参数resultType分简单类型(8个基本类型+String)、实体对象类型、实体对象类型的集合和HashMap。
1、输出参数为简单类型

    <select id="queryStudentCount" resultType="int">
        select count(*) from student
    </select>

2、输出参数为实体对象类型

    <select id="queryPersonByStuno" resultType="student" parameterType="int">
    select * from student where stuno = #{stuno}
    </select>

3、输出参数为实体对象类型的集合

    mapper.xml中的返回值为该类的类型,但在mapper接口中需定义
    为集合类型,即List<Student>
    <select id="queryAllStudents" resultType="student">
        select * from student
    </select>

4、输出参数为HashMap

   <select id="queryStudentOutByHashMap" resultType="HashMap">
       <!-- 别名作为Map的key,如下列代码的key为"no""name" -->
       <!-- 使用HashMap查询单个学生 -->
       select stuno "no",stuname "name" from student where stuno=1
   </select>

   <select id="queryAllStudentsOutByHashMap" resultType="HashMap">
       <!-- 使用HashMap实现查询多个学生 -->
       select stuno "no",stuname "name" from student
   </select>

5、属性名 和 字段名不一致。在resultMap中,子元素id用于设置主键字段与领域模型属性的映射关系,子元素result用于设置普通字段与领域模型属性的映射关系。

   <!-- 1)、使用resultMap指定输出类型及映射关系 -->
   <select id="queryStudentByid" parameterType="int" resultMap="queryStudentByidMap">
       select id,sname from student where id=#{id}
   </select>
   <resultMap id="queryStudentByidMap" type="student">
       <!-- 指定类中的属性 和 表中的字段对应关系 -->
       <id property="stuNo" column="id"/>
       <result property="stuName" column="sname"/>
   </resultMap>

   <!-- 2)、使用使用resultType+HashMap别名 -->
   <select id="queryStudentByIdWithHashMap" parameterType="int" resultType="student">
       <!-- select 表的字段名 "类的属性名" ... 来指定字段名和属性名的对应关系-->
       select id "stuNo",sname "stuName" from student where id=#{id}
   </select>

MyBatis调用存储过程

在oracle中执行
create or replace procedure queryCountByGradeWithProcedure(gName in varchar,scount out number)
as
begin
	select count(1) into scount from student where graname = gname;
end;
/

通过调用存储过程 实现咨询
statementType=“CALLABLE”:表示调用的方式是存储过程
存储过程的输入参数,在mybatis用Map来传递(HashMap)
在Mybatis中执行

<select id="queryCountByGradeWithProcedure" 
statementType="CALLABLE" 
parameterType="HashMap">
        {
	CALL queryCountByGradeWithProcedure(
	#{gName,jdbcType=VARCHAR,mode=IN},
	#{sCount,jdbcType=INTEGER,mode=OUT}
	)
	}
    </select>

动态SQL

通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。
if中的test的值为表字段名对应的类属性名(区分大小写)

//if跟where结合
<select id="queryStuByNOrAWithSQLTag" parameterType="student" resultType="student">
select stuno ,stuname ,stuage  from student where
        <where>
            <if test="stuName !=null and stuName!=''">
                and stuname = #{stuName}
            </if>
            <if test="stuAge !=null and stuAge!=0">
                and stuage = #{stuAge}
            </if>
        </where>
    </select>

foreach标签
collection为迭代的类型:简单类型数组(String+8个基本类型)、对象数组、集合、属性(定义属性时用集合或数组类型)
open:主语句前部分
close:主语句后部分
item:迭代标签
separator:迭代标签的分隔符
以迭代类型为例,当迭代的类型为属性时,test和collection都用传入的属性名进行表示,parameterType为要传入的参数类型 ;当迭代的类型为简单类型数组时,test和colletion都用array表示,parameterType为数组类型;当迭代的类型为集合时,test和colletion都用list表示,parameterType也为list;当迭代的类型为对象数组时,test和colletion都用array表示,parameterType为Object[];

<select id="queryStudentWithForeach" parameterType="stuid" resultType="student">
        select * from student
        <where>
            <if test="stuNos!=null and stuNos.size>0">
                <foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separator=",">
                #{stuNo}
                </foreach>
            </if>
        </where>
    </select>

sql片段:
1、提取相似代码:把代码放入sql标签里,一般把sql标签放在mapper文件最前面
2、引用:使用include标签,在refid中放入sql标签的id

<!--
    <sql id="ObjectArray2">
        <where>
            <if test="array!=null and array.length>0">
                <foreach collection="array" open=" and stuno in (" close=")" item="student" separator=",">
                    #{student.stuNo}
                </foreach>
            </if>
        </where>
    </sql>

    <select id="queryStudentWithObjectArray" parameterType="Object[]" resultType="student">
        select * from student
        <include refid="ObjectArray2">
        </include>
    </select>
    -->

关联查询

分为一对一、一对多、多对一和多对多。
在Mybatis中一对多、多对一、多对多的实现方法相似。故下面只讲述一对一和一对多。能通过业务扩展类实现,也能通过resultMap实现。

使用业务扩展类实现一对一:一般需要三个类,其中两个类分别写不同属性,最后再用一个类统一起来,其核心为:用resultType指定类的属性 包含 多表查询的所有字段

<!-- 
    <select id="queryStudentWithOO" parameterType="int" resultType="StudentBusiness">
        select s.*,c.* from student s inner join studentcard c
        on s.cardid=c.cardid
        where s.stuno = #{stuNo}
    </select>

利用resultMap实现一对一:通过 属性成员 将2个类建立起联系,对象成员使用association映射;javaType指定该属性的类型

    <select id="queryStudentWithOO2" parameterType="int" resultMap="student_card_map">
        select s.*,c.* from student s inner join studentcard c
        on s.cardid=c.cardid
        where s.stuno = #{stuNo}
    </select>
    
    <resultMap id="student_card_map" type="student">
        <id property="stuNo" column="stuNo"/>
        <result property="stuName" column="stuName"/>
        <result property="stuAge" column="stuAge"/>
        <association property="studentCard" javaType="StudentCard">
            <id property="cardId" column="cardId"/>
            <result property="cardInfo" column="cardInfo"/>
        </association>
    </resultMap>
        -->

利用resultMap实现一对多:对象成员使用collection映射;描述属性的元素的类型用ofType

<select id="queryOM" parameterType="int" resultMap="class_student_map">
    <!-- 查询g1班的班级信息和g1班的所有学生信息 -->
        select c.*,s.* from student s
         inner join studentclass c
         on c.classid = s.classid
        where c.classid = #{classId}
    </select>
    
    <resultMap id="class_student_map" type="StudentClass">
        <id property="classId" column="classId"/>
        <result property="className" column="className"/>
        <!-- 配置成员属性,一对多 -->
        <collection property="students" ofType="Student">
            <id property="stuNo" column="stuNo"/>
            <result property="stuName" column="stuName"/>
            <result property="stuAge" column="stuAge"/>
        </collection>
    </resultMap>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值