【问题标题】:Row count for login concept in springboot applicationSpring Boot 应用程序中连接概念的行数
【发布时间】:2019-07-24 12:39:19
【问题描述】:

我正在尝试使用 Spring Boot 应用程序创建登录概念,所以我只需要 mysql 表的行数,我尝试了各种互联网答案,但没有找到解决方案,请您的帮助和建议是适当的。我将在下面提到我的代码。

控制器

@GetMapping("/Login")
public String Login(Model model) {
    model.addAttribute("customer", new Customer());
    return "login";
}

@PostMapping("/LoginProcess")
public String LoginProcess(@ModelAttribute("customer") Customer thecustomer,HttpSession session) {

    System.out.println(thecustomer);
    Customer result = customerservice.Login_service(thecustomer.getUserName(),thecustomer.getPassword());

    if(result==null)
    {
        return "login";
    }
    else if(result.getRole().equals("1"))
    {

        return "admindash";
    }
    else
    {
        //session.setAttribute("jsp_uname", result.getUserName());
        return "customerdash";
    }
}

CustomerImplDao

import javax.persistence.EntityManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.example.filedemo.model.Customer;

@Repository
public class CustomerDaoImpl implements Customerdao {
    @Autowired
    private EntityManager entityManager;

    @Override
    public void save(Customer theCustomer) {
        Session cursession = entityManager.unwrap(Session.class);
        //If Id=0 then It will do insert or id=x then it will do update
        cursession.save(theCustomer);

    }

    @Override
    public Customer Login(String username,String password) {
        Session cursession = entityManager.unwrap(Session.class);   
        String hql="from Customer c where c.UserName=:username and c.password=:password";
        Query<Customer> query = cursession.createQuery(hql,Customer.class);
    query.setParameter("username", username);
    query.setParameter("password", password);
    Customer theCustomer = query.uniqueResult();
    System.out.println("******************"+query.getResultList().size());
      return theCustomer;
    }
}

【问题讨论】:

  • 到底是什么问题?
  • String hql="from Customer c where c.UserName=:username and c.password=:password";
  • 我需要登录行数
  • 不要请不要。您的代码存在多种缺陷,其中之一是使用纯密码。也不要尝试扮演你自己的机制,只需使用 Spring Security 并配置适当的查询。

标签: java spring spring-boot spring-data-jpa jpql


【解决方案1】:

你可以简单地使用:

query.getResultList().size();

或在数据库侧:

entityManager.creteQuery("select count(c) from Customer c where c.UserName=:username and c.password=:password", Long.class).getSingleResult();

为此,您需要将其注入到您的服务(bean)之上:

@Autowired
private EntityManager entityManager;

【讨论】:

  • 非常感谢 query.getResultList().size();我得到了结果
  • 嗨,请您详细说明第二个答案(数据库方面)
  • 嗨,我已经添加了 bean 我已经更新了问题代码,请详细说明 DB 答案,因为我没有找到 DB 端的正确答案。
  • 使用EntityManager 允许在选择查询中计算行数。否则,您将获取列表并检查其大小。
  • 好的,我现在会检查是否有任何错误,我会告诉你帮助我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 2021-10-14
  • 1970-01-01
  • 2020-08-24
  • 2017-05-15
  • 2020-10-26
相关资源
最近更新 更多