最新在学习S2SH的整合,遇到了不少问题。。
现在dao层只有IBaseDao 和 BaseDaoImpl;
这两个做了CURD操作的的接口,和实现。
Service层中的继承泛型dao层,问题是我在junit单元测试的时候,继承basedao的CURD操作没问题,但是service层自己的独有的方法一直报空指针,各位大神帮忙瞅瞅,很急,小弟先在这谢谢了!!下面是我代码!!
IBaseDao.java
package com.yeka.oa.dao; import java.util.List; public interface IBaseDao<T> { public void save(T t); public void delete(Integer id); public void update(T t); public T getObjectById(Integer id); public List<T> getObjectAll(); public List<T> getObjetctByIds(Integer[] ids); }
BaseDaoImpl.java
package com.yeka.oa.dao; import java.lang.reflect.ParameterizedType; import java.util.List; import javax.annotation.Resource; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; @SuppressWarnings("unchecked") @Transactional public class BaseDaoImpl<T> implements IBaseDao<T> { // 注入sessionFactory @Resource public SessionFactory sessionFactory; /** * @return the sessionFactory */ public SessionFactory getSessionFactory() { return sessionFactory; } /** * @param sessionFactory the sessionFactory to set */ public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } Class clazz; // 得到T的真实类型 public BaseDaoImpl() { ParameterizedType tP = (ParameterizedType) this.getClass() .getGenericSuperclass(); clazz = (Class) tP.getActualTypeArguments()[0]; System.out.println(clazz.getName()); } /** * 删除 */ public void delete(Integer id) { // TODO Auto-generated method stub sessionFactory.getCurrentSession().delete( sessionFactory.getCurrentSession().get(clazz, id)); } /** * 获取对象列表 */ public List<T> getObjectAll() { // TODO Auto-generated method stub return sessionFactory.getCurrentSession().createQuery( "from " + clazz.getSimpleName()).list(); } /** * 根据ID查询对象 */ public T getObjectById(Integer id) { return (T) sessionFactory.getCurrentSession().get(clazz, id); } /** * 根据ID数组获取一组对象集合 */ public List<T> getObjetctByIds(Integer[] ids) { return sessionFactory.getCurrentSession().createQuery( "from " + clazz.getSimpleName() + " where id in(:ids)") .setParameter("ids", ids).list(); } public void save(T t) { sessionFactory.getCurrentSession().save(t); } public void update(T t) { sessionFactory.getCurrentSession().update(t); } }
IEmployeeService.java
package com.yeka.oa.service; import java.util.List; import javax.annotation.Resource; import com.yeka.oa.dao.IBaseDao; import com.yeka.oa.entity.SysEmployee; public interface IEmployeeService extends IBaseDao<SysEmployee> { //定义特有方法 //登录 public boolean login(String username,String password); //分页查询 public List<SysEmployee> getPagination(int pageIndex, int pageSize); }
EmployeeServiceImpl.java
package com.yeka.oa.service.impl; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.yeka.oa.dao.BaseDaoImpl; import com.yeka.oa.entity.SysEmployee; import com.yeka.oa.service.IEmployeeService; @Transactional @Service("employeeService") public class EmployeeServiceImpl extends BaseDaoImpl<SysEmployee> implements IEmployeeService { Session session = super.getSessionFactory().getCurrentSession(); @SuppressWarnings("unchecked") public List<SysEmployee> getPagination(int pageIndex, int pageSize) { String hql = "from SysEmployee"; Query query = session.createQuery(hql); query.setMaxResults(pageSize);// 一次查询几条 query.setFirstResult((pageIndex - 1) * pageSize);// 从第几条开始查询 return query.list(); } public boolean login(String username, String password) { boolean flag = true; String hql = "select password from SysEmployee where username='" + username + "'"; Query query = session.createQuery(hql); String temppwd = (String) query.uniqueResult(); if (password != null && temppwd.equals(password)) { flag = true; } else { flag = false; } return flag; } }
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.transaction.annotation.Transactional; import com.yeka.oa.service.IEmployeeService; public class Test { @Transactional @org.junit.Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); IEmployeeService employeeService = (IEmployeeService) context.getBean("employeeService"); employeeService.getObjectAll(); employeeService.login("11", "111"); } }
相关分类