一张表里面  用户名和密码两个字段。在登录页面到后台的数据,如何判断是否存在数据库。?
怎么用hibernate做?

解决方案 »

  1.   

    把前台的用户名和密码传到后台,然后用这两个参数用hql语句查询看有没有值。
      

  2.   

    不太明白你的意思,是判断数据库中是否有这个表吗?如果这样,可以在hibernate的配置文件中设置一个属性:hibernate.hbm2ddl.auto = update,这样启动的时候,他会检测,如果没有则自动见表。如果只是查询数据的话,写个hql查询即可。给你个例子:
    return getHibernateTemplate().execute(new HibernateCallback<List<E>>() {
        public List<E> doInHibernate(Session session) throws HibernateException {
            Query query = session.createQuery("FROM User u WHERE u.name=:name");
            query.setString("name", name);
    return query.list();
        }
    });
      

  3.   

    http://blog.csdn.net/shutingwang/article/details/6691205
    希望对你有帮助。我刚工作时候写的。
      

  4.   

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=hibernate</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">svse</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        
        <mapping resource="Peson.hbm.xml"/>
        </session-factory>
        
        </hibernate-configuration>
      

  5.   

    首先,你得创建一个查找用户的方法,再用hibernate封装好的find方法执行,最后登陆的时候调用就行。
      

  6.   

    HQL 语句
    public User Login(String username) {
    String hql = "from User user where user.username = ? ";
    User user = (User) this.getHibernateTemplate().find(hql, username).get(0);
    return user;
    }action 进行判断
    public String execute() throws Exception {
    User u = (User) userManager.Login(username);
    Map session = ActionContext.getContext().getSession();

    if(u != null && u.getPassword().equals(password))
    {
    this.user = u;
    ActionContext.getContext().getSession().put("login", u);
    return SUCCESS;
    }
    this.setManager("用户名或密码错误!!");

    return INPUT;
    }

    }
      

  7.   

    String hql = "from user where username = 'yourname' and password = 'password'"返回的记录数据是否为1