??????????????????
???????????? ???????[ 2016/6/29 11:56:23 ] ??????????????? ???????
?????????????????????????д??????????????
1 private Session session;
2 private Transaction transaction;
3
4 @Before
5 public void init(){
6 session = getSession();
7 transaction = session.beginTransaction();
8 }
9
10 @After
11 public void destroy(){
12 transaction.commit();
13 session.close();
14 }
??????????????£????????????BaseTest?????????????????????????????????????????????????£?
1 class BaseTest {
2 static Session session;
3 private static Transaction transaction;
4
5 @Before
6 public void init() {
7 session = getSession();
8 transaction = session.beginTransaction();
9 System.out.println("[???]"+new Date()+" session???????????????????");
10 }
11
12 @After
13 public void destroy() {
14 transaction.commit();
15 session.close();
16 System.out.println("[???]"+new Date()+" ??????????session???????");
17 }
18 }
?????????????????д??
public class QueryByCriteria extends BaseTest {
@Test
public void QueryTest(){
Criteria criteria = session.createCriteria(ClassRoom.class);
List<ClassRoom> list = criteria.list();
list.stream()
.forEach(item->System.out.println(item.getClassName()));
}
}
????????HibernateUtil??
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration();
configuration.configure("Hibernate.cfg.xml");
// ???????????????д???????????
configuration.addAnnotatedClass(ClassRoom.class);
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
return configuration.buildSessionFactory(ssrb.build());
}
catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed !" + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11