一
输入参数为简单类型(8个基本类型+String)
#{}中可以是任意值都可以
${}中必须是value
比如根据id查一个人
<?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">
<!-- namespace:该mapper.xml映射文件的唯一标识符 -->
<mapper namespace="org.all.Mapper">
<!-- 通过id来区分是哪个 resultType放回的对象-->
<!-- 动态传值#{id} -->
<select id="selectBlog" resultType="person" parameterType="int">
select * from person where id = #{aa}
</select>
</mapper>
<?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">
<!-- namespace:该mapper.xml映射文件的唯一标识符 -->
<mapper namespace="org.all.Mapper">
<!-- 通过id来区分是哪个 resultType放回的对象-->
<!-- 动态传值#{id} -->
<select id="selectBlog" resultType="person" parameterType="int">
select * from person where id = ${value}
</select>
</mapper>
二
输入参数为对象类型resultType="person"
#{}中是对象属性名
${}中是对象属性名
Person person = new Person();
person.setName("王五");
person.setId(1);
person.setAge("2");
List<Person> persons = mapper.selectByidorname(person);
System.out.println(persons);
<?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">
<!-- namespace:该mapper.xml映射文件的唯一标识符 -->
<mapper namespace="org.all.Mapper">
<select id="selectByidorname" parameterType="person" resultType="person">
select * from person where id = #{id} or age = #{age}
</select>
</mapper>
三
#{}自动给String类型加上双引号
${}原样输出
当输入类型为String的时候,需要手动添加双引号
'${}'
如果根据名字查询一个人
<select id="selectBlog" resultType="person" parameterType="String">
select * from person where id = ${value}
</select>
直接这样写就会报错,因为${}原样输出张三并没有添加但引号