001 |
引用 |
002 |
|
003 |
|
004 |
struts 2 、hibernate、spring所需jar包 |
005 |
|
006 |
struts-core -2 .x.x.jar ----struts核心包 |
007 |
xwork-core -2 .x.x.jar -----身體ruts在其撒很難過構建 |
008 |
ognl -2.6 .x.jar ----對象導航語言 |
009 |
freemarker -2.3 .x.jar ------struts 2 的ui標籤的模板使用 |
010 |
commons-fileupload -1.2 .x.jar ----文件上傳組件 2.1 . 6 版本後需加入此文件 |
011 |
struts-spring-plugin -2 .x.x.jar ---用於struts 2 繼承spring的插件 |
012 |
|
013 |
hibernate核心安裝包下的(下載路徑:http://www.hibernate.org/ ,點擊Hibernate Core 右邊的download) |
014 |
hibernate 2 .jar |
015 |
lib\bytecode\hibernate-cglib-repack -2.1 _ 3 .jar |
016 |
lib\required\*.jar |
017 |
hibernate安裝包下的(下載路徑:http://www.hibernate.org/;點擊Hibernate Annotations 右邊的下載) |
018 |
hibernate-annotations.jar |
019 |
lib\ejb 3 -persistence.jar、hibernate-commons-annotations.jar |
020 |
hibernate針對JPA的實現包(下載路徑:http://www.hibernate.org/ ,點擊Hibernate Entitymanager右邊下載) |
021 |
hibernate-entitymanager.jar |
022 |
lib\test\log 4 j.jar、 slf 4 j-log 4 j 12 .jar |
023 |
|
024 |
spring安裝包下的 |
025 |
dist\spring.jar |
026 |
lib\c 3 p
\c 3 p
-0.9 . 1.2 .jar |
027 |
lib\aspecti\aspectjweaver.jar |
028 |
aspectjrt.jar |
029 |
lib\colib\cglib-nodep -2.1 _ 3 .jar |
030 |
lib\j 2 ee\common-annotations.jar |
031 |
vlib\log 4 j\log 4 j -1.2 . 15 .jar |
032 |
lib\jakarta-commons\commons_loggin.jar |
033 |
|
034 |
數據庫驅動包 |
035 |
|
036 |
|
037 |
|
038 |
引用 |
039 |
|
040 |
|
041 |
建立mysql數據庫ssh 設置編碼爲utf -8 語句: |
042 |
create database ssh character set 'utf8' collate 'utf8_general_ci' |
043 |
|
044 |
|
045 |
|
046 |
引用 |
047 |
|
048 |
|
049 |
1 .先整合spring和hibernate |
050 |
*將spring和hibernate的jar包放入lib下; |
051 |
*建立spring的beans.xml配置文件 |
052 |
|
053 |
|
054 |
|
055 |
Java代碼 |
056 |
1 .<?xml version= "1.0" encoding= "UTF-8" ?> |
057 |
2 .<beans xmlns= "http://www.springframework.org/schema/beans" |
058 |
3 . xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/schema/context" |
059 |
4 . xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:tx= "http://www.springframework.org/schema/tx" |
060 |
5 . xsi:schemaLocation="http://www.springframework.org/schema/beans |
061 |
6 . http://www.springframework.org/schema/beans/spring-beans -2.5 .xsd |
062 |
7 . http://www.springframework.org/schema/context |
063 |
8 . http://www.springframework.org/schema/context/spring-context -2.5 .xsd |
064 |
9 . http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop -2.5 .xsd |
065 |
10 . http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx -2.5 .xsd"> |
066 |
11 . |
067 |
12 . <!-- 將bean交由spring管理能夠 用<bean></bean>和掃描加註 --> |
068 |
13 . <!-- |
069 |
14 . 掃描該包及該包下的子包 |
070 |
15 . --> |
071 |
16 . <context:component-scan base-package= "com.yss" ></context:component-scan> |
072 |
17 . |
073 |
18 . |
074 |
19 . <!-- 集成hibernate sessionFactory單例模式 線程安全 建立耗內存--> |
075 |
20 . <!-- 將hibernate的事務也交由spring管理 --> |
076 |
21 . <bean id= "dataSource" class= "com.mchange.v2.c3p0.ComboPooledDataSource" |
077 |
22 . destroy-method= "close" > |
078 |
23 . <property name= "driverClass" value= "org.gjt.mm.mysql.Driver" /> |
079 |
24 . <property name= "jdbcUrl" |
080 |
25 . value= "jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8" /> |
081 |
26 . <property name= "user" value= "root" /> |
082 |
27 . <property name= "password" value= "root" /> |
083 |
28 . <!--初始化時獲取的鏈接數,取值應在minPoolSize與maxPoolSize之間。Default: 3 --> |
084 |
29 . <property name= "initialPoolSize" value= "1" /> |
085 |
30 . <!--鏈接池中保留的最小鏈接數。--> |
086 |
31 . <property name= "minPoolSize" value= "1" /> |
087 |
32 . <!--鏈接池中保留的最大鏈接數。Default: 15 --> |
088 |
33 . <property name= "maxPoolSize" value= "300" /> |
089 |
34 . <!--最大空閒時間, 60 秒內未使用則鏈接被丟棄。若爲
則永不丟棄。Default: --> |
090 |
35 . <property name= "maxIdleTime" value= "60" /> |
091 |
36 . <!--當鏈接池中的鏈接耗盡的時候c 3 p
一次同時獲取的鏈接數。Default: 3 --> |
092 |
37 . <property name= "acquireIncrement" value= "5" /> |
093 |
38 . <!--每 60 秒檢查全部鏈接池中的空閒鏈接。Default: --> |
094 |
39 . <property name= "idleConnectionTestPeriod" value= "60" /> |
095 |
40 . </bean> |
096 |
41 . |
097 |
42 . <bean id= "sessionFactory" |
098 |
43 . class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
099 |
44 . <property name= "dataSource" ref= "dataSource" /> |
100 |
45 . <property name= "mappingResources" ><!-- 放置hibernate的配置文件 --> |
101 |
46 . <list> |
102 |
47 . <value>com/yss/bean/Employee.hbm.xml</value> |
103 |
48 . </list> |
104 |
49 . </property> |
105 |
50 . <property name= "hibernateProperties" > |
106 |
51 . <value> |
107 |
52 . hibernate.dialect=org.hibernate.dialect.MySQL 5 Dialect |
108 |
53 . hibernate.hbm 2 ddl. auto =update |
109 |
54 . hibernate.show_sql=true |
110 |
55 . hibernate.format_sql=false |
111 |
56 . </value> |
112 |
57 . </property> |
113 |
58 . </bean> |
114 |
59 . |
115 |
60 . <!--hibernate事務管理器配置--> |
116 |
61 . <bean id= "transactionManager" class= "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
117 |
62 . <property name= "sessionFactory" ref= "sessionFactory" ></property> |
118 |
63 . </bean> |
119 |
64 . |
120 |
65 . <!--spring能夠用xml和註解來配置事務 聲明 --> |
121 |
66 . <tx:annotation-driven transaction-manager= "transactionManager" /> |
122 |
67 .</beans> |
123 |
|
124 |
*配置hibernate的model.hbm.xml和建立model類 |
125 |
*建立service |
126 |
service接口: |
127 |
|
128 |
|
129 |
|
130 |
Java代碼 |
131 |
1 .public interface EmployeeService { |
132 |
2 . public boolean save(Employee employee); |
133 |
3 . public boolean update(Employee employee); |
134 |
4 . public Employee find(String username); |
135 |
5 . public boolean delete(String... username);//表示可變參數 |
136 |
6 . public List<Employee> findAll(); |
137 |
7 .} |
138 |
|
139 |
service實現類: |
140 |
|
141 |
|
142 |
|
143 |
Java代碼 |
144 |
1 .import java.util.List; |
145 |
2 . |
146 |
3 .import javax.annotation.Resource; |
147 |
4 . |
148 |
5 .import org.apache.log 4 j.Logger; |
149 |
6 .import org.hibernate.SessionFactory; |
150 |
7 .import org.springframework.stereotype.Service; |
151 |
8 .import org.springframework.transaction.annotation.Propagation; |
152 |
9 .import org.springframework.transaction.annotation.Transactional; |
153 |
10 . |
154 |
11 .import com.yss.bean.Employee; |
155 |
12 .import com.yss.service.EmployeeService; |
156 |
13 . |
157 |
14 . /** |
158 |
15. * @author qing 默認bean名稱 employeeServiceBean |
159 |
16. *@Service @Transactional 注入service和開啓事務 |
160 |
17. */ |
161 |
18 .@Service |
162 |
19 .@Transactional |
163 |
20 .public class EmployeeServiceBean implements EmployeeService { |
164 |
21 . private static Logger logger = Logger.getLogger(Employee.class); |
165 |
22 . /** |
166 |
23. * 注入sessionFactory |
167 |
24. */ |
168 |
25 . @Resource SessionFactory factory; |
169 |
26 . |
170 |
27 . public boolean delete(String... usernames) { |
171 |
28 . try { |
172 |
29 . for (String username : usernames) { |
173 |
30 . factory.getCurrentSession().delete( |
174 |
31 . factory.getCurrentSession().load(Employee.class, |
175 |
32 . username)); |
176 |
33 . } |
177 |
34 . } catch (Exception e) { |
178 |
35 . logger.error(e.getMessage()); |
179 |
36 . return false; |
180 |
37 . } |
181 |
38 . return true; |
182 |
39 . } |
183 |
40 . |
184 |
41 . /* |
185 |
42. * (non-Javadoc) |
186 |
43. * |
187 |
44. * @see com.yss.service.EmployeeService#find(com.yss.bean.Employee) |
188 |
45. * 此標註表示不須要事務處理 |
189 |
46. */ |
190 |
47 . @Transactional(propagation = Propagation.NOT_SUPPORTED) |
191 |
48 . public Employee find(String username) { |
192 |
49 . return (Employee) factory.getCurrentSession().get(Employee.class, |
193 |
50 . username); |
194 |
51 . } |
195 |
52 . |
196 |
53 . @SuppressWarnings( "unchecked" ) |
197 |
54 . @Transactional(propagation = Propagation.NOT_SUPPORTED) |
198 |
55 . public List<Employee> findAll() { |
199 |
56 . return factory.getCurrentSession().createQuery( "from Employee emp" ) |
200 |
57 . .list(); |
201 |
58 . } |
202 |
59 . |
203 |
60 . public boolean save(Employee employee) { |
204 |
61 . try { |
205 |
62 . factory.getCurrentSession().persist(employee);// .save(employee);// |
206 |
63 . // 獲取已經開好的Session |
207 |
64 . } catch (Exception e) { |
208 |
65 . logger.error(e.getMessage()); |
209 |
66 . return false; |
210 |
67 . } |
211 |
68 . return true; |
212 |
69 . } |
213 |
70 . |
214 |
71 . public boolean update(Employee employee) { |
215 |
72 . try { |
216 |
73 . factory.getCurrentSession().merge(employee);// 相似於saveOrUpdate()方法 |
217 |
74 . } catch (Exception e) { |
218 |
75 . logger.error(e.getMessage()); |
219 |
76 . return false; |
220 |
77 . } |
221 |
78 . return true; |
222 |
79 . } |
223 |
80 . |
224 |
81 .} |
225 |
|
226 |
*新建測試類 |
227 |
|
228 |
|
229 |
|
230 |
Java代碼 |
231 |
1 .public class EmployeeTest { |
232 |
2 . private static EmployeeService employeeService; |
233 |
3 . |
234 |
4 . @BeforeClass |
235 |
5 . public static void setUpBeforeClass() throws Exception { |
236 |
6 . try { |
237 |
7 . ApplicationContext context = new ClassPathXmlApplicationContext( |
238 |
8 . "beans.xml" ); |
239 |
9 . employeeService = (EmployeeService) context |
240 |
10 . .getBean( "employeeServiceBean" ); |
241 |
11 . } catch (Exception e) { |
242 |
12 . System.out.println(e.getMessage()); |
243 |
13 . } |
244 |
14 . } |
245 |
15 . |
246 |
16 . @Test |
247 |
17 . public void createTable() { |
248 |
18 . //new ClassPathXmlApplicationContext( "beans.xml" ); |
249 |
19 . }; |
250 |
20 . |
251 |
21 . @Test |
252 |
22 . public void save() { |
253 |
23 . boolean result=employeeService.save(new Employee( "long" , "long" )); |
254 |
24 . if (result) { |
255 |
25 . System.out.println( "保存成功。。。。。" ); |
256 |
26 . }else{ |
257 |
27 . System.out.println( "保存出錯...." ); |
258 |
28 . } |
259 |
29 . }; |
260 |
30 . |
261 |
31 . @Test |
262 |
32 . public void delete() { |
263 |
33 . boolean result=employeeService.delete( "long" ); |
264 |
34 . if (result) { |
265 |
35 . System.out.println( "刪除成功。。。。。" ); |
266 |
36 . }else{ |
267 |
37 . System.out.println( "刪除出錯...." ); |
268 |
38 . } |
269 |
39 . }; |
270 |
40 . |
271 |
41 . @Test |
272 |
42 . public void update() { |
273 |
43 . boolean result=employeeService.update((new Employee( "qing" , "long" ))); |
274 |
44 . if (result) { |
275 |
45 . System.out.println( "更新成功。。。。。" ); |
276 |
46 . }else{ |
277 |
47 . System.out.println( "更新出錯...." ); |
278 |
48 . } |
279 |
49 . }; |
280 |
50 . |
281 |
51 . @Test |
282 |
52 . public void findAll() { |
283 |
53 . List<Employee> elist=employeeService.findAll(); |
284 |
54 . Iterator itor=elist.iterator(); |
285 |
55 . while(itor.hasNext()){ |
286 |
56 . Employee emp=(Employee)itor.next(); |
287 |
57 . System.out.println(emp.getPassword()); |
288 |
58 . } |
289 |
59 . }; |
290 |
60 . |
291 |
61 . @Test |
292 |
62 . public void find(){ |
293 |
63 . Employee employee=employeeService.find( "qing" ); |
294 |
64 . System.out.println(employee.getPassword()); |
295 |
65 . } |
296 |
66 .} |
297 |
|
298 |
*ok 沒問題spring和hibernate整合完畢 |
299 |
|
300 |
*再將struts 2 所需包加入lib中 |
301 |
*建立struts.xml配置文件 |
302 |
|
303 |
|
304 |
|
305 |
Java代碼 |
306 |
1 .<?xml version= "1.0" encoding= "UTF-8" ?> |
307 |
2 .<!DOCTYPE struts PUBLIC |
308 |
3 . "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" |
309 |
4 . "http://struts.apache.org/dtds/struts-2.0.dtd" > |
310 |
5 .<struts> |
311 |
6 . <!-- 將struts的action交由spring管理 不在由struts的工廠介入 --> |
312 |
7 . <constant name= "struts.objectFactory" value= "spring" /> |
313 |
8 . |
314 |
9 . <package name= "employee" namespace= "/employee" extends= "struts-default" > |
315 |
10 . <action name= "list" class= "employeeAction" > |
316 |
11 . <result name= "success" > |
317 |
12 . /WEB-INF/feapp/employee.jsp |
318 |
13 . </result> |
319 |
14 . </action> |
320 |
15 . |
321 |
16 . <action name= "manager_*" class= "employeeManagerAction" method= "{1}" > |
322 |
17 . <result name= "success" > |
323 |
18 . /WEB-INF/feapp/employeeadd.jsp |
324 |
19 . </result> |
325 |
20 . <result name= "message" > |
326 |
21 . /WEB-INF/feapp/result.jsp |
327 |
22 . </result> |
328 |
23 . </action> |
329 |
24 . </package> |
330 |
25 .</struts> |
331 |
|
332 |
*在web.xml中加入 |
333 |
|
334 |
|
335 |
|
336 |
Java代碼 |
337 |
1 .<?xml version= "1.0" encoding= "UTF-8" ?> |
338 |
2 .<web-app version= "2.4" xmlns= "http://java.sun.com/xml/ns/j2ee" |
339 |
3 . xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" |
340 |
4 . xsi:schemaLocation="http://java.sun.com/xml/ns/j 2 ee |
341 |
5 . http://java.sun.com/xml/ns/j 2 ee/web-app_ 2 _ 4 .xsd"> |
342 |
6 . |
343 |
7 . <!-- |
344 |
8 . 指定spring的配置文件,默認從web根目錄尋找配置文件,咱們能夠經過spring提供的classpath:前綴指定從類路徑下尋找 |
345 |
9 . --> |
346 |
10 . <context-param> |
347 |
11 . <param-name>contextConfigLocation</param-name> |
348 |
12 . <param-value>classpath:beans.xml</param-value><!-- 多個配置文件的寫法 classpath:beans 1 .xml,classpath:beans 2 .xml,classpath:beans 3 .xml --> |
349 |
13 . </context-param> |
350 |
14 . <!-- 對Spring容器進行實例化 --> |
351 |
15 . <listener> |
352 |
16 . <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
353 |
17 . </listener> |
354 |
18 . |
355 |
19 . <!-- struts 2 的監聽器 --> |
356 |
20 . <filter> |
357 |
21 . <filter-name>struts 2 </filter-name> |
358 |
22 . <filter-class> |
359 |
23 . org.apache.struts 2 .dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> |
360 |
24 . </filter> |
361 |
25 . <filter-mapping> |
362 |
26 . <filter-name>struts 2 </filter-name> |
363 |
27 . <url-pattern>/*</url-pattern> |
364 |
28 . </filter-mapping> |
365 |
29 . |
366 |
30 . <welcome-file-list> |
367 |
31 . <welcome-file>index.jsp</welcome-file> |
368 |
32 . </welcome-file-list> |
369 |
33 .</web-app> |
370 |
|
371 |
*建立相關jsp和action |
372 |
|
373 |
|
374 |
|
375 |
Java代碼 |
376 |
1 .@Controller @Scope( "prototype" ) |
377 |
2 .public class EmployeeManagerAction extends ActionSupport { |
378 |
3 . @Resource EmployeeService employeeService; |
379 |
4 . private Employee employee; |
380 |
5 . |
381 |
6 . public String addUI(){ |
382 |
7 . //System.out.println( "user come" ); |
383 |
8 . return SUCCESS; |
384 |
9 . } |
385 |
10 . |
386 |
11 . public String add(){ |
387 |
12 . //System.out.println( "--------------" ); |
388 |
13 . boolean result=employeeService.save(employee); |
389 |
14 . //ActionContext.getContext().put( "genders" , Gender.values()); |
390 |
15 . if(result){ |
391 |
16 . ActionContext.getContext().put( "message" , "保存成功!" ); |
392 |
17 . }else{ |
393 |
18 . ActionContext.getContext().put( "message" , "保存失敗!" ); |
394 |
19 . } |
395 |
20 . return "message" ; |
396 |
21 . } |
397 |
22 . |
398 |
23 . public Employee getEmployee() { |
399 |
24 . return employee; |
400 |
25 . } |
401 |
26 . |
402 |
27 . public void setEmployee(Employee employee) { |
403 |
28 . this.employee = employee; |
404 |
29 . } |
405 |
30 .} |