Java_myBatis_XML代理_动态SQL

主要是设计到映射文件的编写:

SELECT:

<sql id="query_user_where">
    <!-- test里面可以编写OGNL表达式 -->
    <!-- 判断字符串不为空的标准写法 -->
    <if test=" user.username != null and user.username !='' ">
         AND username like '%${user.username}%'
    </if>
</sql>

<!-- 分页查询之查询记录 -->
<select id="findUserList" parameterType="UserQueryVO"
    resultType="user">
    SELECT * FROM user
    <where>
        <include refid="query_user_where" />
    </where>
</select>

if语句前面必须带AND,where会自己判断什么时候该去除

UPDATE:

<update id="testDSQL" parameterType="User">
    UPDATE user 
    <set>
        <if test="username != null and username != ''">
            username = #{username},
        </if>
        <if test="address != null and address != ''">
            address = #{address},
        </if>
    </set>
    WERE id = 36
</update>

if最后的逗号是必须写的,set会自己判断什么时候去除

INSERT:

<insert id="insertSelective" parameterType="com.kkb.mybatis.pojo.Account" >
  <selectKey resultType="java.lang.Integer" keyProperty="id" order="AFTER" >
    SELECT LAST_INSERT_ID()
  </selectKey>
  INSERT INTO account
  <trim prefix="(" suffix=")" suffixOverrides="," >
    <if test="name != null" >
      name,
    </if>
    <if test="money != null" >
      money,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides="," >
    <if test="name != null" >
      #{name,jdbcType=VARCHAR},
    </if>
    <if test="money != null" >
      #{money,jdbcType=DOUBLE},
    </if>
  </trim>
</insert>

if最后的逗号是必须写的,trim会自己判断什么时候去除

 

转载于:https://www.cnblogs.com/amiezhang/p/9615195.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值