春季启动应用程序中登录概念的行计数

问题描述 投票:1回答:1

我正在尝试使用spring启动应用程序创建一个登录概念,所以我只需要一个mysql表的行计数,我尝试了各种互联网答案,但没有任何解决方案,请你的帮助和建议是appriciate。我会在下面提到我的代码。

调节器

@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;
    }
}
java spring hibernate spring-boot java-ee
1个回答
3
投票

你可以简单地使用:

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;
© www.soinside.com 2019 - 2024. All rights reserved.