Mybatis标签

1.类型别名:

<configuration>
        <typeAliases>
                    <typeAliase alias="别名" type="类型地址"/>
        </typeAliases>
<configuration>

2.给一个包起别名(包中的类的别名就是他的类名)      

<tyoeAliases>
        <package name="包名"/>
</tyoeAliases>

3.增删改查标签的属性

          3.1:传入参数:parameterType="数据类型";

          3.2:指定返回类型:

                   1:resultType="数据类型地址";

                   2.:resultMap="result的id";

4.得参数的两种方法#{ } ${ }

            简单类型(int,String等):#{这随便写}   ${value}  (一个参数的情况下多个参数写属性名) 

            复杂类型(类,数组等):#{属性名}  ${属性名}

 #{ }与${ }的区别:

        #{ }得参数的话会带有属性的符号   如String就会带双引号

        ${ }得参数的时候不会带属性符号

5.resultMap的属性与标签:          

<resultMap>
            <id column="数据库列名”  prooerty="类的属性名" jdbctype="数据库中的类型">
</resultMap>    

    5.1:多对一的标签(一个类中有另一个类作为属性):

<association property="作为属性的类名" JavaType="类型的地址">

    <id column="数据库列名” prooerty="类的属性名" jdbctype="数据库中的类型">
    <result column="数据库列名”  prooerty="类的属性名" jdbctype="数据库中的类型">

<result> </association>

    5.2一对多标签(一个类中有另一个类的集合)

<collection property="类的集合名" oftype="类的地址">
   <id column="数据库列名”  prooerty="类的属性名" jdbctype="数据库中的类型">
</collection>    

        所有的集合都用oftype指定

6.SQL片段:

      sql片段可以重复利用多次调用

     例:     

<select>
        <include refid="sql片段id">
</select>

<sql id=“名”>
        sql语句
</sql>

7.动态SQL语句

       7.1:where(where会自动识别是否是第一个where如果是第一个where会自动去掉and进行拼接)

<select id="selectByParams" parameterType="map" resultType="user">

  select * from user

     <where>

       <if test="id != null ">id=#{id}</if>

       <if test="name != null and name.length()>0" >and name=#{name}</if>

       <if test="gender != null and gender.length()>0">and gender = #{gender}</if>

     </where>

</select> 

       7.2choose(和switch很像)

<choose>

    <when test="条件">-------这个和case一样
          SQL语句
    </when>

     <otherwise>-----这个和default一样
           SQL语句
     </otherwise>
</choose>    

       7.3foreach

     <foreach collection="集合名" item="起个名" index="index" open="开始循环的拼接" close="结束循环的拼接" separator=",">

 
  

       #{起的名}

     </foreach>

       7.4遍历拼接:trim

<trim prefix="前缀" suffix="后缀" suffixoverrides="在每句的末尾追加内容"   

prefixoverrides="在每句前面追加的内容">


</trim>

       7.5 set


<update>

    update user

    <set>

      <if test="name != null and name.length()>0">name = #{name},</if>

      <if test="gender != null and gender.length()>0">gender = #{gender},</if>

    </set>

    where id = #{id}

  </update> 

8.if与choose的区别

         if会进入多次而choose只会进入一次

9.and与or的优先等级

          and的优先等级大于or,有and和or的语句会优先执行and部分最后才执行or

 

转载于:https://www.cnblogs.com/HQ0422/p/9994219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值