博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue-springboot项目 mybatis条件查询结果为null时解决方案 @Param @RequestParam 的参数传递
阅读量:3903 次
发布时间:2019-05-23

本文共 6861 字,大约阅读时间需要 22 分钟。

先附上查到的一点资料:

MyBatis真正强大之处就在于SQL映射语句,也就是它的魅力所在。

@Param

接口:

public List getUserListByParam(@Param(“userName”)String username,@Param(“userRole”)Integer roleId);

Mapper映射文件:

语句中接收参数的方式有两种:

1、 #{}预编译 (可防止sql注入)
2、${}非预编译(直接的sql拼接,不能防止sql注入)

注意:使用@Param注解封装的参数 小括号中参数名需要与#{}中参数名一致

直接传入多个参数 使用下标获取

接口:

public List getUserListByParam(String username,Integer roleId);

Mapper映射文件:

#{}会将参数转换成String类型进行处理(特殊字符会进行转义) ${}不会

使用#{}会进行sql预处理 也就是表示使用PreparedStatement进行执行sql语句 可以有效防止sql注入 但是使用${}不会

在这里插入图片描述

public List
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
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.data this.teacherlist = res.data console.log(res.data) this.total = res.numbers; this.queryInfo.pageNum=1

前后尝试了很多方法,包括加与不加参数,xml语句中是用like还是“=”,前后端传参的方法,然后因为查的是中文还怀疑是不是编码问题,上最后成功的截图:

在这里插入图片描述
在这里插入图片描述

以为很简单的事,不知道为什么那么多办法都不行,先上代码,待会查漏补缺,

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.data this.teacherlist = res.data console.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
teachera = teacherDao.getone(name); String str = "uu"; System.out.println(teachera); System.out.println("搜索老师"); System.out.println(name); HashMap
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);
这句话获取总数。
在这里插入图片描述

@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
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
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}'%'无法实现模糊查询。

这样可以模糊查询:

对比其他的选择语句:

        

@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
addRoleUser(@RequestParam("ids") List
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
teachera = teacherDao.getone(name); String str = "uu"; System.out.println(teachera); System.out.println("搜索老师"); System.out.println(name); HashMap
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&param2=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里面

转载地址:http://mvten.baihongyu.com/

你可能感兴趣的文章
摄像头
查看>>
我的理想,我的奋斗目标
查看>>
Nginx基于多域名、多端口、多IP配置虚拟主机
查看>>
一次Linux 系统受攻击的解决过程
查看>>
最新最全Apache源码编译安装
查看>>
最新mysql数据库源码编译安装。
查看>>
第一章 vue入门
查看>>
Linux文件引用计数的逻辑
查看>>
linux PCIe hotplug arch analysis
查看>>
LDD3 study note 0
查看>>
cpio compress and extract
查看>>
PCI SMMU parse in ACPI
查看>>
const使用实例
查看>>
面向对象设计案例——点和圆关系
查看>>
深拷贝与浅拷贝
查看>>
WinForm 打开txt文件
查看>>
WinForm 实现日志记录功能
查看>>
WinForm 读取照片文件
查看>>
WinForm ComboBox不可编辑与不可选择
查看>>
WinForm textbox控件设置为不可编辑
查看>>