forked from maxliaops/Java_Web_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodsDAO.java
More file actions
131 lines (125 loc) · 4.42 KB
/
GoodsDAO.java
File metadata and controls
131 lines (125 loc) · 4.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.dao;
import org.hibernate.Session;
import java.util.List;
import org.hibernate.Query;
import com.actionForm.GoodsForm;
import org.hibernate.Transaction;
import com.core.MySession;
public class GoodsDAO {
private Session session = null;
public List query(String strif, int del) {
session = MySession.openSession(); //打开Session
String hql = "";
if (strif != "all" && strif != null && strif != "") { //条件查询
hql = "FROM GoodsForm goods WHERE " + strif + "";
} else { //查询全部数据
hql = "FROM GoodsForm goods WHERE ifdel=" + del + " ORDER BY ifdel";
}
System.out.println(hql);
List list=null;
try {
Query query = session.createQuery(hql);
list = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
MySession.closeSession(session); //关闭Session
}
return list;
}
//修改商品信息时的查询
public GoodsForm query(int id) {
session = MySession.openSession(); //打开Session
GoodsForm goodsForm = null;
try {
goodsForm = (GoodsForm) session.get(GoodsForm.class, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
MySession.closeSession(session); //关闭Session
}
return goodsForm;
}
public int insert(GoodsForm goodsForm) {
int ret = 0;
Transaction tx = null;
String str = "name='" + goodsForm.getName() + "' AND spec='" +
goodsForm.getSpec() + "'";
List list = query(str, 0);
if (list.size() > 0) { //存在该信息
ret = 2;
} else {
session = MySession.openSession(); //打开Session
try {
tx = session.beginTransaction();
session.save(goodsForm);
tx.commit();
ret = 1;
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
System.out.println("添加物资信息时的错误信息:" + e.getMessage());
return ret = 0;
} finally {
MySession.closeSession(session); //关闭Session
}
}
return ret;
}
// 修改物资信息
public int update(GoodsForm goodsForm) {
session = MySession.openSession(); //打开Session
int ret = 0;
Transaction tx = null;
try {
tx = session.beginTransaction();
// ChStr chStr=new ChStr();
GoodsForm goodsF = (GoodsForm) session.get(GoodsForm.class,
goodsForm.getId());
goodsF.setSpec(goodsForm.getSpec());
goodsF.setUnit(goodsForm.getUnit());
goodsF.setPrice(goodsForm.getPrice());
goodsF.setProducer(goodsForm.getProducer());
// goodsF.setSpec(chStr.toChinese(goodsForm.getSpec()));
// goodsF.setUnit(chStr.toChinese(goodsForm.getUnit()));
// goodsF.setPrice(goodsForm.getPrice());
// goodsF.setProducer(chStr.toChinese(goodsForm.getProducer()));
session.update(goodsF);
tx.commit();
ret = 1;
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
System.out.println("修改物资信息时的错误信息:" + e.getMessage());
return ret = 0;
} finally {
MySession.closeSession(session); //关闭Session
}
return ret;
}
public int del(int id, byte val) {
session = MySession.openSession(); //打开Session
int ret = 0;
Transaction tx = null;
try {
tx = session.beginTransaction();
GoodsForm goodsF = (GoodsForm) session.get(GoodsForm.class, id);
goodsF.setIfdel(val);
System.out.println(val + "*********" + id);
session.update(goodsF);
tx.commit();
ret = 1;
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
System.out.println("删除/恢复物资信息时的错误信息:" + e.getMessage());
return ret = 0;
} finally {
MySession.closeSession(session); //关闭Session
}
return ret;
}
}