mybatis中的参数传递

在mybatis中有两种方法获取参数内容

1.#{}获取参数内容

 使用索引,从 0 开始 #{0}表示第一个参数
         也可以使用#{param1}第一个参数
         如果只有一个参数(基本数据类型或 String),mybatis对#{}里面内容没有要求只要写内容即可.
          如果参数是对象#{属性名}
          如果参数是 map 写成#{key}
          #{} 获取参数的内容支持 索引获取,param1 获取指定位置参数,并且 SQL 使用?占位符
          如果在 xml 文件中出现 “<” , “>” ,双引号等特殊字符时可以使用<![CDATA[ 内容 ]]>

其底层sql语句通过占位符实现,可以防止sql注入

<select id="selById" parameterType="int" resultType="teacher">
        select * from teacher where id=#{0}
    </select>

2.${}获取参数内容:

${} 字符串拼接不使用?,默认找${内容}内容的 get/set 方法,如
果写数字,就是一个数字
<select id="selById" resultType="People" parameterType="map">
        select * from people where id=${id} and name='${name}'
    </select>

猜你喜欢

转载自blog.csdn.net/m0_38078065/article/details/85332460