dao层实现方法:
- Java code
package com.ailincms.dao.impl;import java.util.List;import javax.annotation.Resource;import org.hibernate.Query;import org.hibernate.SessionFactory;import org.springframework.stereotype.Repository;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.ailincms.bean.CMSUser;import com.ailincms.dao.CMSUserDao;@Repository(value="cMSUserDaoBean") @Transactionalpublic class CMSUserDaoBean implements CMSUserDao { @Resource SessionFactory factory; public void createCMSUser(CMSUser cmsuser) { try{ factory.getCurrentSession().persist(cmsuser); } catch(RuntimeException e) { throw e; } } public void updateCMSUser(CMSUser cmsuser) { try{ factory.getCurrentSession().merge(cmsuser); } catch(RuntimeException e) { throw e; } } public void deleteByUsername(String username) { try{ factory.getCurrentSession().delete(factory.getCurrentSession().createQuery("from CMSUser as user where user.username='"+username+"'").uniqueResult()); } catch(RuntimeException e) { throw e; } } public void deleteByUserid(Integer userid) { try { factory.getCurrentSession().delete(factory.getCurrentSession().get(CMSUser.class, userid)); } catch (RuntimeException e) { throw e; } } @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.NOT_SUPPORTED) public List<CMSUser> findByProperty(String propertyName, Object value) { try { return factory.getCurrentSession().createQuery("from CMSUser as user where user."+propertyName+" = '"+value+"' ").list(); } catch (RuntimeException e) { throw e; } } @Transactional(propagation=Propagation.NOT_SUPPORTED) public CMSUser findByUsername(String username) { Query q = factory.getCurrentSession().createQuery("from CMSUser as user where user.username=?"); q.setString(0, username); return (CMSUser)q.uniqueResult(); } @Transactional(propagation=Propagation.NOT_SUPPORTED) public CMSUser findByUserid(Integer userid) { try { return (CMSUser)factory.getCurrentSession().get(CMSUser.class, userid); } catch (RuntimeException e) { throw e; } } @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.NOT_SUPPORTED) public List<CMSUser> getAllCMSUser() { try { return factory.getCurrentSession().createQuery("from CMSUser").list(); } catch (RuntimeException e) { throw e; } } }
service层方法:
- Java code
package com.ailincms.service.impl;import java.util.Date;import org.springframework.stereotype.Service;import com.ailincms.bean.CMSUser;import com.ailincms.dao.CMSUserDao;import com.ailincms.dao.impl.CMSUserDaoBean;import com.ailincms.service.CMSUserService;@Service(value="cMSUserServiceBean")public class CMSUserServiceBean implements CMSUserService { CMSUserDao userDao = new CMSUserDaoBean(); public void validateCMSUser(String username, String password) { try{ CMSUser cmsUser = userDao.findByUsername(username); System.out.println(cmsUser.getPassword()); } catch(Exception e){ e.printStackTrace(); } } public Date getLoginTime(String username) { return null; }}
Junit测试代码: