说明
本来都打算写工作周报去了,打开后看的这个难道我的问题,突然有了想法。
面临问题:
错误一:
在js中使用ajax,在与后端的数据库传值中报
org.hibernate.QueryException: could not resolve property: busId of: com.ninebot.ad.schdBus.busInspect.entity.Inspect [SELECT COUNT(*) FROM com.ninebot.ad.schdBus.busInspect.entity.Inspect i WHERE i.busId = ? AND i.idate = ?],我实在不懂这个查询的SQL语句该如何写,通过尝试成功。
学习内容
$.ajax 的使用方法
主要包含以下五个基本要素:
url 触发后执行的路径
data 需要从前端传给后端的数据;
type 传输的方式
success 后台运行成功后对于返回的结果做何种操作。
contentType 语言格式
以下是 $.ajax({});的使用代码:
$.ajax({
url:"${basePath}schdBus/busInspect_compareDate.action",
data:{"idate":idate,"busId":busId},
type:"post",
success: function(msg){
//2、更新状态栏、操作栏的显示值
if("OK" == msg){
i.innerHTML="OK";
i.style.color="green";
} else if("NO1" == msg){
i.innerHTML="该日期已输入";
i.style.color="red";
} else if("NO2" == msg){
i.innerHTML="日期为空,请输入";
i.style.color="red";
} else if("NO3" == msg){
i.innerHTML="班车为空,请输入";
i.style.color="red";
} else{
i.innerHTML="系统错误";
i.style.color="red";
}
},
error: function(){
alert("查询失败");
}
});
以下是前端代码:
定义了与前端对应的变量 idate 、busId
action
public class InspectAction extends BaseAction {
@Resource
private InspectService inspectService;
@Resource
private BusService busService;
private Inspect inspect;
private List<Inspect> inspectList;
private List<Bus> busList;
private String idate;
private String busId;
public void compareDate() {
long x;//接收数据库查询值用
String y = "NO3";//给前端返回信息,前端$.ajax的msg接收
QueryHelper queryHelper =new QueryHelper(Inspect.class, "i");//查询助手,添加查询的类,和别称
try {
if(!Util.isEmptyString(busId)){//判断是否为空
if(!Util.isEmptyString(idate)){//判断是否为空
queryHelper.addCondition("i.bus.busId = ?", busId)//这个地方就是我错误的地方,这里要写成i.bus.busId,内容以entity为基础,而不是数据库列名
.addCondition("i.idate = ?", new SimpleDateFormat("yyyy-MM-dd").parse(idate));//这个地方
x = inspectService.count(queryHelper); //给x传值
if(x == 0){
y="OK";
}else{
y="NO1";
}
}else{
y="NO2";
}
}
//输出更新结果
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(y.getBytes("utf-8"));
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}