action直接调的DAO。
dao代码为:
public class UserDao extends HibernateDaoSupport {
//普通用户登录验证
@SuppressWarnings("unchecked")
public boolean checkUser(String userName,String userPassword){
boolean flag = false;
String hql = "from Userinfo as user where user.username = '"+userName+"' and user.userpassword = '"+userPassword+"'";
List<Userinfo> userList = this.getHibernateTemplate().find(hql);
if(userList.size()>0){
flag = true;
}
return flag;
}
applicationContext.xml中这样注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--指明hibernate配置文件的位置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!--定义DAO -->
<bean id="userDao" class="com.dao.UserDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 配置LoginAction.java类文件 -->
<bean id="LoginAction" class="com.action.LoginAction" scope="prototype">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
</beans>
Userinfo.hbm.xml文件是自动生成的
<hibernate-mapping>
<class name="com.bean.Userinfo" table="userinfo" catalog="test">
<id name="userName" type="java.lang.String">
<column name="userName" length="6" />
<generator class="identity" />
</id>
<property name="userPassword" type="java.lang.String">
<column name="userPassword" length="45" not-null="true" />
</property>
<property name="userRole" type="java.lang.Integer">
<column name="userRole" not-null="true" />
</property>
</class>
</hibernate-mapping>
hibernate.cfg.xml文件:
里面有两句话我不是很理解
<property name="myeclipse.connection.profile">mysql</property>这句是什么意思?
<mapping resource="com/bean/Userinfo.hbm.xml" />这句我知道是配置该bean的xml
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">mysql</property>
<mapping resource="com/bean/Userinfo.hbm.xml" />
</session-factory>
------解决方案--------------------
你先运行:from Userinfo as user
如果可以的话,那么就是你后面的user.username或user.userpassword写错了!看看Userinfo类中属性是不是这个,hql必须换成实体类中的属性名称,而不是表的字段名称。