package com.hkbfl.service.impl;import java.io.Serializable;
import java.util.List;import com.hkbfl.domain.Users;
import com.hkbfl.service.inter.UserServiceInter;
import com.hkbfl.util.HibernateUtil;public class UserServiceImpl extends BaseServiceImpl implements UserServiceInter { /**
* 验证用户是否合法
* @author
* @funtion 完成用户验证
* @参数说明
* @return 如果验证合法,返回完整的Users对象。验证失败,返回null
*/
public Users checkUsers(Users users){
String hql = "from Users where username = ? and password = ?";
String[] parameters = {users.getUsername(), users.getPassword()};
List<Users> list = HibernateUtil.executeQuery(hql, parameters);
if(list.size() == 0){
return null;
}else{
return list.get(0);
}
} public Object findById(Class clazz, Serializable id) {
// TODO Auto-generated method stub
return null;
}
}
import java.util.List;import com.hkbfl.domain.Users;
import com.hkbfl.service.inter.UserServiceInter;
import com.hkbfl.util.HibernateUtil;public class UserServiceImpl extends BaseServiceImpl implements UserServiceInter { /**
* 验证用户是否合法
* @author
* @funtion 完成用户验证
* @参数说明
* @return 如果验证合法,返回完整的Users对象。验证失败,返回null
*/
public Users checkUsers(Users users){
String hql = "from Users where username = ? and password = ?";
String[] parameters = {users.getUsername(), users.getPassword()};
List<Users> list = HibernateUtil.executeQuery(hql, parameters);
if(list.size() == 0){
return null;
}else{
return list.get(0);
}
} public Object findById(Class clazz, Serializable id) {
// TODO Auto-generated method stub
return null;
}
}
解决方案 »
- javax.servlet.ServletException: Cannot expose request attribute
- ajax长轮询如何实现的。
- Struts2登录出现404错误,帮个忙
- 请教(Exception in thread "main" org.hibernate.MappingException: Unknown entity)
- 谁能给我解释一下 编程式事务与声明式事务的区别啊?马上给分
- Hibernate 使用动态模型(Map)时报 org.hibernate.LazyInitializationException错误
- 急!求hibernate HQL语句一条。
- ejb bean 里提示Method xxx should not throw 'java.rmi.RemoteException'
- 求教一个hibernate问题
- java是从main函数开始执行的么
- 这段代码看不懂,帮忙看看咯
- jacob设置垂直对齐方式
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration> <session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=30cg
</property>
<property name="connection.username">sa</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.show_sql">true</property>
<property name="myeclipse.connection.profile">30cgDB</property>
<mapping resource="com/hkbfl/domain/UserType.hbm.xml" />
<mapping resource="com/hkbfl/domain/Ordertype.hbm.xml" />
<mapping resource="com/hkbfl/domain/PlanType.hbm.xml" />
<mapping resource="com/hkbfl/domain/ClassType.hbm.xml" />
<mapping resource="com/hkbfl/domain/Apply.hbm.xml" />
<mapping resource="com/hkbfl/domain/Authority.hbm.xml" />
<mapping resource="com/hkbfl/domain/Challenge.hbm.xml" />
<mapping resource="com/hkbfl/domain/Curriculum.hbm.xml" />
<mapping resource="com/hkbfl/domain/Declaration.hbm.xml" />
<mapping resource="com/hkbfl/domain/Friend.hbm.xml" />
<mapping resource="com/hkbfl/domain/Heart.hbm.xml" />
<mapping resource="com/hkbfl/domain/Images.hbm.xml" />
<mapping resource="com/hkbfl/domain/Inspiration.hbm.xml" />
<mapping resource="com/hkbfl/domain/Payment.hbm.xml" />
<mapping resource="com/hkbfl/domain/Question.hbm.xml" />
<mapping resource="com/hkbfl/domain/QuestionService.hbm.xml" />
<mapping resource="com/hkbfl/domain/Status.hbm.xml" />
<mapping resource="com/hkbfl/domain/Stories.hbm.xml" />
<mapping resource="com/hkbfl/domain/StoriesRevert.hbm.xml" />
<mapping resource="com/hkbfl/domain/StudyPlan.hbm.xml" />
<mapping resource="com/hkbfl/domain/SuccessStories.hbm.xml" />
<mapping
resource="com/hkbfl/domain/SuccessStoriesRevert.hbm.xml" />
<mapping resource="com/hkbfl/domain/Sysdiagrams.hbm.xml" />
<mapping resource="com/hkbfl/domain/Target.hbm.xml" />
<mapping resource="com/hkbfl/domain/TargetImg.hbm.xml" />
<mapping resource="com/hkbfl/domain/TargetRevert.hbm.xml" />
<mapping resource="com/hkbfl/domain/UserMessage.hbm.xml" />
<mapping resource="com/hkbfl/domain/Users.hbm.xml" /> </session-factory></hibernate-configuration>
public static List executeQuery(String hql,String [] parameters){
Session s=null;
List list=null;
try {
s=openSession();
Query query=s.createQuery(hql);
System.out.println(hql);
//先判断是否有参数要绑定
if(parameters != null && parameters.length >0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
list=query.list();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
throw new RuntimeException(e.getMessage());
}finally{
if(s!=null&&s.isOpen()){
s.close();
}
}
return list;
}
应该是这样吧
//先判断是否有参数要绑定
if(parameters != null && parameters.length >0){
for(int i=0;i<parameters.length;i++){
query.setString(i, parameters[i]);
}
}
楼主 以上的代码为hql语句的参数赋值,索引应该从1开始,query.setString(i, parameters[i]);改成query.setString(i+1, parameters[i]);就好了。。
HQL应该起个别名
改一下试试呢:
select u from Users u where u.username = ? and u.password = ?