用註解實現ssh的例子
阿新 • • 發佈:2018-11-03
在一個稍大的專案中,通常會有上百個元件,如果這些元件採用xml的bean定義來配置,顯然會增加配置檔案的體積,查詢以及維護起來也不太方便。個人也不喜歡配置那麼多的xml檔案。下面我們就利用java的註解實現ssh框架,註解相當於一種標記加了註解就等於打上了某種標記,沒加,則等於沒有某種標記,以後,javac編譯器,開發工具包和其他程式可以用反射來了解你的類以及各種元素上有何種標記,看你有什麼標記,就去幹相應的事,標記可以載入包,類,欄位,方法,方法的引數以及區域性變數上。關於註解在這裡不多說,網上大把資料。
先看看完整的工程目錄吧
1.為了使用註解我們需要配置web.xml檔案,在web-inf目錄下內容如下
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app version="2.5"
-
xmlns=
"http://java.sun.com/xml/ns/javaee"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee
-
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
-
-
-
<!-- 載入spring配置檔案 -->
-
<context-param>
-
<param-name>contextConfigLocation
</param-name>
-
<param-value>
-
classpath*:applicationContext*.xml
-
</param-value>
-
</context-param>
-
-
<listener>
-
<listener-class>
-
org.springframework.web.context.ContextLoaderListener
-
</listener-class>
-
</listener>
-
<!-- struts2 的配置 -->
-
<filter>
-
<filter-name>struts2
</filter-name>
-
<filter-class>
-
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
-
</filter-class>
-
<init-param>
-
<!-- //固定格式-->
-
<param-name>actionPackages
</param-name>
-
<param-value>com.ssh
</param-value>
-
</init-param>
-
</filter>
-
-
<filter-mapping>
-
<filter-name>struts2
</filter-name>
-
<url-pattern>/*
</url-pattern>
-
</filter-mapping>
-
<display-name>
</display-name>
-
<welcome-file-list>
-
<welcome-file>index.jsp
</welcome-file>
-
</welcome-file-list>
-
</web-app>
2.接下來看看spring和hibernate的配置檔案applicationContext.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:context=
"http://www.springframework.org/schema/context"
-
xmlns:aop=
"http://www.springframework.org/schema/aop"
-
xmlns:tx=
"http://www.springframework.org/schema/tx"
-
xsi:schemaLocation=
"
-
http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context.xsd
-
http://www.springframework.org/schema/aop
-
http://www.springframework.org/schema/aop/spring-aop.xsd
-
http://www.springframework.org/schema/tx
-
http://www.springframework.org/schema/tx/spring-tx.xsd">
-
-
<!-- 使用 annotation -->
-
<context:annotation-config />
-
<!-- 使用 annotation 自動註冊bean,並檢查@Controller, @Service, @Repository註解已被注入 -->
-
<context:component-scan base-package="com.ssh" />
-
-
<!-- 資料庫配置 -->
-
<bean id="dataSource"
-
class=
"org.apache.commons.dbcp.BasicDataSource">
-
<property name="driverClassName"
-
value=
"com.mysql.jdbc.Driver">
-
</property>
-
<property name="url"
-
value=
"jdbc:mysql://localhost:3306/myssh">
-
</property>
-
<property name="username" value="anan">
</property>
-
<property name="password" value="123456">
</property>
-
</bean>
-
-
<!-- sessionFactory -->
-
<bean id="sessionFactory"
-
class=
"org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
-
<property name="dataSource">
-
<ref bean="dataSource" />
-
</property>
-
<property name="hibernateProperties">
-
<props>
-
<prop key="hibernate.dialect">
-
org.hibernate.dialect.MySQLDialect
-
</prop>
-
<prop key="hibernate.show_sql">true
</prop>
-
<!-- 可以自動建立資料庫表(create),不建立(none) -->
-
<prop key="hibernate.hbm2ddl.auto">update
</prop>
-
</props>
-
</property>
-
<!-- 包掃描的方式載入註解類(推薦) -->
-
<property name="packagesToScan">
-
<list>
-
<value>com.ssh.*
</value>
-
</list>
-
</property>
-
</bean>
-
-
<!--JDBC事務管理器,根據你的情況使用不同的事務管理器,如果工程中有Hibernate,就用Hibernate的事務管理器 -->
-
<bean id="transactionManager"
-
class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
-
<property name="dataSource">
-
<ref local="dataSource" />
-
</property>
-
</bean>
-
-
<!-- 用註解來實現事務管理 -->
-
<tx:annotation-driven transaction-manager="transactionManager" />
-
</beans>
基本都有註釋了,這裡不多說了,需要注意的是如果是自己搞回去用千萬記得要改上面的資料庫配置。
3.接下來我們再看看struts的配置檔案struts.xml
-
<?xml version="1.0" encoding="UTF-8" ?>
-
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
-
<struts>
-
<!-- 開啟使用開發模式,詳細錯誤提示 -->
-
<constant name="struts.devMode" value="false" />
-
<!-- 將物件交給spring管理 -->
-
<constant name="struts.objectFactory" value="spring" />
-
<!-- 指定資源編碼型別 -->
-
<constant name="struts.i18n.encoding" value="UTF-8" />
-
<!-- 指定每次請求到達,重新載入資原始檔 -->
-
<constant name="struts.i18n.reload" value="false" />
-
<!-- 指定每次配置檔案更改後,自動重新載入 -->
-
<constant name="struts.configuration.xml.reload" value="false" />
-
<!-- 預設字尾名 -->
-
<!-- <constant name="struts.action.extension" value="do,action,jhtml,," /> -->
-
<!-- Struts Annotation -->
-
<!-- <constant name="actionPackages" value="com.test1"/> -->
-
</struts>
基本上註釋寫的很詳細了吧,我也不是很懂就那樣配置著先....
接下來我們就可以安心的寫java程式碼了,先建立如下圖所示的包和類
先看看實體類User.java
-
package com.ssh.user.model;
-
-
import javax.persistence.Column;
-
import javax.persistence.Entity;
-
import javax.persistence.GeneratedValue;
-
import javax.persistence.GenerationType;
-
import javax.persistence.Id;
-
import javax.persistence.Table;
-
-
/**
-
* 類名稱:User
-
* 類描述:使用者資訊實體
-
* 建立人:anan
-
* 建立時間:2012-12-21 下午10:55:19
-
* 修改人:anan
-
* 修改時間:2012-12-21 下午10:55:19
-
* 修改備註:
-
* @version
-
* */
-
@Entity
-
@Table(name =
"user")
-
public
class User {
-
/**
-
* 使用者id
-
*/
-
@Id
-
@Column(name =
"userId")
-
@GeneratedValue(strategy = GenerationType.IDENTITY)
-
private
int userId;
-
-
/**
-
* 使用者名稱
-
*/
-
@Column(name =
"userName", length =
50)
-
private String userName;
-
-
/**
-
* 使用者登入密碼
-
*/
-
@Column(name =
"passWord", length =
50)
-
private String passWord;
-
-
-
public int getUserId() {
-
return userId;
-
}
-
-
public void setUserId(int userId) {
-
this.userId = userId;
-
}
-
-
public String getUserName() {
-
return userName;
-
}
-
-
public void setUserName(String userName) {
-
this.userName = userName;
-
}
-
-
public String getPassWord() {
-
return passWord;
-
}
-
-
public void setPassWord(String passWord) {
-
this.passWord = passWord;
-
}
-
-
public User(int userId, String userName, String passWord) {
-
this.userId = userId;
-
this.userName = userName;
-
this.passWord = passWord;
-
}
-
-
public User(String userName, String passWord) {
-
this.userName = userName;
-
this.passWord = passWord;
-
}
-
-
public User(int userId) {
-
this.userId = userId;
-
}
-
-
public User() {
-
-
}
-
<