4.MyBatis(2)

-----------------------------------------------------mapper代理------------------------------------------------------------
1.mybatis不允许方法的重载;
2.使用mapper接口,再mybatis.xml文件中扫描方式,接口名和xxxmapper.xml名称一致
<mappers>
        <!--扫描xml-->
        <!--<mapper resource="com\junxie\mapper\FlowerMapper.xml"></mapper>-->
        <!--扫描包:先扫描包下的接口,再扫描包下和接口名一致的xml文件-->
        <package name="com.junxie.mapper"/>
    </mappers>
3.mapper.xml文件中的namespace属性必须是接口的全路径名;
4.sql中的id必须和接口中的方法名一致;
------------------------------------------------------------------------------面试题
mybatis获得主键自增:
方式一:
<!--useGeneratedKeys:开启主键自增方式
    keyProperty:设置主键自增再那个实体类属性(数据表列上)-->
    <insert id="insert" useGeneratedKeys="true" keyProperty="id">
        insert into flower values (default ,#{name},#{price},#{production});
    </insert>
方式二:
<insert id="insert" >
        <selectKey order="AFTER" keyProperty="id" resultType="int">
            select @@identity 
        </selectKey>
        insert into flower values (default ,#{name},#{price},#{production});
    </insert>
-----------------------------------------------------动态SQL----------------------------------------------------------------
    if标签:
注意:书写时注意符号的中英文,尤其是感叹号写错不容易发现;
------------------------------------------------------------------------------
<select id="selectMore" resultType="Flower">
        select * from flower where 1=1
        <if test="param1 != null and param1 !=''">
             and name=#{param1}
        </if>
        <if test="param2 != null and param2 !=''">
             and production=#{param2}
        </if>
</select>
------------------------------------------------------------------------------
    where标签
1.会自动的添加where关键字,并且会自动去除第一个and;
------------------------------------------------------------------------------
<select id="selectMore2" resultType="Flower">
        select * from flower
        <where>
            <if test="param1 != null and param1 !=''">
                    and name=#{param1}
            </if>
            <if test="param2 != null and param2 !=''">
                    and production=#{param2}
            </if>
        </where>
</select>
------------------------------------------------------------------------------
    choose标签
1.相当于if else语句,只要一个成立,其他的都不会继续;
------------------------------------------------------------------------------
 <select id="selectMore3" resultType="Flower">
        select * from flower
        <where>
            <choose>
                <when test="param1!=null and param1!=''">name=#{param1}</when>
                <when test="param2!=null and param2!=''">production=#{param2}</when>
                <otherwise>1=1</otherwise>
            </choose>
        </where>
    </select>
------------------------------------------------------------------------------
    set标签
1.会增加set关键字,会去掉最后一个逗号;
------------------------------------------------------------------------------
<update id="update">
        update flower
        <set>
            <if test="name!=null and name!=''">name=#{name},</if>
            <if test="price!=null and price!=''">price=#{price},</if>
            <if test="production!=null and production!=''">production=#{production}</if>
        </set>
        where id =#{id}
    </update>
------------------------------------------------------------------------------
    trim标签、
1.<!--prefix:前缀
    suffix:后缀
    suffixOverrides:去掉后缀
    prefixOverrides:去掉前缀-->
2.功能强大,可以充当大部分关键字,比如where,set ,
------------------------------------------------------------------------------
    <update id="update2">
        update flower
        <trim prefix="set" suffix="" suffixOverrides=", " prefixOverrides="">
            <if test="name!=null and name!=''">name=#{name},</if>
            <if test="price!=null and price!=''">price=#{price},</if>
            <if test="production!=null and production!=''">production=#{production}</if>
        </trim>
        where id =#{id}
    </update>
------------------------------------------------------------------------------
    foreach标签
1.查询指定条件的数据信息;
------------------------------------------------------------------------------
 <!--collection:指定遍历的集合
    item="id":循环遍历的每一个值
open="(" 以xxx开始
separator="," 以xxx分割
 close=")"--> 以xxx结束
    <select id="selectMore" resultType="Flower">
        select * from flower where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
------------------------------------------------------------------------------
    bind 标签
1.注意:concat的使用与bind标签的作用相同
------------------------------------------------------------------------------
<select id="selectMore" resultType="Flower">
        select * from flower
        <where>
            <if test="param1!=null and param1!=''">
                name like concat('%',#{param1},'%')
            </if>
            <if test="param2!=null and param2!=''">
               and  production like concat('%',#{param2},'%')
            </if>
        </where>
    </select>
------------------------------------------------------------------------------
<select id="selectMore" resultType="Flower">
        select * from flower
        <where>
            <if test="param1!=null and param1!=''">

                <bind name="param" value="'%'+param1+'%'"/>
                name like #{param}
            </if>
            <if test="param2!=null and param2!=''">
               and  production like concat('%',#{param2},'%')
            </if>
        </where>
    </select>
------------------------------------------------------------------------------
     sql标签和include标签
1.sql标签可以把共用的sql提取出来,然后用include标签嫁接到动态sql中;
------------------------------------------------------------------------------
<sql id="sql1">
        select * from flower
    </sql>
    <select id="selectMore" resultType="Flower">
        <include refid="sql1"></include>
        <where>
            <if test="param1!=null and param1!=''">

                <bind name="param" value="'%'+param1+'%'"/>
                name like #{param}
            </if>
            <if test="param2!=null and param2!=''">
               and  production like concat('%',#{param2},'%')
            </if>
        </where>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值