mybatis 一点整理

时间:2023-12-19 23:14:08

mapper指定对应的接口

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xx.xx.xx.dao.xxMapper">

多指定几个

<sql id="someXXXFields">
        x.id as id,
        x.name as name,
        x.age as age,
       ...
 </sql>

select 子句在写具体查询语句时候可以灵活拼接

<select id="getMessage" parameterType="String" resultMap="xxResultMap">
   select
        <include refid="someXXXFields"/>
        ,
        <include refid="someYYYFields"/>
   from XXX x left join YYY y on x.id = y.id
        <where>
            <include refid="conditionWhere"/>
        </where>

</select>

那么显然where子句也能进行指定   (1) if test        (2) foreach collection

 <sql id="orderWhere">
        <if test="orderNum!=null">
            and x.order_num = #{orderNum}
        </if>
        <if test="orderNumList!=null">
            and x.order_num in
            <foreach collection="orderNumList" item="orderNum" open="(" separator="," close=")">
                #{orderNum}
            </foreach>
        </if>
 </sql>

choose  when otherwise

 <choose>
     <when test="country != null">
           and country = #{country}
     </when>
     <otherwise>
           and country is null
     </otherwise>
</choose>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.qunar.hotel.otatts.provider.order.dao.OrderInfoMapper">