当前位置: 代码迷 >> J2EE >> SSH项目中遇到的有关问题,请高手解答
  详细解决方案

SSH项目中遇到的有关问题,请高手解答

热度:102   发布时间:2016-04-22 01:45:28.0
SSH项目中遇到的问题,请高手解答。
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测试代码:
  相关解决方案