-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestHibernate.java
More file actions
59 lines (48 loc) · 1.57 KB
/
TestHibernate.java
File metadata and controls
59 lines (48 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.com.ttblog.sshbootstrap_table.model.User;
import cn.com.ttblog.sshbootstrap_table.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-context.xml"})
public class TestHibernate {
private static Logger LOG = LoggerFactory.getLogger(TestHibernate.class);
@Autowired
private IUserService userService;
@Autowired
private SessionFactory hibernate;
private Session session;
@Before
public void inject(){
session=hibernate.openSession();
}
@Test
public void testcount(){
Query q=session.createSQLQuery("select count(*) from user");
LOG.debug("查询用户个数:{}",q.uniqueResult());
}
@Test
public void testfind(){
List<User> users=session.createSQLQuery("select * from user").list();
LOG.debug("查询用户列表:{}",users.size());
}
@Test
public void testHql(){
String hql="from User where id=?";
Query q=session.createQuery(hql);
q.setParameter(0, 1L);
List result=q.list();
User u=(User) result.get(0);
LOG.debug("hql-result:{},u.getName():{}",result,u.getName());
}
}