• 课时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>

      

  • 相关阅读:
    ubuntu搭建php开发环境记录
    zz-什么是网关,路由,dns,通俗讲解
    如何设置root用户密码
    zz三台centos7虚拟机设置相互免密码登录
    go之闭包及其应用
    网络是怎样连接的
    进程间通信方式探索
    现代操作系统——操作系统概念
    现代操作系统——硬件_IO设备——设备控制器和设备本身
    simotion byte/word ASCII码转换为字符、字符串
  • 原文地址:https://www.cnblogs.com/thisHBZ/p/12458268.html
Copyright © 2020-2023  润新知