【JAVA】mybatis xml中的语句 「if」中 相等或不等

本文详细解析了MyBatis映射文件的配置方法,包括命名空间、ResultMap、Insert、Update、Delete和Select语句的使用技巧,以及如何通过<if>标签进行条件判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写查询语句的时候出现的错,记录一下: 

<?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.mybatis.demo.domain.dao.cfDAO">
    <resultMap id="techermap" type="实体类的地址(和数据库表中字段一致的定义的类)">
        <id property="id" column="字段名1" javaType ="java.lang.String"/>
        <result property="amt" column="字段名2" javaType ="java.math.BigDecimal"/>
    </resultMap>

    <!--insert 是要和这个xml同名的xxxDAO.java中的定义名字一致-->
    <insert id="insert">
       insert into demo (id ,amt) values (#{id ,jdbcType=VARCHAR},#{amt,jdbcType=VARCHAR})
    </insert>

    <update id ="XXS">
        update demo 
        <set>
            <if test="id != null and id !=''">
                id =#{id ,jdbcType=VARCHAR},
            </if>
        </set>
    </update>

    <delete id ="deleteByKey">
        delete from demo where id = #{id}
    </delete>

    <select id ="findByWhere">
        select id from demo 
        <where>
            amt ='10' and ((Date3!= '2020630' AND Date4!= '2020730') OR (Date3== '2020430' AND Date4== '2020530'))
            <if test="accDate!=null and accDate!= ''">
                and accDate = #{accDate}
            </if>
            <!--Date在数据库中没有该字段,定义类中有这个字段-->
            <if test="Date1!=null and Date1!= '' and Date2!=null and Date2!= ''">
                and Date between  #{Date1} and #{Date2} 
            </if>
             <if test="Date1!=null and Date1!= ''">
                <if test='Date2==null and Date2== "" '>
                    and Date  = #{Date1}
                </if>
            </if>
        </where>

    </select>
</mapper>

 注: 在判断时候,<if> 不等于的时候可用 “ ”,字段的值可以用‘’;

                               相等的时候用‘’,字段的值可以用“”。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值