java 泛型dao_Java泛型dao的问题,求助,急啊!!

最新在学习S2SH的整合,遇到了不少问题。。

现在dao层只有IBaseDao 和 BaseDaoImpl;

这两个做了CURD操作的的接口,和实现。

Service层中的继承泛型dao层,问题是我在junit单元测试的时候,继承basedao的CURD操作没问题,但是service层自己的独有的方法一直报空指针,各位大神帮忙瞅瞅,很急,小弟先在这谢谢了!!下面是我代码!!

IBaseDao.javapackage com.yeka.oa.dao;

import java.util.List;

public interface IBaseDao {

public void save(T t);

public void delete(Integer id);

public void update(T t);

public T getObjectById(Integer id);

public List getObjectAll();

public List getObjetctByIds(Integer[] ids);

}

BaseDaoImpl.javapackage 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 implements IBaseDao {

// 注入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 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 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.javapackage 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 {

//定义特有方法

//登录

public boolean login(String username,String password);

//分页查询

public List getPagination(int pageIndex, int pageSize);

}

EmployeeServiceImpl.javapackage 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 implements IEmployeeService {

Session session = super.getSessionFactory().getCurrentSession();

@SuppressWarnings("unchecked")

public List 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.javaimport 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");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值