美文网首页
Mybatis进阶

Mybatis进阶

作者: 蓝色Hippie | 来源:发表于2020-06-09 14:49 被阅读0次

一、Mybatis 参数处理

    1.1 单参

           单参数时,Mybatis会直接取出参数值给Mapper文件赋值,如#{id}

            如:DAO层:         void delete (Integer id);

            XML Mapper:<delete> delete from user where id=#{id}  </delete>

    1.2 多参

        三种形式:JavaBean传递参数、Map接口、@param注解

        1.2.1  JavaBean传递参数

             DAO:  User select(User user);// User实体类包含  name、sex属性

             mapper:  <select> select * from user where name=#{name} and sex=#{sex} </select>

        1.2.2 Map接口形式

             DAO:  User select(Map<String,Object> map);// map集合中放入 name、sex以及对应的值

             mapper:  <select> select * from user where name=#{name} and sex=#{sex} </select>

        1.2.3  @param注解

             DAO:  User select(@Param("username") String name,@Param("param2") String sex);

             mapper:  <select> select * from user where name=#{username} and sex=#{param2} </select>

    1.3 集合类型参数

       如:    DAO:  User select(Collection list);

              mapper:  <select> select * from user where name=#{collection[0]} and sex=#{collection[1]} </select>

        PS:可自定义集合的key

        如:      DAO:  User select(@Param("test") int[] arr);

              mapper:  <select> select * from user where name=#{test[0]} and sex=#{test[1]} </select>

二、动态sql

    2.1 foreach

        特点:循环遍历集合,支持数组和List、Set接口,对其提供遍历功能

        配置:collection-要循环的集合;    item-迭代出的每项;

        index-索引;    open\close-以什么开头和结尾;        separator-分隔符;

        如: DAO:  User select(@Param("test") int[] arr);

         mapper: 

         <select> 

        select * from user where  id  in 

        <foreach collection="test"  item="id" index="i" open="("  close=")" separator=",">

        #{id}

        </foreach>

         </select>

三、批量操作

    3.1 、回顾传统 jdbc 批量插入的方式

        两种:使用for循环执行sql插入;

        statement/parestatement中的addBatch,批量提交;

    3.2 、 mybatis批量-1

        借助foreach标签,使用insert into table()

方式1

    3.3 、mybatis批量-2

        借助mysql数据库连接属性 allowMultiQueries=true

        url="jdbc:mysql://localhost:3306/mybatis?allowMultiQueries=true";

方式2

    3.4、mybatis批量-3

        基于sqlsession的ExecutorType进行批量添加

        private static String RESOURCE="mybatis-config.xml";

        InputStream is= Resources.getResourceAsStream(RESOURCE);

        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);

        sqlSession .update("updateUser", user);//可利用for循环更新或新增多个用户

        sqlSession.commit();

        sqlSession.close();

相关文章

  • Mybatis进阶

    基本知识 resultMap constructor–实例化的时候通过构造器将结果集注入到类中oidArg– ID...

  • MyBatis进阶

    对象之间的关系: 关联关系:A对象依赖B对象,并且把B对象作为A对象的一个属性,则A和B是依赖关系. ** 按照多...

  • mybatis进阶

    1. insert返回主键id 添加useGeneratedKeys,keyProperty即可。 KeyProp...

  • Mybatis进阶

    一、Mybatis 参数处理 1.1 单参 单参数时,Mybatis会直接取出参数值给Mapper文件赋值,...

  • 【Mybatis】 02 - Mybatis 进阶

    1. 知识回顾 1.1 回顾自定义Mybatis流程分析 1.2 回顾Mybatis环境搭建-以及实现查询所有的功...

  • SpringBoot入门建站全系列(四)Mybatis使用进阶篇

    SpringBoot入门建站全系列(四)Mybatis使用进阶篇:动态SQL与分页 上一篇介绍了Mybatis的配...

  • 精雕细琢!阿里大师53天悉心打磨出来的MyBatis+设计模式架

    全文内容目录一览 Java设计模式实践指南(字节跳动版) MyBatis入门到进阶(含面试题解) MyBatis底...

  • 自己实现一个缓存

    基本框架 提高 进阶:经典责任链及变种模式 mybatis缓存的变种责任链

  • Mybatis进阶教程

    前言 接着上一篇Mybatis入门继续,上一篇主要演示了Mybatis的基本操作,对数据库的增删改查,但是在实际项...

  • MyBatis 进阶-ResultMap

    ResultMap 是 MyBatis 中最重要最强大的元素 可以实现复杂查询结果到复杂对象关联的关系转化复杂 J...

网友评论

      本文标题:Mybatis进阶

      本文链接:https://www.haomeiwen.com/subject/dbpxfhtx.html