hibernate+Spring 中使用sql語句
以下兩個方法是DAO成的方法:
1
public List queryBySQL(PageInfo page,String deptids) {
2
final String[] params={};
3
final String sql= " From TDatasrc t1 Where t1.TDept.deptid In ("+deptids+")";
4
5
System.out.println("sql=========="+sql);
6
System.out.println("params=========="+params);
7
8
int rowsCount = queryListCountForJDBC(sql, params).intValue();
9
10
System.out.println("rowsCount=========="+rowsCount);
11
page.setRowsCount(rowsCount);
12
13
final int rowNumber = page.getRowNumber();
14
final int firstReslult = page.getFirstIndex();
15
// return this.getSession().createSQLQuery(hql).addEntity(TFunction.class).list();
16
return getHibernateTemplate().executeFind(new HibernateCallback() {
17
18
public Object doInHibernate(Session session)
19
throws HibernateException, SQLException {
20
Query query = session.createQuery(sql);
21
if(params!=null){
22
for (int i = 0; i < params.length; i++) {
23
query.setParameter(i, params[i]);
24
}
25
}
26
27
if (rowNumber > 0) {
28
query.setFirstResult(firstReslult);
29
query.setMaxResults(rowNumber);
30
}
31
32
return query.list();
33
}
34
35
});
36
}

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

1
public List queryDeptidBySql(String deptid){
2
String sql="Select * "+
3
"From t_Dept "+
4
"Connect By Prior Deptcode = Parentcode "+
5
"Start With Deptid ='"+deptid+"'";
6
System.out.println(""+sql);
7
// List list = getHibernateTemplate().find(sql);
8
return this.getSession().createSQLQuery(sql).addEntity(TDept.class).list();
9
}

2

3

4

5

6

7

8

9

該方法是Service層的方法,通過調用DAO層的方法實現對數據庫的操作
1
public List qureyDatasrcBySql(PageInfo page,String deptid) {
2
3
List<String> list_deptid = new ArrayList<String>();
4
TDept dept=new TDept();
5
List list_dept=datasrcManageDAO.queryDeptidBySql(deptid);
6
for(int i=0;i<list_dept.size();i++){
7
dept=(TDept)list_dept.get(i);
8
list_deptid.add(dept.getDeptid());
9
10
}
11
12
String deptids="";
13
14
for(int i=0;i<list_deptid.size();i++){
15
if(deptids.equals(""))
16
deptids="'"+list_deptid.get(i)+"'";
17
else
18
deptids=deptids+",'"+list_deptid.get(i)+"'";
19
}
20
System.out.println("********* deptids[0]*******************"+ deptids);
21
return datasrcManageDAO.queryBySQL(page,deptids);
22
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

posted on 2008-10-06 15:53 魯勝迪 閱讀(1854) 評論(0) 編輯 收藏 所屬分類: 一點點