SSH 查询所有数据的测试类 报空指针 求指教
三层架构:
Dao层:
package com.shhelian.app.dao;
import java.util.List;
public interface MasterDao {
public List find(String hql);
}
Impl:
package com.shhelian.app.dao.impl;
import java.util.List;
import javax.persistence.Entity;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.shhelian.app.dao.MasterDao;
@Entity
@Component("MasterDao")
public class MasterDaoImpl extends HibernateDaoSupport implements MasterDao {
@Override
public List find(String hql) {
return super.getHibernateTemplate().find(hql);
}
@Autowired
public void overSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
}
server层:
package com.shhelian.app.service;
import java.util.List;
public interface MasterServer {
public List getAll();
}
Impl:
package com.shhelian.app.service.impl;
import java.util.List;
import javax.persistence.Entity;
import org.springframework.stereotype.Component;
import com.shhelian.app.dao.MasterDao;
import com.shhelian.app.service.MasterServer;
@Entity
@Component("MasterService")
public class MasterServerImpl implements MasterServer {
public MasterDao mdao;
@Override
public List getAll() {
System.out.println("xxxxxxxxx");
return this.mdao.find("from Form_Account_Master");
}
public void setMdao(MasterDao mdao) {
this.mdao = mdao;
}
}
这是我写的一个测试类 。 查询Form_Account_master表中的数据的size
package com.shhelian.app.util;
import java.util.List;
import com.shhelian.app.model.FormAccountMaster;
import com.shhelian.app.service.impl.MasterServerImpl;
public class Test {
private static CustomerContextHolder ch;
private static DataSourceMap dp;
public static void main(String[] args) {
ch.setCustomerType(dp.yxh); //设置数据源
MasterServerImpl mp = new MasterServerImpl();
List list = mp.getAll();
System.out.println(list.size());
}
}
报空指针的错误 ,代码很简单 可是错误我找了好久都没有找到,求大神帮忙 。 还有我Spring配置文件里配置的是两个数据源:分别是Mysql和Sqlserver 项目部署的时候没有任何问题。 下面是错误提示:
xxxxxxxxx --- 这里是我上面代码中输出的
Exception in thread "main" java.lang.NullPointerException
at com.shhelian.app.service.impl.MasterServerImpl.getAll(MasterServerImpl.java:21)
at com.shhelian.app.util.Test.main(Test.java:16)
[解决办法]
测试类要这样写:
package com.honglin.test;
import java.util.List;
import net.sf.json.JSONArray;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.honglin.dao.HrDepartmentDao;
import com.honglin.dao.HrPostDao;
import com.honglin.dao.HrStaffDao;
import com.honglin.dao.impl.HrDepartmentDaoImpl;
import com.honglin.dao.impl.HrFunctionDaoImpl;
import com.honglin.dao.impl.HrPostDaoImpl;
import com.honglin.dao.impl.HrStaffDaoImpl;
import com.honglin.domain.HrDepartment;
import com.honglin.domain.HrFunction;
import com.honglin.domain.HrPost;
public class Function {
private ApplicationContext ctx;
private 你的DAO
@Before
public void init() {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
你的DAO = ctx.getBean("你的DAO注解的名字", DAO的类名.class);
}
@Test
public void HrFunctionFindAll() {
你的DAO .要测试的方法
}
}