20190502(已解决)关于$.ajax在软件的使用过程中,数据库查询注意情况

博客记录了在js中使用$.ajax与后端数据库传值时遇到的错误,即org.hibernate.QueryException,作者通过尝试解决了查询SQL语句的编写问题。同时介绍了$.ajax的使用方法,包含url、data、type、success、contentType五个基本要素,并给出前端代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

说明

本来都打算写工作周报去了,打开后看的这个难道我的问题,突然有了想法。
面临问题:

错误一:

在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();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值