spring boot freemarker首页查询中怎么进行模糊查询?
时间: 2023-07-31 13:10:16 浏览: 61
在Spring Boot中使用Freemarker进行模糊查询的步骤如下:
1.在Controller中定义一个方法,用于接收前端页面传递的查询关键字参数。
2.在Service层实现一个方法,用于根据关键字进行模糊查询。
3.在Controller中调用Service层的方法,将查询结果存储到ModelAndView对象中。
4.在Freemarker页面中通过表单提交关键字参数,然后使用Freemarker的#list指令遍历查询结果进行展示。
下面是一个简单的示例代码:
Controller层代码:
```
@RequestMapping("/search")
public ModelAndView search(@RequestParam("keyword") String keyword) {
List<User> userList = userService.search(keyword);
ModelAndView mav = new ModelAndView("userList");
mav.addObject("userList", userList);
return mav;
}
```
Service层代码:
```
public List<User> search(String keyword) {
List<User> userList = userDao.search(keyword);
return userList;
}
```
Freemarker页面代码:
```
<form action="/search" method="get">
<input type="text" name="keyword" value=""/>
<button type="submit">搜索</button>
</form>
<table>
<thead>
<tr>
<th>用户名</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<#list userList as user>
<tr>
<td>${user.username}</td>
<td>${user.email}</td>
</tr>
</#list>
</tbody>
</table>
```
在上面的示例代码中,关键字参数通过@RequestParam注解进行接收,Service层的search方法通过模糊查询数据库得到查询结果,最终在Freemarker页面中展示查询结果列表。