MyBatis--智能标签

先说一个自己总能碰见的问题,今天终于解决
在Test(测试类)中,引@Test有时会报错,
大多数是因为复制过来的代码所以造成这种原因,这个时候你肯定想到的是pom.xml,里面jar包没有引入,
可是如果引入了还报错是为什么呢,这是因为,你的XXX.iml里面没有加上这些

下面是智能标签:

<!--智能标签foreach List自定义-->
<select id="findByListGeneric" resultType="Student">
    select <include refid="columns"></include> from student
    <if test="list.size>0">
        WHERE stuno in
        <foreach collection="list" open="(" close=")" separator="," item="myitem">
            #{myitem.stuno}
        </foreach>
    </if>
</select>

<!--智能标签foreach List-->
<select id="findByList" resultType="Student">
    select * from student
    <if test="list.size>0">
        WHERE stuno in
        <foreach collection="list" open="(" close=")" separator="," item="myid">
            #{myid}
        </foreach>
    </if>
</select>


<!--智能标签foreach-->
<select id="findByArray" resultType="Student">
    select * from student
    <if test="array.length>0">
        WHERE stuno in
        <foreach collection="array" open="(" close=")" separator="," item="myid">
            #{myid}
        </foreach>
    </if>
</select>


<!--智能标签choose-->
<select id="findByChoose" resultType="Student">
    select * from student
    <where>
        <choose>
            <when test="stuname != null">
                and stuname like '%' #{stuname} '%'
            </when>
            <when test="stuage!=null">
                and stuage>#{stuage}
            </when>
            <otherwise>
                and 1=1
            </otherwise>
        </choose>
    </where>

</select>

<!--智能标签if-->
<select id="findByIf" resultType="Student">
    select * from student
    <where>
        <if test="stuname!=null">
            and stuname like '%' #{stuname} '%'
        </if>
        <if test="stuage!=null">
            and stuage>#{stuage}
        </if>
    </where>

</select>


下面是一对多:

<resultMap id="DeptMapperMultipleSQL" type="Dept">
    <id column="deptNo" property="deptNo"></id>
    <result column="deptName" property="deptName"></result>
    <collection property="emps" ofType="Emp" select="selectEmpsById" column="deptno">
    </collection>

</resultMap>
<select id="selectEmpsById" resultType="Emp">
    select * from  emp where deptno=#{deptNo}
</select>
<!--一对多的单条SQL解决方案-->
<select id="findEmpsByDeptNoMultipleSQL" resultMap="DeptMapperMultipleSQL">
    select deptName,deptNo
    from dept
    where deptno=#{deptNo}
</select>

<resultMap id="DeptMapper" type="Dept">
    <id column="deptNo" property="deptNo"></id>
    <result column="deptName" property="deptName"></result>
    <!--
       ofType:集合中单个元素的类型
    -->
    <collection property="emps" ofType="Emp">
        <id  column="empNo" property="empNo"></id>
        <result column="empName" property="empName"></result>
    </collection>
</resultMap>
<select id="findEmpsByDeptNo" resultMap="DeptMapper">
    select empNo,empName,deptName,dept.deptNo
    from emp,dept
    where emp.deptno=dept.deptno
    and dept.deptno=#{deptNo}
</select>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值