Home > Database > Mysql Tutorial > body text

Hibernte配置以及junit测试笔记

WBOY
Release: 2016-06-07 16:06:26
Original
1070 people have browsed it

Hibernate配置 junit测试 import static org.junit.Assert.*; public class HibernateTest{ SessionFactory factory=null; Session session=null; //测试之前初始化数据 @Before public void setUp() throws Eception{ System.out.println(初始化); Configur

Hibernate配置
junit测试
import static org.junit.Assert.*;


public class HibernateTest{
SessionFactory factory=null;
Session session=null;
//测试之前初始化数据
@Before
public void setUp() throws Eception{
System.out.println("初始化");
Configuration config=new Configuration


().configure();

ServiceRegistry sr=new


ServiceRegistryBuilder.applySetting(config.getProperties());
factory=config.buildSessionFactory(sr);
session=factory.openSession();



}
@After
//测试之后释放数据
public void tearDown() throws Exception{
System.out.println("结束,释放资源");
if(session.isOpen()){
session.close();
}
}

}
@Test测试方法
public void tearDown() throws Exception{
System.out.println("初始化");



} @Test
public void testAdd(){
Transaction tx=session.beginTransaction();
Person p=new Person("admin",123456,new Date


());
session.persist(p);//新增需要事务
tx.commit();


}
}
//Hibernate深入配置
access

//field(可以不需要getter/setter方法操作对象属性)
(尽量不要这样设置)
Person p=(Person)session.get(Person.class,id);//根据反射创建


对象
System.out.println(p);




Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template