Java tutorial
/* * Copyright 2005-2015 shopxx.net. All rights reserved. * Support: http://3936242.01p.com/ * License: http://3936242.01p.com/license */ package net.shopxx.dao.impl; import javax.persistence.NoResultException; import net.shopxx.dao.AdminDao; import net.shopxx.entity.Admin; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Repository; @Repository("adminDaoImpl") public class AdminDaoImpl extends BaseDaoImpl<Admin, Long> implements AdminDao { public boolean usernameExists(String username) { if (StringUtils.isEmpty(username)) { return false; } String jpql = "select count(*) from Admin admin where lower(admin.username) = lower(:username)"; Long count = entityManager.createQuery(jpql, Long.class).setParameter("username", username) .getSingleResult(); return count > 0; } public Admin findByUsername(String username) { if (StringUtils.isEmpty(username)) { return null; } try { String jpql = "select admin from Admin admin where lower(admin.username) = lower(:username)"; return entityManager.createQuery(jpql, Admin.class).setParameter("username", username) .getSingleResult(); } catch (NoResultException e) { return null; } } }