zoukankan      html  css  js  c++  java
  • mybatis对象的插入

    或者:

    传入JAVA对象

    mapper接口代码:

    public int findUserList(User user);   

    xml代码:

    复制代码
    <select id="findUserList" parameterType="User" resultType="java.lang.Integer">    
            SELECT COUNT(*) FROM USER user    
            <where>    
                <if test="code != null">     
                    and user.CODE = #{code}     
                </if>    
                <if test="id != null">     
                    and user.ID = #{id}     
                </if>    
                <if test="idList !=null ">    
                    and user.ID in (    
                        <foreach  collection="idList" item="id" index="index" separator=",">   
                             #{id}   
                        </foreach>    
                    )    
                </if>    
            </where>    
    </select>    
    复制代码

     JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。

    比如这里User含有一个名为idList的list,所以User中用idList取值,这点和单独传list或array时不太一样。

    注解@Param

       注解单一属性;这个类似于将参数重命名了一次

    例子1:

    mapper接口代码:

    List<User> selectUserByTime(@Param(value="startdate")String startDate);  

    xml代码:

    <select id="selectUserByTime" resultType="User" parameterType="java.lang.String">    
        select * from t_user   
        where  create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD')  
    </select>  

    例子2:

    注解javaBean

    mapper接口代码:

    List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);  

    xml代码:

    <select id="selectUserByTime" resultType="User" parameterType="DateVO">    
        select *  
        from t_user  
        where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD')   
              and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD')   
     </select>   
  • 相关阅读:
    Java 类和对象12
    Java类和对象11
    java 类和对象10
    Java类和对象9
    Java类和对象8
    Java 类和对象7
    包装与方法
    JAVA链表
    泛型
    多态 接口
  • 原文地址:https://www.cnblogs.com/leeego-123/p/10497127.html
Copyright © 2011-2022 走看看