1. 程式人生 > >用註解實現ssh的例子

用註解實現ssh的例子

在一個稍大的專案中,通常會有上百個元件,如果這些元件採用xml的bean定義來配置,顯然會增加配置檔案的體積,查詢以及維護起來也不太方便。個人也不喜歡配置那麼多的xml檔案。下面我們就利用java的註解實現ssh框架,註解相當於一種標記加了註解就等於打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具包和其他程式可以用反射來了解你的類以及各種元素上有何種標記,看你有什麼標記,就去幹相應的事,標記可以載入包,類,欄位,方法,方法的引數以及區域性變數上。關於註解在這裡不多說,網上大把資料。

 

先看看完整的工程目錄吧

 

 

1.為了使用註解我們需要配置web.xml檔案,在web-inf目錄下內容如下


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5"
  3. xmlns= "http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
  7. <!-- 載入spring配置檔案 -->
  8. <context-param>
  9. <param-name>contextConfigLocation </param-name>
  10. <param-value>
  11. classpath*:applicationContext*.xml
  12. </param-value>
  13. </context-param>
  14. <listener>
  15. <listener-class>
  16. org.springframework.web.context.ContextLoaderListener
  17. </listener-class>
  18. </listener>
  19. <!-- struts2 的配置 -->
  20. <filter>
  21. <filter-name>struts2 </filter-name>
  22. <filter-class>
  23. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  24. </filter-class>
  25. <init-param>
  26. <!-- //固定格式-->
  27. <param-name>actionPackages </param-name>
  28. <param-value>com.ssh </param-value>
  29. </init-param>
  30. </filter>
  31. <filter-mapping>
  32. <filter-name>struts2 </filter-name>
  33. <url-pattern>/* </url-pattern>
  34. </filter-mapping>
  35. <display-name> </display-name>
  36. <welcome-file-list>
  37. <welcome-file>index.jsp </welcome-file>
  38. </welcome-file-list>
  39. </web-app>


2.接下來看看spring和hibernate的配置檔案applicationContext.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. xmlns:aop= "http://www.springframework.org/schema/aop"
  6. xmlns:tx= "http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation= "
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16. <!-- 使用 annotation -->
  17. <context:annotation-config />
  18. <!-- 使用 annotation 自動註冊bean,並檢查@Controller, @Service, @Repository註解已被注入 -->
  19. <context:component-scan base-package="com.ssh" />
  20. <!-- 資料庫配置 -->
  21. <bean id="dataSource"
  22. class= "org.apache.commons.dbcp.BasicDataSource">
  23. <property name="driverClassName"
  24. value= "com.mysql.jdbc.Driver">
  25. </property>
  26. <property name="url"
  27. value= "jdbc:mysql://localhost:3306/myssh">
  28. </property>
  29. <property name="username" value="anan"> </property>
  30. <property name="password" value="123456"> </property>
  31. </bean>
  32. <!-- sessionFactory -->
  33. <bean id="sessionFactory"
  34. class= "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  35. <property name="dataSource">
  36. <ref bean="dataSource" />
  37. </property>
  38. <property name="hibernateProperties">
  39. <props>
  40. <prop key="hibernate.dialect">
  41. org.hibernate.dialect.MySQLDialect
  42. </prop>
  43. <prop key="hibernate.show_sql">true </prop>
  44. <!-- 可以自動建立資料庫表(create),不建立(none) -->
  45. <prop key="hibernate.hbm2ddl.auto">update </prop>
  46. </props>
  47. </property>
  48. <!-- 包掃描的方式載入註解類(推薦) -->
  49. <property name="packagesToScan">
  50. <list>
  51. <value>com.ssh.* </value>
  52. </list>
  53. </property>
  54. </bean>
  55. <!--JDBC事務管理器,根據你的情況使用不同的事務管理器,如果工程中有Hibernate,就用Hibernate的事務管理器 -->
  56. <bean id="transactionManager"
  57. class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
  58. <property name="dataSource">
  59. <ref local="dataSource" />
  60. </property>
  61. </bean>
  62. <!-- 用註解來實現事務管理 -->
  63. <tx:annotation-driven transaction-manager="transactionManager" />
  64. </beans>


基本都有註釋了,這裡不多說了,需要注意的是如果是自己搞回去用千萬記得要改上面的資料庫配置。

 

3.接下來我們再看看struts的配置檔案struts.xml


  
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
  3. <struts>
  4. <!-- 開啟使用開發模式,詳細錯誤提示 -->
  5. <constant name="struts.devMode" value="false" />
  6. <!-- 將物件交給spring管理 -->
  7. <constant name="struts.objectFactory" value="spring" />
  8. <!-- 指定資源編碼型別 -->
  9. <constant name="struts.i18n.encoding" value="UTF-8" />
  10. <!-- 指定每次請求到達,重新載入資原始檔 -->
  11. <constant name="struts.i18n.reload" value="false" />
  12. <!-- 指定每次配置檔案更改後,自動重新載入 -->
  13. <constant name="struts.configuration.xml.reload" value="false" />
  14. <!-- 預設字尾名 -->
  15. <!-- <constant name="struts.action.extension" value="do,action,jhtml,," /> -->
  16. <!-- Struts Annotation -->
  17. <!-- <constant name="actionPackages" value="com.test1"/> -->
  18. </struts>

基本上註釋寫的很詳細了吧,我也不是很懂就那樣配置著先....

接下來我們就可以安心的寫java程式碼了,先建立如下圖所示的包和類

 

先看看實體類User.java


  
  1. package com.ssh.user.model;
  2. import javax.persistence.Column;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. /**
  9. * 類名稱:User
  10. * 類描述:使用者資訊實體
  11. * 建立人:anan
  12. * 建立時間:2012-12-21 下午10:55:19
  13. * 修改人:anan
  14. * 修改時間:2012-12-21 下午10:55:19
  15. * 修改備註:
  16. * @version
  17. * */
  18. @Entity
  19. @Table(name = "user")
  20. public class User {
  21. /**
  22. * 使用者id
  23. */
  24. @Id
  25. @Column(name = "userId")
  26. @GeneratedValue(strategy = GenerationType.IDENTITY)
  27. private int userId;
  28. /**
  29. * 使用者名稱
  30. */
  31. @Column(name = "userName", length = 50)
  32. private String userName;
  33. /**
  34. * 使用者登入密碼
  35. */
  36. @Column(name = "passWord", length = 50)
  37. private String passWord;
  38. public int getUserId() {
  39. return userId;
  40. }
  41. public void setUserId(int userId) {
  42. this.userId = userId;
  43. }
  44. public String getUserName() {
  45. return userName;
  46. }
  47. public void setUserName(String userName) {
  48. this.userName = userName;
  49. }
  50. public String getPassWord() {
  51. return passWord;
  52. }
  53. public void setPassWord(String passWord) {
  54. this.passWord = passWord;
  55. }
  56. public User(int userId, String userName, String passWord) {
  57. this.userId = userId;
  58. this.userName = userName;
  59. this.passWord = passWord;
  60. }
  61. public User(String userName, String passWord) {
  62. this.userName = userName;
  63. this.passWord = passWord;
  64. }
  65. public User(int userId) {
  66. this.userId = userId;
  67. }
  68. public User() {
  69. }
  70. <