zoukankan      html  css  js  c++  java
  • mybatis mapper映射文件全解

      

    目录


    select、update、delete、insert

    设置参数类型以及取值

    基本数据类型

    对象数据类型

    map数据类型

    #{  } 和 ${  } 的区别

    ResultMap

    Auto-mapping

    cache


    select、update、delete、insert

      这分别对应有四个标签<select>、<update>、<delete>、<insert>,在绑定sql语句的时候,可以有很多的属性,属性可以见名知意,具体如下:

    <select
      id="selectPerson"
      parameterType="int"
      resultType="hashmap"
      resultMap="personResultMap"
      flushCache="false"
      useCache="true"
      timeout="10000"
      fetchSize="256"
      statementType="PREPARED"
      resultSetType="FORWARD_ONLY">
    
    <insert
      id="insertAuthor"
      parameterType="domain.blog.Author"
      flushCache="true"
      statementType="PREPARED"
      keyProperty=""
      keyColumn=""
      useGeneratedKeys=""
      timeout="20">
    
    <update
      id="insertAuthor"
      parameterType="domain.blog.Author"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">
    
    <delete
      id="insertAuthor"
      parameterType="domain.blog.Author"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">
    

      对于<select>、<insert>、<update>、<delete>,SqlSession分别有两个方法与之对应:

    <select>
    int SqlSession.select(String statement)
    int SqlSession.select(String statement, Object parameter)
    
    <insert>
    int SqlSession.insert(String statement)
    int SqlSession.insert(String statement, Object parameter)
    
    <update>
    int SqlSession.update(String statement)
    int SqlSession.update(String statement, Object parameter)
    
    <delete>
    int SqlSession.delete(String statement)
    int SqlSession.delete(String statement, Object parameter)
    

      

    设置参数类型以及取值

      设置参数类型,是通过设置parameterType的值,设置的类型不同,sql中取值的方式也有所区别。

      parameterType属性可以有下面几种取值:

      1、基本数据类型

        比如int、double,其实应该写为Integer、Double,但是有自动装箱功能,所以可以直接写基本数据类型。

        sql取值的方式:对于这种的参数,在接收传入的参数值时,可以使用#{index}来获取,注意,index从0开始计数,如果只有一个参数,那么index可以写成任意名称。

    <mapper namespace="cn.ganlixin.mapper.PersonMapper">
    
    	<select id="selectPersonById1" parameterType="int" resultType="cn.ganlixin.pojo.Person">
    		select * from person where id=#{0}
    	</select>
    	
    	<!-- 如果只有参数,可以将#{0} 写为任意名称 -->
    	<select id="selectPersonById2" parameterType="int" resultType="cn.ganlixin.pojo.Person">
    		select * from person where id=#{id}  
    	</select>
    </mapper>
    

      

      2、Object类型

        可以写实体类(如果没有配置alias,需要写全路径),比如cn.ganlixin.pojo.Person类。

        sql取值的方式:对于对象这种类型,可以使用#{property}去访问实体类中的property属性值,比如#{age},可以获取Person的age属性值。

    <mapper namespace="cn.ganlixin.mapper.PersonMapper">
    	<select id="selectPersonByName"
    			parameterType="cn.ganlixin.pojo.Person" 
    			resultType="cn.ganlixin.pojo.Person">
    			
    		select * from person where name=#{name}  
    		<!-- 相当于取Person.name的值 -->
    	</select>
    </mapper>
    

        简单测试:

    String method = "cn.ganlixin.mapper.PersonMapper.selectPersonByName";
    Person person = new Person();
    person.setName("aaaa");
    person = (Person)  session.selectOne(method, person);
    

      

      3、map类型

        将要传入一个数据,封装到一个map中,然后再传入即可。

        sql取值的方式:对于map的取值,和object的取值类似,使用#{key}即可获取map中key对应value。

    <mapper namespace="cn.ganlixin.mapper.PersonMapper">
    	<select id="selectPersonByName" parameterType="map" resultType="cn.ganlixin.pojo.Person">
    		select * from person where name=#{name}  
    		<!-- 相当于取map.get(name)的值 -->
    	</select>
    </mapper>
    

        简单测试:

    String method = "cn.ganlixin.mapper.PersonMapper.selectPersonByName";
    Map<String, String> data = new HashMap<>();
    data.put("name", "aaaa");
    Person person = (Person)  session.selectOne(method, data);
    

      

      4、#{  } 和 ${  } 的区别

      需要注意的是,sql中取值可以使用#{  } 和 ${  }两种方式,区别在于:

      1、#{  } 会使用预处理操作,而${  }不会进行预处理操作。

      2、${  } 只是很简单的使用字符串拼接

    select * from person where name='${name}'
    如果上面不适用引号将${name}括起来,那么就会将${name}的值当做字段名,而不是字段值
    
    select * from person where name=${0}
    上面这条sql语句,并不会使用传入的第1个参数,而是真的name=0
    

      

    ResultMap

       关于resultMap的使用,可以查看:

      mybatis 使用resultMap实现表间关联

    Auto-mapping

       关于auto-mapping的使用,可以查看:

        mybatis 使用auto mapping原理实现表间关联

    cache

      mybatis中的缓存有两种,一级缓存和二级缓存。

      可以查看:mybatis 使用缓存策略

       

  • 相关阅读:
    python自学Day01(自学书籍python编程从入门到实践)
    关于要不要转行做程序员的建议
    什么是高并发?
    看了就想收藏的文章链接
    java反射的理解
    Mac执行sh批处理文件出现permission denied
    Error running 'lis [clean]': No valid Maven installation found. Either set the home directory in the configuration dialog or set the M2_HOME environment variable on your system.
    Error running 'Tomcat 8.5.37': Unable to open debugger port (127.0.0.1:55358): java.net.SocketException "socket closed"
    浅谈JS-ES6新特性
    ==和equals的区别
  • 原文地址:https://www.cnblogs.com/-beyond/p/10114730.html
Copyright © 2011-2022 走看看