zoukankan      html  css  js  c++  java
  • 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会自己判断什么时候去除

  • 相关阅读:
    32位和64位的区别
    Git--版本管理的使用及理解
    Maven使用详解
    记录centos7下tomcat部署war包过程
    SSM三大框架整合教程
    Mybatis 框架搭建实例
    Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)
    JDBC 操作数据库实例
    mysql 常用命令语法
    MySQL下载安装详情教程(Windows)
  • 原文地址:https://www.cnblogs.com/amiezhang/p/9615195.html
Copyright © 2011-2022 走看看