最简单的查询在第一篇已经记录了,在这里不赘述
1、一个参数的查询
在Controller中新增 通过ID去查找具体的学生
@RequestMapping("/findById")
public String findById(Model model){
Integer id = 3;
Student student = studentMapper.findById(id);
model.addAttribute("student",student);
return "views/studentDetail";
}
Mapper层新增:
Student findById(Integer id);
XML新增:
<select id="findById" parameterType="java.lang.Integer" resultType="com.test.demo.domain.Student">
select * from student where id = #{id}
</select>
前端新建详情页:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>详情</title>
</head>
<body>
<p th:text="${student.id}"></p>
<p th:text="${student.sname}"></p>
</body>
</html>
访问:http://localhost:8080/studentManage/findById
2、多个参数的查询
controller新增: 参数为id和姓名
@RequestMapping("/listStudentByName")
public String listStudentByName(Model model){
Integer id = 2;
String sname = "stu";
List<Student> students = studentMapper.listStudentByName(id,sname);
model.addAttribute("students",students);
return "views/listStudent";
}
mapper新增:
List<Student> listStudentByName(Integer id, String sname);
XML新增:
<select id="listStudentByName" resultType="com.test.demo.domain.Student">
select * from student where id > #{id} and sname like concat('%',#{sname},'%')
</select>
访问:http://localhost:8080/studentManage/listStudentByName
注意点:
1、mapper中不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。如果使用了则 #{} 或 ${} 的方式都可以。但是使用@Param时需要注意,在XML中引用的是@Param()括号中的参数名,如果不使用也需要注意XML中应和mapper中参数名保持一致。
2、#{}是预编译处理,$ {}是字符串替换 我们知道,SQL注入是发生在编译的过程中,因为恶意注入了某些特殊字符,最后被编译成了恶意的执行操作。而预编译机制则可以很好的防止SQL注入。
3、不写parameterType 注意此时不可也不必再写参数类型了
3、复杂类型参数查询
controller新增: 需要传入多个ID作为条件查询
@RequestMapping("/findBylistId")
public String findBylistId(Model model){
List<Integer> ids = new ArrayList<>();
ids.add(1);
ids.add(4);
ids.add(5);
List<Student> students = studentMapper.findBylistId(ids);
model.addAttribute("students",students);
return "views/listStudent";
}
mapper新增: 注意此处没有使用@Param注解
List<Student> findBylistId(List<Integer> ids);
XML新增:
<select id="findBylistId" parameterType="java.util.List" resultType="com.test.demo.domain.Student">
select * from student
<if test="null != list">
where id in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</select>
访问:http://localhost:8080/studentManage/findBylistId
注意点:
1、foreach标签:foreach是对一个集合进行遍历,通常是在构建 IN 条件语句的时候。
item:表示集合中每一个元素进行迭代时的别名
index:用于表示在迭代过程中,每次迭代到的位置
collection:指定传入参数的类型
open:开始时拼接的字符串
separator:表示在每次进行迭代之间以什么符号作为分隔符
close:结束时拼接的字符串
2、collection: 标签中的这个属性很容易出错,单独拿出来说一下
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果在mapper中使用了@Param()注解,则collection属性值为@Param()括号中的值
4、多参数+复杂类型
想一下,数据一览表上的查询框可能会有多个,而它们的类型还不一样,此时我们就需要使用Map了
controller新增:特别设计了一个数组参数,可以把上一条中的也试一次
@RequestMapping("/findBymap")
public String findBymap(Model model){
Map<String,Object> map = new HashMap<>();
Integer[] ids = {1,4,5};
map.put("ids",ids);
map.put("sname","新增");
map.put("age",10);
List<Student> students = studentMapper.findBymap(map);
model.addAttribute("students",students);
return "views/listStudent";
}
mapper新增:
List<Student> findBymap(Map<String,Object> map);
XML新增:
<select id="findBymap" parameterType="java.util.Map" resultType="com.test.demo.domain.Student">
select * from student
<where>
<if test="null != sname">
and sname like concat('%',#{sname},'%')
</if>
<if test="null != age">
and age > #{age}
</if>
<if test="null != array">
and id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</where>
</select>
访问:http://localhost:8080/studentManage/findBymap
注意点:
1、<where>标签 如果上面的SQL写成这样
这么写的问题是:当没有sname参数,却有age参数的时候
sql就会变成:select * from student and age > 10 ..... 这样肯定是错的
<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。
扩展: 这里的姓名模糊查询,还可以使用<bind>标签,方便后续使用,上一篇中也提到Oracle和Mysql中模糊查询写法是不一样的,这样也可以避免更换数据库带来一些麻烦
<select id="findBymap" parameterType="java.util.Map" resultType="com.test.demo.domain.Student">
<bind name="sname" value="'%'+sname+'%'"></bind>
select * from student
<where>
<if test="null != sname">
and sname like #{sname}
</if>
<if test="null != age">
and age > #{age}
</if>
<if test="null != array">
and id in
<foreach collection="array" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</where>
</select>