zoukankan      html  css  js  c++  java
  • MyBati__mapper 中取值(#{} 或${}) 以及 parameterType为(基本类型 或复杂类型)

    参考资料:

    MyBatis学习笔记(三)——parameterType为基本类型时的使用方法

    MyBatis的传入参数parameterType类型

    1. MyBatis的传入参数parameterType类型分两种

       1.1  基本数据类型:int,string,long,Date;

       1.2  复杂数据类型:类和Map

    2. 如何获取参数值:

       2.1  基本数据类型:#{随意起个名字}  或  ${_parameter} 或 ${value}   注意这里的区别

       2.2  复杂数据类型:#{属性名} 或 #{属性名} ,map中则是#{key} 或 ${key} 

    特殊情况:

    order by 后面只能用 ${}取值。

    例如,传入类型为 string ,并且order by,那么只能写成以下两种

    1 <select id="selectByAge2" resultType="stu">
    2     select * from table1 order by ${value}
    3 </select>
    4 
    5 <select id="selectByAge3" resultType="stu">
    6     select * from table1 order by ${_parameter}
    7 </select>

    另外,当传入类型为基本类型,没有用${_parameter} 或 ${value}这样取值时,会报类似以下错误(根据传入类型的基本类型的不同有所不同)。

    例如:传入类型为 string,取值写成  ${id}

    1 <select id="selectByAge4" resultType="stu">
    2     select * from table1 order by ${id}
    3 </select>

    报错:

    1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'

    传入类型为 string ,并且order by的实例代码:

    mapper.xml 文件内容:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.xxx.maven.mapper.StudentMapper">
    
    <!-- 需要传参的sql语句  #{} 和 ${} 取值的区别-->
         
          <select id="selectByAge1"  resultType="stu">
            select * from table1 order by #{id}
        </select>
        
        <select id="selectByAge2" resultType="stu">
            select * from table1 order by ${value}
        </select>
        
          <select id="selectByAge3" resultType="stu">
            select * from table1 order by ${_parameter}
        </select>
        
        <select id="selectByAge4" resultType="stu">
            select * from table1 order by ${id}
        </select>
        
    </mapper>

    对应测试类中调用方法的代码:

     1 //<!-- 需要传参的sql语句  #{} 和 ${} 取值的区别-->
     2         // #{id}  
     3         @Test
     4         public void selectByAge1() {
     5             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge1","id");
     6             for(Student student:list){
     7                 System.out.println(student);
     8             }    
     9         }
    10         // ${value} 
    11         @Test
    12         public void selectByAge2() {
    13             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge2","id");
    14             for(Student student:list){
    15                 System.out.println(student);
    16             }
    17         }
    18         // ${_parameter}  
    19         @Test
    20         public void selectByAge3() {
    21             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge3","id");
    22             for(Student student:list){
    23                 System.out.println(student);
    24             }
    25         }
    26         // #{id}  
    27         @Test
    28         public void selectByAge4() {
    29             
    30             List<Student> list =  session.selectList("com.qphone.maven.mapper.StudentMapper.selectByAge4","id");
    31             for(Student student:list){
    32                 System.out.println(student);
    33             }
    34         }
    测试方法  selectByAge1  返回查询结果,但是order by不会起作用
    测试方法  selectByAge2  返回查询结果,order by会起作用
    测试方法  selectByAge3  返回查询结果,order by会起作用
    测试方法  selectByAge4  报错

    1 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'id' in 'class java.lang.String'
  • 相关阅读:
    一个滑动小块
    js 封装的函数 总结
    引用公用文件
    静态页面表单中js验证
    linux中Firefox浏览器 手动安装 flash
    将双击“root的主文件”弹出的窗口设置为文件浏览器
    eclipse中的aptana插件的安装
    System Center VMM请注意不同语言版本的差异
    CodeFirst模式开发涉及到mysql简单使用
    Echarts的使用
  • 原文地址:https://www.cnblogs.com/mumu122GIS/p/9982409.html
Copyright © 2011-2022 走看看