mybatis学习之路(二)

本文深入探讨MyBatis框架中参数处理方式,包括简单类型与对象类型的应用,详解#{}

输入参数:parameterType
1.类型为 简单类型(8个基本类型+String) 和 对象类型
    a.#{} ${}
        #{任意值}
        ${value},其中的标识符只能value
    b.#{}自动给String类型加上''
    ${}原样输出,无单引号'',但是数据库需要‘’,如果需要,手动添加。适合于动态排序(动态字段)
    select * from student where stuname = #{name}
    select * from student where stuname = '${value}'
    动态排序:select * from student order by ${value} asc

    c.#{}可以防止SQL注入
    ${}不可以

    相同之处:
    a.都可以获取对象的值

    i.获取对象的值:
    select * from student where stuage=#{stuAge} or stuname like #{stuName}  student.setStuName("%w%");
    select * from student where stuage=#{stuAge} or stuname like '%${stuName}%' student.setStuName("w");
    ii.级联对象(嵌套类型对象)
        student类包含address类,address中有homeaddress和schooladdress两个属性

        select * from student where homeaddress=#{address.homeAddress} or schooladdress = '${address.schoolAddress}'
    iii.传入参数为hashmap where stuage = #{stuAge}
        就是把Map.put("属性名","值")
        用map的key值匹配占位符#{stuAge},如果匹配成功,就用value替换占位符
    }


2.调用存储过程(在mybatis中,存储过程的输入参数只能使用Map,输入参数要跟存储过程的参数名保持一致)
    a.查询某个年级的学生总数
    输入:年级
    输出:该年级的学生总数
    select标签中有个属性是statemenType,设置为callable,就是存储过程
        {call 存储过程名(#{参数1,jdbcType=VARCHAR,mode=IN},#{参数2,jdbcType=INTEGER,mode=OUT})}(mode代表是输入参数,或者输出参数)
    map中只放入输入参数,不管输出参数,key跟参数名对应,value对应值

    **获取存储过程的输出参数,map.get("输出参数名")。
    **存储过程有输出参数,就不用设置返回值。在mapper接口中,返回值为void,存储过程没有返回值
    **mapper.xml的存储过程的语句要放在{}中
    **存储过程无论输入参数是什么值,语法上都需要用map来传递该值
    **增删改,只要是transactionmanager是jdbc,则增删改都需要提交事务

3.输出参数 resultType
 1)简单类型(8+String)
 2)对象类型
 3)集合list类型:虽然输出类型为元素类型,但是resultType还是student对象类型
 4)map类型:select stuno 'no' stuname 'name' from student 通过别名作为key,值为value
     --hashmap是一个集合,可以存放多个元素,但是返回值为hashMap时,查询结果只能时1个学生的属性(no,name)
     -->结论:一个hashmap对应一个学生的多个元素
     **如果存放多个学生,就需要多个map,list<Map>
4.resultMap:实体类的属性、数据表的字段不一致;属性与表字段名不一样
    属性与表字段名不一样情况:表:id,name 属性:stuno,stuname
    a.在xml中使用resultMap
    <resultMap type="student" id="xxx">
        <id property="stuNo" column="id" />主键用id
        <result property="stuName" column="name" />非主键用result
    </resultMap>
    b.使用hashMap,使用别名确定一一对应关系
        <select id="as" resultType="student" parameterType="int">
            select id "stuNo",name "stuName" from student(别名相当于hashmap)
        </select> 
    注意:如果10个字段,但发现 某一个字段结果始终为默认值(0,0,null),则可能是表的字段和属性名不一致
5.动态sql
查询全部:
String statement = "select * from student"
根据年龄查询
String statement = "select * from student where stuno=#{stuNo}"
根据姓名和年龄查询
String statement = "select * from student where stuno=#{stuNo} and stuage = #{stuAge}"

    <select id="as" resultType="student" parameterType="student">
        select id,name from student where
        <if test="stuName !=null and stuName!='' ">
            stuName = #{stuName}
        </if>
        <if test="stuAge !=null and stuAge!='' ">
            and stuAge = #{stuAge} 
        </if>
    </select> 
但是如果第一个不成立只有第二个就容易出问题,解决方案有2:
a:
    <select id="as" resultType="student" parameterType="student">
        select id,name from student where 1=1
        <if test="stuName !=null and stuName!='' ">
            stuName = #{stuName}
        </if>
        <if test="stuAge !=null and stuAge!='' ">
            and stuAge = #{stuAge} 
        </if>
    </select>
b:
    <select id="as" resultType="student" parameterType="student">
        select id,name from student
        <where>
        <if test="stuName !=null and stuName!='' ">
            and stuName = #{stuName}
        </if>
        <if test="stuAge !=null and stuAge!='' ">
            and stuAge = #{stuAge} 
        </if>
        </where>
    </select>  
<foreach>迭代的类型:数组、对象数组、集合、属性(Grade类:List<Integer> ids)
*属性(Grade类:List<Integer> ids)年纪里有很多学生,list集合里放的是学号,传入参数是一个list几个
    <select id="as" resultType="student" parameterType="grade">(grade是一个类,类中包含list<Integer>类型的stuNos参数)
        select id,name from student
        <where>
        <if test="stuNos !=null and stuNos.size>0 ">
            <foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    </select> 
*简单类型数组:将多个元素值放入数组中,数组名为stuNos
    <select id="as" resultType="student" parameterType="int[]">(参数为数组)
        select id,name from student
        <where>
        <if test="stuNos !=null and stuNos.size>0 ">
            <foreach collection="stuNos" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    </select> 
    **这样识别不了,因为参数不重要。无论编写代码时,传递的是什么参数名(stuNos),在mappper.xml中必须用list代替该数组
    <select id="as" resultType="student" parameterType="list">(参数为list)
        select id,name from student
        <where>
        <if test="list !=null and list.size>0 ">
            <foreach collection="list" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    </select> 
*对象数组,将多个元素放入对象数组中Student[] stu = {stu1,stu2,stu3}
    <select id="as" resultType="student" parameterType="Object[]">(不管是什么类型的对象数组,都要写Object[])
        select id,name from student
        <where>
        <if test="array !=null and array.length>0 ">
            <foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    </select> 
*SQL片段:
    java:方法;
    数据库:存储过程、存储函数
    Mybatis:SQL片段
    做法:
    a.提取相似代码
    b.引用
    在mapper.xml中,假设一下代码比较常用
        <where>
        <if test="array !=null and array.length>0 ">
            <foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    可以放在sql标签里
    <sql id="ass">
        <where>
        <if test="array !=null and array.length>0 ">
            <foreach collection="array" open=" and stuno in (" close=")" item="stuNo" separater=",">
                #{stuNo}
            </foreach>
        </if>
        </where>
    </sql>
    然后在常用的地方
    <select id="as" resultType="student" parameterType="list">(参数为list)
        select id,name from student
        <include refid = "ass" ></include>
    </select> 
    如果代码引用不在一个文件里,需要加上namespace全名+id

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦幻D开始

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

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

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

打赏作者

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

抵扣说明:

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

余额充值