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");
更加不對了。
前后嘗試了很多方法,包括加與不加參數,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 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屬性的一個別
@RequestParam:
前端提交的form表單數據的name屬性 和方法中的參數名不一致時 ,springMVC就無法自動封裝參數,所以需要@RequestParam(前端name屬性名稱)來指定前端提交的表單的name屬性的名稱
當前端的name屬性和方法的參數名一致的時候,可以不使用此注解
主要作用在Controller層
————————————————
對于自己上面的情況,由于方法名是name,與前端一致,因此去掉@RequestParam(value = “name”, required = true)也可以:
@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 的参数传递的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【练习】c++用链栈实现计算器
- 下一篇: 【问题记录】解决npm 报错This d