• Mybatis参数处理


    一、单个参数

      单个参数,mybatis不会做特殊处理

      直接#{ } 括号里面的内容可以随意些。

    二、多个参数

      多个参数在明确封装参数时在每个参数 前面加上@param("参数名 ")

    接口:

    public void updateStu(@Param("sid")Integer sid,@Param("sname")String sname,@Param("score")String score,@Param("sclass")String sclass);
    //XMl文件
    <
    update id="updateStu"> update stu1 set sname=#{sname}, score=#{score},sclass=#{sclass} where sid=#{sid} </update>

    三、在接口传入多个参数时候不加@Param的情况下,如果继续在statement语句中 update stu1 set sname=#{sname},  score=#{score},sclass=#{sclass} where sid=#{sid}

      就会出错,

      但是按照接口参数的顺序,设定为param1.param2....,在#{}的里面按需要填写响应的param。

    <update id="updateStu">
            update stu1 set sname=#{param1},score=#{param2},sclass=#{param3} where sid=#{param4}
        </update>

    四、POJO

      如果多个参数都是POJO中的属性,我们就可以直接将POJO对象传入

      Java接口:

      

    public void insertStu(Student student);

      xml文件: 

      直接在#{ }中写POJO的属性,就可以将POJO的所对应的属性的值传入参数。

    <insert id="insertStu" parameterType="com.neuedu.bean.Student"
            useGeneratedKeys="true" keyProperty="sid">
            insert into stu1
            (sname,score,sclass) values(#{sname},#{score},#{sclass} )
    </insert>

    五、Map:

      如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入map

      Java接口:

    public void updateStuMap(Map<Object, Object>map);

      xml文件: 

    <!--public void updateStuMap(Map<Integer, String >map); 多个参数Map的修改 -->
        <update id="updateStuMap">
            update stu1 set sname=#{sname},
            score=#{score},sclass=#{sclass} where sid=#{sid}
        </update>

      在测试类中,要将传入的参数和参数对应的属性,以key-value的形式存入map中

      如:

    @Test
        public void testUpdateStu2() throws IOException{
            SqlSessionFactory sqlSessionFactory = sqlSessionFactory();
            SqlSession openSession = sqlSessionFactory.openSession();
            Stu mapper = openSession.getMapper(Stu.class);
            Map<String, String> map=new HashMap<>();
            map.put("sname", "xiaoming");
            map.put("score", "100");
            map.put("sclass", "1");
            map.put("sid", "1");
            mapper.updateStuMap(map);
            openSession.commit();
            openSession.close();
        }
  • 相关阅读:
    字符数组,字符指针,字符串常量以及其sizeof的一些总结
    Linux——makefile
    动态定义多维数组
    二叉树的前序、中序、后序遍历(非递归)
    构造函数、析构函数抛出异常的问题
    倒排索引
    宏定义
    sizeof && strlen
    搜索引擎技术原理
    最小生成树
  • 原文地址:https://www.cnblogs.com/xuesheng/p/7475348.html
Copyright © 2020-2023  润新知