zoukankan      html  css  js  c++  java
  • 使用reusltMap实现高级映射,工具类,别名,多条件查询以及模糊查询

    1.

    SQL映射文件的根节点是mapper元素,需要指定namespace来区别于其他的mapper,保证全局唯一,并且其名称必须要跟接口同名,作用是绑定DAO接口,即面向接口编程。

      resultType:

      resultType直接表示 返回 类型 ,包括基础类型和复杂数据类型


      resultMap:

      resultMap则是对外部resultMap的引用,对应resultMap的id 表示返回结果映射到 哪一个resultMap。

      他的应用场景是:数据库字段信息与对象属性不一致或者需要做复杂的联合查询以便自由控制映射结果 。


      resultType与resultMap两者的区别:

      在 MyBatis的select元素中,resultType和resultMap本质上是一样的,都是Map数据结构。但是 二者不能同时 存在。


      resultMap的自动映射级别

       MyBatis中分为三个映射级别:

        NONE:禁止自动匹配

        PARTIAL:(默认):自动匹配所有属性有内部嵌套(association,collection)的除外

        FULL:自动匹配所有  


      MyBatis的SQL语句参数入参:

      对于基础数据类型的参数数据,使用@param注解实现参数入参;复杂数据类型的参数直接入参即可。


      resultMap的association 和 collection 可以实现高级结果映射。

      association属性有:

       javaType:完整Java类名或者别名。若映射到一个JavaBean,则MyBatis通常会自行检测到其类型;

       若映射到一个HashMap,则应该明确指定JavaType,来确保所需行为。  

       property:映射数据库列的实体对象的属性。


      collection的属性有:

      ofType:完整Java类名或者别名,及集合所包含的类型。  

      property:映射数据库列的实体对象的属性。 

    2.别名
        <typeAliases><!--类型 别名-->
            <!--<typeAlias type="cn.happy.entity.Dept" alias="Dept"></typeAlias>-->
            <!--方式二:可以将被扫描包的简单类名作为 别名-->
            <package name="cn.happy.entity"></package>
        </typeAliases>
      可以在小配置中直接使用我们定制的简单名称进行数据操作

     

    3.工具类

     都是静态的
      public class MyBatisUtil {
        static String resouce="mybatis-config.xml"; 
        static InputStream is ; 
        static SqlSessionFactory factory; 
        static {
            try {
                is= Resources.getResourceAsStream(resouce);  
                factory= new SqlSessionFactoryBuilder().build(is); 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static SqlSession getSession(){  
            return factory.openSession(true);
        }
    }

    4..多条件查询

      select * from student
      where 1=1

     多条件查询Map
        1)dao
           public List<Student> getStudentByMultiCondition(Map<String,Object> map);
       
       
        2) xml中
          <select id="getStudentByMultiCondition" resultType="Student">

        where stuname like concat('%',#{stuname},'%')
          </select>
       
       
        3)测试类
          Map<String,Object> map=new HashMap<String,Object>();
          map.put("stuname","发哥");

     多条件查询,索引号
       05.查询姓名中包含“雨”,并且年龄>20的学生信息
        public  List<StudentInfo> findStudentsByConditionMutliArgs(String stuName,int stuAge);
     
         <!--多条件查询使用索引-->
        <select id="findStudentsByConditionMutliArgs" resultType="StudentInfo">
            select * from  studentinfo where stuname like '%' #{0} '%' and stuAge>#{1}
        </select>

     5.模糊查询

      1).select * from studentinfo where stuname like '%' #{stuname} '%'   ? +++++++++ sql   数据库服务器
      2).select * from studentinfo where stuname like concat('%',#{stuname},'%')
      3).select * from studentinfo where stuname like '%${stuname}%'  客户端的查询内容连同%%符号 就被发送到了服务器

    6.openSession底层到底做了什么:

     1.session.commit();  SqlSession接口的
      2.我们知道的是 SqlSession接口的实现类是DefaultSqlSession
      3.找到实现类的commit
        public void commit() {
        commit(false);
        }
      4.
      public void commit(boolean force) {
        try {
          executor.commit(isCommitOrRollbackRequired(force)); //去调度执行器的commit,入参进来的结果为true
          dirty = false;  //事务结束后,恢复dirty的值
      }
      5.
       public void commit(boolean required) throws SQLException {  required 为真
        if (required) {
          transaction.commit();//引起事务提交
        }
      }

    session.commit();引起事务的提交
        Transaction.commit();
        关于dirty: factory.openSession(); dirty=false
        CUD  : 底层都是update,在try中共第一行code dirty=true
                直接影响到了,方法判定  dirty||force
                if(required){
              transaction.commit();
              dirty=false;
            }

  • 相关阅读:
    docker 的官方PHP镜像 如何修改 php.ini 配置
    数据结构和算法
    接口的幂等性
    linux ftp服务器设置,只允许用户访问指定的文件夹,禁止访问其他文件夹
    rabitmq + php
    微信上传客服消息图片
    golang Printf 函数有超过 10 个转义字符
    golang bufio.NewScarme
    springboot(一)
    springboot加入第三方jar,使用mvn打包
  • 原文地址:https://www.cnblogs.com/hsa521/p/7678382.html
Copyright © 2011-2022 走看看