<!-- 查询列表 --><!-- 系统不知道返回封装为什么类型,所以要注明返回类型 --><selectid="selectAllStudents"resultType="Student">
select id,name,age,score from student
<!-- 如果数据库为tid,tname,tage,那么我们可以使用别名
select tid id,tname name,tage age,tscore score from student --></select>
3.模糊查询
我们需要查询名字的时候一般是模糊查询。那么使用下面的sql即可:
<!-- 模糊查询--><!-- 不能写成'%#{name}%' --><!-- 可以写成这样,也就是使函数拼接 select id,name,age,score from student where name like concat('%',#{xxx},'%') --><!-- 也可以写成这样,‘’引起来的是写死的,而变量是不可以引起来的select id,name,age,score from student where name like '%' #{xxx} '%' --><!-- '%' #{xxx} '%'中间必须有空格,要不就无效了 --><selectid="selectStudentsByName"resultType="Student"><!--最常用的(动态参数) select id,name,age,score from student where name like '%' #{name} '%' --><!-- 下面的是字符串拼接 ,只能写value,了解即可,容易sql注入,执行效率低,不建议使用-->
select id,name,age,score from student where name like '%${value}%'
</select>
<select id="selectStudentById" resultType="Student">
select * from student where id=${value}
</select>
模糊查询的时候,一下方式是拼接的模式,容易被sql注入,所以我们一般不推荐:
<selectid="selectStudentsByName"resultType="Student"><!-- 下面的是字符串拼接 ,只能写value,了解即可,容易sql注入,执行效率低,不建议使用-->
select id,name,age,score from student where name like '%${value}%'
</select>
注意不可以写成’%#{name}%’,拼接的必须使用 $ 符号。可以使用函数进行连接:
<select id="selectStudentsByName" resultType="Student">
select id,name,age,score from student where name like concat('%',#{xxx},'%')
</select>