Spring和Mybatis整合时遇到的问题
能够按照商品名称和作者同时查询
<select id="findByCondation" parameterType="cn.pojo.Product" resultType="cn.pojo.Product">
select productid,name,baseprice,writer from product where 1=1
<if test="name != null and name!=''">
and name like '%${name}%'
</if>
<if test="writer !=null and writer!=''">
and writer like'%${writer}%'
</if>
controller中的方法
@RequestMapping("/queryAll.do")
public String queryByCondation(Product product){
List<Product> list = service.findByCondation(product);
String json=JSON.toJSONString(list);
System.out.println(json);
return json;
}
> jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script src="jquery-1.8.2.min(1).js"></script>
<script>
function a1() {
$.ajax({
type:"post",
url:"queryAll.do",
data:"name="+$("#name").val()+"&writer="+$("#writer").val()+"&baseprice="+$("#baseprice").val(),
dataType:"json",
success: function (result) {
var shus=result;
$("#table").empty()
$("#table").append( "<tr>"+"<td>序号</td>"+
"<td>商品名称</td>"+
"<td>价格</td>"+
"<td>出版社</td>"+"</tr>")
for (var i in shus){
var item=shus[i]
var name=item.name
var writer=item.writer
var baseprice=item.baseprice
var id=item.productid
$("#table").append(
"<tr>"+"<td>"+id+"</td>"+
"<td>"+name+"</td>"+
"<td>"+baseprice+"</td>"+
"<td>"+writer+"</td>"+
"<td><input type='button' value='删除' οnclick='del("+id+")'></td>"+
"<td><input type='button' value='修改' οnclick='upd("+id+")'></td>"+"</tr>")
}
}
})
}
function del(id) {
$.ajax({
type:"post",
data: "id="+id,
// data: {"id":id},
url: "delete.do",
dataType: "json",
success:function (result) {
if (result.boo){
location="query.jsp"
}
}
})
}
function upd(id) {
//?问号传参后面的键值对要紧挨着
location = "update.jsp?id="+id;
}
</script>
</head>
<body>
请输入商品名称:<input type="text" id="name" value=""><br>
请输入价格:<input type="text" value="" id="baseprice"><br>
请输入作者:<input type="text" value="" id="writer"><br>
<input type="button" onclick="a1()" value="查询">
<form>
<table border="1" id="table">
</table>
</form>
</body>
运行后报错
[WARNING] Resolved [org.springframework.validation.BindException:
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object ‘product’ on field ‘baseprice’: rejected value
[]; codes
[typeMismatch.product.baseprice,typeMismatch.baseprice,typeMismatch.double,typeMismatch];
arguments
[org.springframework.context.support.DefaultMessageSourceResolvable:
codes [product.baseprice,baseprice]; arguments []; default message
[baseprice]]; default message [Failed to convert property value of
type ‘java.lang.String’ to required type ‘double’ for property
‘baseprice’; nested exception is java.lang.NumberFormatException:
empty String]]