public class TestConn extends TestCase {
public static DepartmentDAO dao;
private static ClassPathXmlApplicationContext context = null;
private static final String APPLICATION_CONTEXT_PATH = "com/tansun/attendance/conf/";
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
protected Object getBean(String beanName) {
if (beanName == null)
throw new IllegalArgumentException(beanName + " parameter is null!");
return context.getBean(beanName);
}
protected void setUp() throws Exception {
super.setUp();
if (context == null) {
String[] confs = new String[] { "applicationContext.xml",
"hibernate.cfg.xml", };
for (int i = 0; i < confs.length; i++) {
confs[i] = APPLICATION_CONTEXT_PATH + confs[i];
}
context = new ClassPathXmlApplicationContext(confs);
}
}
public static void main(String[] args) {
/*
* 读取配置 文件的几种方式
*/
/*********************
* 一、 使用 ClassPathResource *******************
*/
// Resource reasource = new
// ClassPathResource("conf/applicationContext.xml");
// BeanFactory factory = new XmlBeanFactory(reasource);
// DepartmentDAO dao = (DepartmentDAO) factory.getBean("DepartmentDAO");
// DepartmentDAO dao = new DepartmentDAO();
/************************************
* 二、 使用 applicationContext 找到 applicationContext.xml 配置文件
* **********************************
*/
String sprconf = "conf/applicationContext.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(sprconf);
DepartmentDAO dao = (DepartmentDAO) ac.getBean("DepartmentDAO");
Department department = new Department();
department.setDid(2);
/*
* 不用 hibernate.cfg.xml 文件 使用 dataSource 配置,将 hibernate 配置到spring 中时, 直接
* 使用 spring Bean 操作数据库
*/
dao.delete(department);
/*
* 使用 hibernate 配置 文件时,需要使用 sessionFactory 创建 一个 session , 通过 session
* 来操作数据库
*/
// String hibconf = "conf/hibernate.cfg.xml";
// SessionFactory sf = new
// Configuration().configure(hibconf).buildSessionFactory();
// Session session = sf.openSession();
// Transaction tx = session.beginTransaction();
// session.delete(department);
// tx.commit();
// Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");
// System.out.println("-------"+sdf.format(date));
// department.setSysUpdatetime(testDate());
//
// // HibernateDaoSupport hib = new HibernateDaoSupport();
//
// dao.save(department);
// testDate();
}
public static String testDate() {
Date date = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat("EEEE-MMMM-dd-yyyy");//
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-SS");
System.out.println("-------" + sdf.format(date));
return sdf.format(date);
}
}