zoukankan      html  css  js  c++  java
  • 课时15::MyBatis处理多个参数问题

    .1)目前 将多个参数封装到有个pojo对象中,使用对象传递 --> 如果不用呢 传入的是多个参数

      1.传入多个参数时,不用在mapper.xml中编写parameterType,并且参数类型不是混合类型(又有对象又有简单的类型)

        1.1 方式一

          1.1.1 异常提示:stuNo不能使用,就可以使用的是:arg3, arg2, arg1, arg0, param1, param2, param3, param4这些参数 

     <insert id="addStudent"  databaseId="mysql"  useGeneratedKeys="true" keyProperty="stuno">
            insert into student(stuName,stuAge,graName,stuSex)
            values(#{param1},#{param2},#{param3},#{param4})
        </insert>
    或者
     <insert id="addStudent"  databaseId="mysql"  useGeneratedKeys="true" keyProperty="stuno">
            insert into student(stuName,stuAge,graName,stuSex)
            values(#{arg0},#{arg1},#{arg2},#{arg3})
        </insert>

          1.1.2 这样就可以实现多参数传递了 接口如下

     Integer addStudent(String stuName,Integer stuAge,String graName,Integer sex);

        1.2 方式二 命名参数 推荐使用

          1.2.1 可以再接口中这么写

     Integer addStudent(@Param("sName") String stuName, @Param("sAge")Integer stuAge,
                           @Param("gName")String graName, @Param("sSex")Integer sex);

          1.2.2 在sql映射文件直接写自定义的别名

     <insert id="addStudent"  databaseId="mysql"  useGeneratedKeys="true" keyProperty="stuno">
            insert into student(stuName,stuAge,graName,stuSex)
            values(#{sName},#{sAge},#{gName},#{sSex})
        </insert>

      2.如果一个入参有简单类型又有对象类型怎么办? 混合类型

        2.1 在接口编写

     Integer addStudent(@Param("sName") String stuName, @Param("stu")Student student);

        2.2 使用命名的名称.类中的属性

      <insert id="addStudent"  databaseId="mysql"  useGeneratedKeys="true" keyProperty="stuno">
            insert into student(stuName,stuAge,graName,stuSex)
            values(#{sName},#{stu.stuAge},#{stu.graName},#{stu.stuSex})
        </insert>

      

  • 相关阅读:
    Spark源码分析之Sort-Based Shuffle读写流程
    浅谈Spark2.x中的Structured Streaming
    Spark应用提交
    js面向对象插件的做法框架new goBuy('.cakeItem',{ add:'.add', reduce:'.reduce' },[1,0.7,0.6]);
    dubbo 运行过程
    linux 监控命令
    DUBBO Thread pool is EXHAUSTED!
    线程池深入(li)
    高性能、高流量Java Web站点打造的22条建议
    Maven 打胖jar
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12458268.html
Copyright © 2011-2022 走看看