vue-springboot项目 mybatis条件查询结果为null时解决方案 @Param @RequestParam 的参数传递
先附上查到的一点资料:
MyBatis真正强大之处就在于SQL映射语句,也就是它的魅力所在。
@Param
接口:
public List getUserListByParam(@Param(“userName”)String username,@Param(“userRole”)Integer roleId);
Mapper映射文件:
<select id="getUserListByParam" resultType="User" >select * from smbms_user where username like CONCAT('%',#{userName},'%') and userRole=#{userRole}</select>
语句中接收参数的方式有两种:
1、 #{}预编译 (可防止sql注入)
2、${}非预编译(直接的sql拼接,不能防止sql注入)
注意:使用@Param注解封装的参数 小括号中参数名需要与#{}中参数名一致
直接传入多个参数 使用下标获取
接口:
public List getUserListByParam(String username,Integer roleId);
Mapper映射文件:
<select id="getUserListByParam" resultType="User" >select * from smbms_user where username like CONCAT('%',#{0},'%') and userRole=#{1}</select>
#{}会将参数转换成String类型进行处理(特殊字符会进行转义) ${}不会
使用#{}会进行sql预处理 也就是表示使用PreparedStatement进行执行sql语句 可以有效防止sql注入 但是使用${}不会
<select id="getone" resultType="com.naughty.userlogin02.bean.Teacher">SELECT Id,name,object,age,rate,type FROM `teacherlist`<if test="name !=null ">WHERE name like #{name}'%'</if></select>
public List<Teacher> getone(@Param("name") String name);@GetMapping("/getone")public String search(String name){Teacher teacher = teacherDao.getone("%"+name+"%");String str = "uu";System.out.println(teacher);System.out.println("搜索老师");System.out.println(name);HashMap<String, Object> res = new HashMap<>();int numbers = teachera.size();res.put("numbers",numbers);res.put("data",teachera);String users_json = JSON.toJSONString(res);return users_json;
尝试:
public String search(String name) throws UnsupportedEncodingException {
name = new String(name.getBytes("GBK"), "UTF-8");
更加不对了。
- 其中一条报错:
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE name = ‘%李兰%’’ at line 3
async getList () {const {data: res} = await this.$http.get("getone", {params:{ name: this.name}});// res = res.datathis.teacherlist = res.dataconsole.log(res.data)this.total = res.numbers;this.queryInfo.pageNum=1
前后尝试了很多方法,包括加与不加参数,xml语句中是用like还是“=”,前后端传参的方法,然后因为查的是中文还怀疑是不是编码问题,上最后成功的截图:
以为很简单的事,不知道为什么那么多办法都不行,先上代码,待会查漏补缺,
<el-input placeholder="请输入搜索内容" v-model="name" clearable @clear="getteacherList" ><el-button slot="append" icon="el-icon-search" @click="getList"></el-button></el-input>name:'',teacherlist: [],// 用户列表
async getList () {// var data = JSON.stringify('xxx':'yyy','aaa':'bbb');const {data: res} = await this.$http.get("getone", {params: {name:this.name}});// res = res.datathis.teacherlist = res.dataconsole.log(res.data)this.total = res.numbers;this.queryInfo.pageNum=1},async getteacherList(){//这个是得到所有数据console.log("我是getteacherList");// 调用get请求const { data: res } = await this.$http.get("allteacher", {params: this.queryInfo});this.teacherlist = res.data; // 将返回数据赋值this.total = res.numbers; // 总个数},
后端:
@GetMapping("/getone")public String search(@RequestParam(value = "name", required = true) String name) {// name = new String(name.getBytes("GBK"), "UTF-8");List<Teacher> teachera = teacherDao.getone(name);String str = "uu";System.out.println(teachera);System.out.println("搜索老师");System.out.println(name);HashMap<String, Object> res = new HashMap<>();int numbers = teachera.size();res.put("numbers",numbers);res.put("data",teachera);String users_json = JSON.toJSONString(res);return users_json;}
int numbers = teachera.size();
res.put(“numbers”,numbers);
这句话获取总数。
<select id="getone" resultType="com.naughty.userlogin02.bean.Teacher" parameterType="String">select * from teacherlist where name like #{name}</select>
@Repositorypublic interface TeacherDao {public int getteacherByMassage(@Param("name") String name, @Param("object") String object,@Param("age") int age,@Param("rate") Double rate,@Param("type")String type);public List<Teacher> getallteacher(@Param("name") String name, @Param("pageStart") int pageStart, @Param("pageSize") int pageSize);public int getTeacherCounts(@Param("name") String name);public int addteacher(Teacher teacher);public List<Teacher> getone(@Param(value = "name") String name);
}
public class Teacher {int id;private String name;private int age;private String object;private Double rate;private String type;public Teacher(){
select * from teacherlist where name like '%'#{name}'%'
无法实现模糊查询。
这样可以模糊查询:
<select id="getone" resultType="com.naughty.userlogin02.bean.Teacher" parameterType="String">select * from teacherlist where name like concat('%', #{name}, '%')</select>
对比其他的选择语句:
<select id="getTeacherCounts" resultType="java.lang.Integer">SELECT count(*) FROM `teacherlist`<if test="name !=null ">WHERE name like #{name}</if></select><select id="getallteacher" resultType="com.naughty.userlogin02.bean.Teacher">SELECT * FROM teacherlist
<!-- <if test="name !=null ">-->
<!-- -->
<!-- </if>-->WHERE name like #{name}LIMIT #{pageStart},#{pageSize}</select>
@Param
@Insert(“insert into sys_role_permission(permissionid,roleid) values (#{permissionId},#{roleId})”)
int addRolePermission(@Param(“permissionId”) Integer pId, @Param(“roleId”) Integer roleId);
@Param:
当 @Insert 后面的条件有多个的时候 values (#{permissionId},#{roleId}) ,并且方法中的 int addRolePermission(@Param(“permissionId”) Integer pId, @Param(“roleId”) Integer roleId); 参数名pId,roleId和 sql中的条件参数#{permissionId} ,#{roleId} 的名称不一致的时候,就可以使用 @Param 来指定 sql后面的参数名
使用@Param后,接口中的参数顺序也可以打乱,只要id唯一即可
当只有一个参数的时候可以不使用此注解
@RequestParam 支持下面四种参数:
defaultValue 如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值
name 绑定本次参数的名称,要跟URL上面的一样
required 这个参数是不是必须的
value 跟name一样的作用,是name属性的一个别
public Map<String,Object> addRoleUser(@RequestParam("ids") List<Integer> ids,@RequestParam("userId") Integer userId)try {result = userService.addUserRole(ids, userId);} catch (Exception e) {resultJson.put("success", false);resultJson.put("message", "授权失败");e.printStackTrace();}
@RequestParam:
前端提交的form表单数据的name属性 和方法中的参数名不一致时 ,springMVC就无法自动封装参数,所以需要@RequestParam(前端name属性名称)来指定前端提交的表单的name属性的名称
当前端的name属性和方法的参数名一致的时候,可以不使用此注解
主要作用在Controller层
————————————————
对于自己上面的情况,由于方法名是name,与前端一致,因此去掉@RequestParam(value = “name”, required = true)也可以:
@GetMapping("/getone")public String search(String name) {// name = new String(name.getBytes("GBK"), "UTF-8");List<Teacher> teachera = teacherDao.getone(name);String str = "uu";System.out.println(teachera);System.out.println("搜索老师");System.out.println(name);HashMap<String, Object> res = new HashMap<>();res.put("numbers",1);res.put("data",teachera);String users_json = JSON.toJSONString(res);return users_json;}
@PathVariable
这个注解能够识别URL里面的一个模板,我们看下面的一个URL
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
1
上面的一个url你可以这样写:
@RequestMapping("/hello/{id}")
public String getDetails(@PathVariable(value=“id”) String id,
@RequestParam(value=“param1”, required=true) String param1,
@RequestParam(value=“param2”, required=false) String param2){
…
}
@ResponseBody
responseBody表示服务器返回的时候以一种什么样的方式进行返回, 将内容或对象作为 HTTP 响应正文返回,值有很多,一般设定为json
@RequestBody
一般是post请求的时候才会使用这个请求,把参数丢在requestbody里面
vue-springboot项目 mybatis条件查询结果为null时解决方案 @Param @RequestParam 的参数传递相关推荐
- vue 前端项目带条件查询的分页列表开发实战
一 添加医院设置路由 修改文件 E:\vue-sdgt\src\router\index.js {path: '/hospital',component: Layout,redirect: '/hos ...
- 教你如何制作vue+springboot项目
前言 最近刚刚做了一个基于vue+springboot的学生成绩管理系统,于是基于这点,对遇到的一些问题进行一些配置的汇总.以及vue+springboot项目是怎么实现的,以下将贴出vue和spri ...
- oracle jdbctype null,Oracle数据库之springboot 项目mybatis plus 设置 jdbcTypeForNull
本文主要向大家介绍了Oracle数据库之springboot 项目mybatis plus 设置 jdbcTypeForNull,通过具体的内容向大家展现,希望对大家学习Oracle数据库有所帮助. ...
- springboot项目mybatis日志自定义设置无法生效
springboot项目mybatis日志自定义设置无法生效,就是无法设置日志级别,无法对java.sql.PreparedStatement.java.sql.Connection等进行设置. 翻了 ...
- 在腾讯云服务器跑Vue + SpringBoot项目
背景:闲来无事,跟着做了个Vue+SpringBoot项目,做了一些之后项目内容没什么思路搞什么了,然后就想着再搭个服务器. 一:项目及云服务器还有域名准备 项目:这个-自己准备吧,只要自己做好的能在 ...
- Vue+Springboot项目练手(主要是后端)
如果观看的朋友不太了解Vue的话我建议你可以学习一下Vue框架,如果你没有太多时间的话,可以参考我如下文章,不懂的可以给发信息,应该能解决问题. Vue学习笔记:我个人不太建议看这篇文章,可以自己去找 ...
- 入门vue+springboot项目
介绍 为了应付毕业设计,花了一个多月学习了vue+springboot做一个项目.现在把项目的一些架构和重点整理一下. 1.vue前端 vue主要还是配置问题,我在项目中用到的ajax和element ...
- Mybatis为什么查询结果为空时返回值为NULL或空集合?
以下内容如有错误欢迎指出,有则改之无则加勉~ 一行数据记录如何映射成一个 Java 对象,这种映射机制是 MyBatis 作为 ORM 框架的核心功能之一,也是我们这篇文章需要学习的内容 开始前我们先 ...
- SpringBoot项目中获取yml文件的属性时实体属性类出现Spring Boot Configuration Annotation Processor not found in classpath
1.SpringBoot项目的项目结构如下: 2.属性实体类 上面出现了Spring Boot Configuration Annotation Processor not found in clas ...
最新文章
- 腾讯首席战略官詹姆斯: 从互联网信息的永久性和稀缺性看腾讯的投资逻辑
- linux centos 使用 alpine 编译的二进制文件 报错 /lib/ld-musl-x86_64.so.1: bad ELF interpreter 解决方法
- 二叉树 -php实现先序、中序、后序遍历二叉树
- wpf 修改label值_WPF 获取动态添加控件的值
- 个人博客网站的设计与实现_新手建立个人博客网站后如何提高回访率?
- 动态规划训练8 [E - Multiplication Puzzle POJ1651]
- java github_GitHub Research:超过50%的Java记录语句写错了
- 表格数字乘以百分比怎么算_EXCEL记住这两个快捷键,1秒种设好数字格式
- Flowable 数据库表结构 ACT_HI_COMMENT
- redis的内存优化【转】
- android自动布局优先级,自动布局AutoLayout
- oc - runtime运行机制
- The Unique MST 判断生成树是否唯一
- 如何为新项目创建新的空分支
- creo管道设计教程_Creo7.0设计探索在管道设计的应用
- Excel使用技巧随笔
- 数据治理-数据质量管理
- javascript Date format(js日期格式化)
- iOS打包导出时出现Missing iOS Distribution signing identity问题
- 挪车电话也有商机,易扫挪车App
热门文章
- 世界三大顶级音响_世界三大汽车赛事是什么?一起来了解一下
- mysql的limit和or_面试官:谈谈MySQL的limit用法、逻辑分页和物理分页
- c 多文件全局变量_C语言开发单片机为啥都是全局变量形式?
- split函数python_Python字符串split函数知多少【Python每日一个知识点第75期】
- 单片机c语言 openssl,Linux下C语言使用openssl库进行加密
- 安卓隐藏摄像_侧置摄像与隐藏前摄,拒绝单调乏味,这两款国产5G手机独具匠心...
- 软工专硕考研_分析|华北电力(北京)大学20计算机考研报录分析!电子信息复试狂刷114人,软工专硕复试录取高达1:4.7!...
- 六十七、完成Vue项目首页图标区域布局和逻辑实现
- 极客产品经理学习笔记
- NAACL 2021 | AWS AI 提出基于对比学习的端到端无监督聚类方法