mybatis 传参

使用#{}或${}:
两者的区别
  • #{}支持根据索引获取参数
  • #{}使用?占位符,${}直接拼接字符串
  • 占位符?不允许进行数学运算
  • ${}获取String参数时,需要添加引号
一、#{}
  • 传一个值(基本数据类型或String):根据索引获取
    • <select id="selOne" resultType="com.mybatis.pojo.Student" parameterType="int">
    •     select * from stu where id=#{0}
    • </select>
  • 传一个对象
    • <select id="selOne" resultType="com.mybatis.pojo.Student" parameterType="com.mybatis.pojo.Student">
    •     select * from stu where id=#{id} and name=#{name}
    • </select>
  • 传一个map
    • <select id="selOne" resultType="com.mybatis.pojo.Student" parameterType="map">
    •     select * from stu where id=#{id} and name=#{name}
    • </select>
 
二、${}
  • 传一个对象
    • <select id="selOne" resultType="com.mybatis.pojo.Student" parameterType="com.mybatis.pojo.Student">
    •     select * from stu where id=${id} and name=${'name'}
    • </select>
  • 传一个map
    • <select id="selOne" resultType="com.mybatis.pojo.Student" parameterType="map">
    •     select * from stu where id=#{id} and name=#{'name'}
    • </select>
 
 

猜你喜欢

转载自www.cnblogs.com/ruowei/p/10852077.html
今日推荐