本文共 6861 字,大约阅读时间需要 22 分钟。
先附上查到的一点资料:
MyBatis真正强大之处就在于SQL映射语句,也就是它的魅力所在。接口:
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 Listgetone(@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");更加不对了。
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"); Listteachera = 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 Listgetallteacher(@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}'%'无法实现模糊查询。
对比其他的选择语句:
@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 MapaddRoleUser(@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"); Listteachera = 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里面的一个模板,我们看下面的一个URLhttp://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里面转载地址:http://mvten.baihongyu.com/