问题描述
刚接触ssh2,自己写了个注册登录模块,注册很正常,插入数据也没有问题,但是在登录验证时,无法用hql查询,后面的打印语句也不能执行,不知道是不是我的hql有问题,顺便问下,spring整合hibernate之后hql的写法是不是与单纯hibernate环境下写法不一样?下面是我的写的dao源码,请大神帮忙看下是什么问题,搞了一下午了没头绪packageshopping.daoImpl;importjava.util.ArrayList;importjava.util.List;importorg.springframework.orm.hibernate3.support.HibernateDaoSupport;importshopping.dao.UserDAO;importshopping.entity.User;publicclassUserDAOHibernateextendsHibernateDaoSupportimplementsUserDAO{publicvoiddeleteUser(intid){//TODOAuto-generatedmethodstubgetHibernateTemplate().delete(getById(id));}publicUsergetById(intid){//TODOAuto-generatedmethodstubreturngetHibernateTemplate().get(User.class,id);}publicvoidinsertUser(Useruser){//TODOAuto-generatedmethodstubgetHibernateTemplate().save(user);}publicvoidupdateUser(Useruser){//TODOAuto-generatedmethodstubgetHibernateTemplate().update(user);}publicintvalidate(Stringusername,Stringpassword){//TODOAuto-generatedmethodstubList<User>userList=newArrayList();Stringhql="fromUser";userList=getHibernateTemplate().find(hql);System.out.println(userList.size());/*Sessionsession=(Session)getSession();Queryquery=session.createQuery(hql);userList=query.list();*///sql查询可以用了/*Stringsql="select*fromuuserwhereusername='"+username+"'andpassword='"+password+"'";Sessionsession=(Session)getSession();Queryquery=session.createSQLQuery(sql);userList=query.list();*/if(userList.size()>0){return1;}else{return0;}}}
解决方案
解决方案二:
写法是一样的,程序执行之后是报错还是其他问题,可以使用单步调试看一下哪里有问题。
解决方案三:
没有任何反映,下面的输出语句没有执行,也单步调试过了,HibernateTemplate是有对象的,执行find方法所在行的时候出现这个东西
解决方案四:
User对象的属性跟hibernate中的配置文件看看。是不是别名,字段名称错了,不对应什么的
解决方案五:
packageshopping.entity;publicclassUser{privateintid;privateStringmemNum;//会员号(注册年月日+六位数字),暂时用newDate().getTime();代替privateStringusername;//用户名,用于登录privateStringrealname;privateStringsex;//性别privateStringidCard;//身份证号privateStringpassword;//密码,用户登录privateStringphone;//联系电话privatelongpoint;//积分,可抵现privateStringaddress;//寄货地址privateStringbankCard;//银行卡号privateintisenable;//是否可用,标注用户注销,0为可用,1为不可用publicUser(){}publicintgetId(){returnid;}publicvoidsetId(intid){this.id=id;}publicStringgetMemNum(){returnmemNum;}publicvoidsetMemNum(StringmemNum){this.memNum=memNum;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetRealname(){returnrealname;}publicvoidsetRealname(Stringrealname){this.realname=realname;}publicStringgetSex(){returnsex;}publicvoidsetSex(Stringsex){this.sex=sex;}publicStringgetIdCard(){returnidCard;}publicvoidsetIdCard(StringidCard){this.idCard=idCard;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicStringgetPhone(){returnphone;}publicvoidsetPhone(Stringphone){this.phone=phone;}publiclonggetPoint(){returnpoint;}publicvoidsetPoint(longpoint){this.point=point;}publicStringgetAddress(){returnaddress;}publicvoidsetAddress(Stringaddress){this.address=address;}publicStringgetBankCard(){returnbankCard;}publicvoidsetBankCard(StringbankCard){this.bankCard=bankCard;}publicintgetIsenable(){returnisenable;}publicvoidsetIsenable(intisenable){this.isenable=isenable;}}
解决方案六:
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="user"class="shopping.entity.User"scope="singleton"/><beanid="userAction"class="shopping.action.UserAction"><propertyname="userService"ref="userService"/><propertyname="user"ref="user"></property></bean><beanid="userService"class="shopping.serviceImpl.UserServiceImpl"><propertyname="userDao"><refbean="userDao"/><!--指向其他配置文件下的bean--></property></bean></beans>
解决方案七:
找到错误的原因了,是因为struts反射获取action中的方法时抛出了一个InvocationTargetException异常,就是下面这个login方法,但是不知道该怎么解决packageshopping.action;importshopping.entity.User;importshopping.service.UserService;importshopping.util.UserUtil;importcom.opensymphony.xwork2.ActionSupport;publicclassUserActionextendsActionSupport{privateUserServiceus;privateUseruser;privateStringtip;//用于封装处理结果,可在下个页面获取publicStringlogin(){intreval=us.validate(user.getUsername(),user.getPassword());if(reval==1){return"success";}else{return"fail";}}publicStringregist(){user.setMemNum(UserUtil.autoProduceMemNum());user.setPoint(0);user.setIsenable(0);us.addUser(user);return"input";}publicStringgetTip(){returntip;}publicvoidsetTip(Stringtip){this.tip=tip;}publicvoidsetUs(UserServiceus){this.us=us;}publicUserServicegetUs(){returnus;}publicvoidsetUserService(UserServiceus){this.us=us;}publicUsergetUser(){returnuser;}publicvoidsetUser(Useruser){this.user=user;}}
解决方案八:
http://blog.csdn.net/luoduyu/article/details/1443895来这里看看,也许对你有帮助
解决方案九:
不是应改SELECT*FROMUser的吗