vue-springboot项目 mybatis条件查询结果为null时解决方案 @Param @RequestParam 的参数传递
先附上查到的一點(diǎn)資料:
MyBatis真正強(qiáng)大之處就在于SQL映射語(yǔ)句,也就是它的魅力所在。
@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>語(yǔ)句中接收參數(shù)的方式有兩種:
1、 #{}預(yù)編譯 (可防止sql注入)
2、${}非預(yù)編譯(直接的sql拼接,不能防止sql注入)
注意:使用@Param注解封裝的參數(shù) 小括號(hào)中參數(shù)名需要與#{}中參數(shù)名一致
直接傳入多個(gè)參數(shù) 使用下標(biāo)獲取
接口:
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>#{}會(huì)將參數(shù)轉(zhuǎn)換成String類(lèi)型進(jìn)行處理(特殊字符會(huì)進(jìn)行轉(zhuǎn)義) ${}不會(huì)
使用#{}會(huì)進(jìn)行sql預(yù)處理 也就是表示使用PreparedStatement進(jìn)行執(zhí)行sql語(yǔ)句 可以有效防止sql注入 但是使用${}不會(huì)
<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");
更加不對(duì)了。
前后嘗試了很多方法,包括加與不加參數(shù),xml語(yǔ)句中是用like還是“=”,前后端傳參的方法,然后因?yàn)椴榈氖侵形倪€懷疑是不是編碼問(wèn)題,上最后成功的截圖:
以為很簡(jiǎn)單的事,不知道為什么那么多辦法都不行,先上代碼,待會(huì)查漏補(bǔ)缺,
<el-input placeholder="請(qǐng)輸入搜索內(nèi)容" v-model="name" clearable @clear="getteacherList" ><el-button slot="append" icon="el-icon-search" @click="getList"></el-button></el-input>name:'',teacherlist: [],// 用戶(hù)列表 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(){//這個(gè)是得到所有數(shù)據(jù)console.log("我是getteacherList");// 調(diào)用get請(qǐng)求const { data: res } = await this.$http.get("allteacher", {params: this.queryInfo});this.teacherlist = res.data; // 將返回?cái)?shù)據(jù)賦值this.total = res.numbers; // 總個(gè)數(shù)},后端:
@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);
這句話(huà)獲取總數(shù)。
這樣可以模糊查詢(xún):
<select id="getone" resultType="com.naughty.userlogin02.bean.Teacher" parameterType="String">select * from teacherlist where name like concat('%', #{name}, '%')</select>對(duì)比其他的選擇語(yǔ)句:
<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:
當(dāng) @Insert 后面的條件有多個(gè)的時(shí)候 values (#{permissionId},#{roleId}) ,并且方法中的 int addRolePermission(@Param(“permissionId”) Integer pId, @Param(“roleId”) Integer roleId); 參數(shù)名pId,roleId和 sql中的條件參數(shù)#{permissionId} ,#{roleId} 的名稱(chēng)不一致的時(shí)候,就可以使用 @Param 來(lái)指定 sql后面的參數(shù)名
使用@Param后,接口中的參數(shù)順序也可以打亂,只要id唯一即可
當(dāng)只有一個(gè)參數(shù)的時(shí)候可以不使用此注解
@RequestParam 支持下面四種參數(shù):
defaultValue 如果本次請(qǐng)求沒(méi)有攜帶這個(gè)參數(shù),或者參數(shù)為空,那么就會(huì)啟用默認(rèn)值
name 綁定本次參數(shù)的名稱(chēng),要跟URL上面的一樣
required 這個(gè)參數(shù)是不是必須的
value 跟name一樣的作用,是name屬性的一個(gè)別
@RequestParam:
前端提交的form表單數(shù)據(jù)的name屬性 和方法中的參數(shù)名不一致時(shí) ,springMVC就無(wú)法自動(dòng)封裝參數(shù),所以需要@RequestParam(前端name屬性名稱(chēng))來(lái)指定前端提交的表單的name屬性的名稱(chēng)
當(dāng)前端的name屬性和方法的參數(shù)名一致的時(shí)候,可以不使用此注解
主要作用在Controller層
————————————————
對(duì)于自己上面的情況,由于方法名是name,與前端一致,因此去掉@RequestParam(value = “name”, required = true)也可以:
@PathVariable
這個(gè)注解能夠識(shí)別URL里面的一個(gè)模板,我們看下面的一個(gè)URL
http://localhost:8080/springmvc/hello/101?param1=10¶m2=20
1
上面的一個(gè)url你可以這樣寫(xiě):
@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表示服務(wù)器返回的時(shí)候以一種什么樣的方式進(jìn)行返回, 將內(nèi)容或?qū)ο笞鳛?HTTP 響應(yīng)正文返回,值有很多,一般設(shè)定為json
@RequestBody
一般是post請(qǐng)求的時(shí)候才會(huì)使用這個(gè)請(qǐng)求,把參數(shù)丟在requestbody里面
總結(jié)
以上是生活随笔為你收集整理的vue-springboot项目 mybatis条件查询结果为null时解决方案 @Param @RequestParam 的参数传递的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【练习】c++用链栈实现计算器
- 下一篇: 在服务器上打包部署springboot+