SpringBoot框架学习记录第二篇,自定义查询和动态查询

更新时间:2023-05-22 09:19:36 阅读: 评论:0

SpringBoot框架学习记录第⼆篇,⾃定义查询和动态查询⼀. ⾃定义查询@Query
1.1. ⾃定义模糊查询
模糊查询:121
代码实现:
边侧
/**
* public class StudentController 中的代码
* Jpa中⾃定义查询Query学习
* 1.Spring Boot模糊查询第⼀种⽅法,返回JSON数据
* @return
*/
@ResponBody
@GetMapping("/queryName")
提高工作执行力public List<Student> queryByName(){
return studentRepository.findByName("121");
}
/**
kissme是什么意思* public interface StudentRepository 中的代码
* 模糊查询数据
* 常⽤错误,(XXX) is not mapped 问题
* 解决⽅法,下⾯的Student 不是数据库表名字,⽽是实体名
* @param urname
* @return
*/
@Query(" lect b from Student b where b.urname like %?1% ")
public List<Student> findByName(String urname);
1.2. ⾃定义随机本地查询⼏条数据
随机查询3条数据:
代码实现:
* Jpa中⾃定义查询Query学习
* 2.Spring Boot随机查询⼏条数据,返回JSON数据
善意的谎言故事* @return
*/
@ResponBody
@GetMapping("/randomList")
public List<Student> randomByName(){
return studentRepository.randomList(3);
}
/**
* public interface StudentRepository 中的代码
* 本地随机查询⼏条数据,nativeQuery默认是fal,这⾥进⾏开启
* @param n
* @return
*/
@Query(value="lect * from t_student order by RAND() limit ?1",nativeQuery=true)
public List<Student> randomList(Integer n);virusbarrier
⼆. 动态查询Specification使⽤
封装Specification查询条件,在Spring Data JPA 2.0以前使⽤ Specifications 这个辅助类来操作where、not、and和or连接,在2.0版本以后这个类会被剔除,可以直接使⽤ Specification ⾃⾝对象来操作where多条件连接
2.1. 效果预览
abhor2.2. 代码实现
JAVA代码:
* Specification动态查询,
* ⽐如⽤户需要两个条件进⾏查询,这时就需要动态判断,就是拼接SQL
* @param student
* @return
*/
@RequestMapping("/list2")
public ModelAndView ListTwo(Student student){
ModelAndView mav = new ModelAndView();
/**
* 使⽤匿名内部类,通过构造使⽤条件拼接
* 后期要写在Service中,以及排序逻辑有利于代码开发
* Root:查询哪个表
* CriteriaQuery:查询哪些字段,排序是什么
* CriteriaBuilder:字段之间是什么关系,如何⽣成⼀个查询条件,每⼀个查询条件都是什么⽅式
*/
List<Student> studentList = studentRepository.findAll(new Specification<Student>() {
@Override
public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cd) {
//动态拼接
Predicate predicate = cd.conjunction();
if(student != null){
//判断第⼀个条件
Urname() != null && !"".Urname())){
//获取表达式
耶鲁大学Expressions().add(cd.("urname"), "%"+Urname()+"%"));    }
北京出国留学中介
//判断第⼆个条件
Sex() != null && !"".Sex())){
//获取表达式
}
}eng是什么意思
return predicate;
}
});
//返回页⾯
mav.addObject("studentList",studentList);
mav.addObject("title","动态查询");
mav.tViewName("studentList2");
return mav;
}
HTML代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta chart="UTF-8"></meta>
<title th:text="${title}+'-拾光博客'"></title>
</head>
<body>
<p><a href="/student/addview">添加学⽣</a></p>
<form method="post" action="/student/list2">
学⽣姓名:<input type="text" name="urname"/>    ;学⽣性别:<input type="text" name="x"/>    
胡萝卜用英语怎么说<input type="submit" value="搜索"/>
</form>
<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>操作</th>
</tr>
<tr th:each="student:${studentList}">
<td th:text="${student.id}"></td>
<td th:text="${student.urname}"></td>
<td th:text="${student.x}"></td>
<td th:text="${student.age}"></td>
<td>
<!-- <a href="/student/studel/?id=" th:value="${student.id}">删除</a> -->
<a th:href="@{'/student/stuedit/'+${student.id}}">修改</a>
<a th:href="@{'/student/studel/'+${student.id}}">删除</a>
</td>
</tr>
</table>
</body>
</html>

本文发布于:2023-05-22 09:19:36,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/118157.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:查询   条件   数据   代码   操作   名字
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图