Hibernate 几种本地单元测试 的操作数据库 方法

    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);

}

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值