?
第四步,商業邏輯層(數據庫開發工程師以及主程序員等)
?
本來還可以分成很多層的,筆者怕把新手給暈得一個不剩了,所以把N層先暫時合并到一層,其實這也是我們在一般的實際項目中經常使用的方式。
本例中采用的是hibernate來做數據庫訪問中間件,因此,請新同學按以下步驟進行:
1、設計一個實現IUser接口的類,用來做VO(PO)。
User.java的內容全部如下:
package com.easyjf.example.business.hibernate;
import com.easyjf.example.business.IUser;
public class User? implements IUser,java.io.Serializable {
?private static final long serialVersionUID=10083494895498l;
??? // Fields??
?private String cid;
?private String userName;
?private String password;
?private String email;
?private String tel;
?private String birthday;
?private String intro;
??? // Constructors
??? /** default constructor */
??? public User() {
??? }???
??? /** constructor with id */
??? public User(String cid) {
??????? this.cid = cid;
??? }
?public String getCid() {
??return cid;
?}
?public void setCid(String cid) {
??this.cid = cid;
?}
? …….//更多的垃圾getter及setter代碼省略
}
?
2、寫hibernate的配置文件
?
然后結合User.java及數據庫管理員建的表結構,寫hibernate映射配置文件User.hbm.xml,全部內容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"
<hibernate-mapping>
??? <class name="com.easyjf.example.business.hibernate.User" table="User">
??????? <id name="cid" type="string">
??????????? <column name="cid" length="16" />
???????????? <generator class="com.easyjf.example.business.hibernate.HibIdGenerator">
??????????? <param name="column">cid</param>
??????????? <param name="length">16</param>
??????????? </generator>
??????? </id>
??????? <property name="userName" type="string">
??????????? <column name="username" length="16" not-null="true" />
??????? </property>
??????? <property name="password" type="string">
??????????? <column name="password" length="16" not-null="true" />
??????? </property>
??????? <property name="email" type="string">
??????????? <column name="Email" length="50" />
??????? </property>
??????? <property name="tel" type="string">
??????????? <column name="tel" length="50" />
??????? </property>
???????? <property name="birthday" type="string">
??????????? <column name="birthday" length="23" />
??????? </property>???????
??????? <property name="intro" type="string">
??????????? <column name="intro" length="500" />
??????? </property>
??? </class>
</hibernate-mapping>
?
注意其中有關主鍵生成的部份配置如下:
<generator class="com.easyjf.example.business.hibernate.HibIdGenerator">
筆者這里使用的是自己的主鍵值生成器。你也可以使用hibernate自帶的默認主鍵生成器,怎么用就不說了。大家可以從本示例完整代碼下載中獲得HibIdGenerator.java類的源代碼,下載地址:
?
3、?寫IUserService的實現
這里由于使用的是hibernate,而Spring中提供了hibernate DAO,可以讓使用hibernate的朋友們更加簡單使用hibernate訪問數據庫,該示例中把Service及DAO合并在一起。全部UserDao的代碼如下:
package com.easyjf.example.business.hibernate;
import java.util.Collection;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.easyjf.example.business.IUser;
import com.easyjf.example.business.IUserService;
public class UserDao extends HibernateDaoSupport implements IUserService {
?public boolean del(IUser user) {
??boolean ret=true;
??try{
??this.getHibernateTemplate().delete(user);
??}
??catch(Exception e)
??{
???ret=false;
??}
??return ret;
?}?
?public List query(String scope,Collection paras) {
??return this.getHibernateTemplate().find("from User where "+scope,paras.toArray());??
?}
?public IUser login(String userName, String password, String ip) {
??IUser user=readByName(userName);
??if(user==null || user.getPassword()==null ||(!user.getPassword().equals(password)))//用戶不存在或者密碼不正確
??{
??return null;
??}??
??return user;?
?}
?public IUser newUser() {??
??return new User();
?}
?public IUser read(String cid) {??
??return (IUser)this.getHibernateTemplate().get(User.class,cid);
?}
?public IUser readByName(String userName) {??
??List list= this.getHibernateTemplate().find("from User where userName='"+userName+"'");
??if(list!=null)return (IUser)list.get(0);
??else
???return null;
?}
?public boolean save(IUser user) {??
??boolean ret=true;
??try{
??this.getHibernateTemplate().save(user);
??}
??catch(Exception e)
??{
???ret=false;
??}
??return ret;?
?}?
?public boolean update(IUser user) {
??boolean ret=true;
??try{
??this.getHibernateTemplate().update(user);
??}
??catch(Exception e)
??{
???e.printStackTrace();
???ret=false;
??}
??return ret;?
?}?
}
?
從代碼中我們看到,UserDao類繼承于Spring的HibernateDaoSupport類,目的在于方便通過hibernate操作數據庫,同時實現了系統設計師所設計的IUserService接口中的相關功能。
是對是錯,可以自己單元測試一下。
?
完了嗎?還沒有呢。下面是最后一步,也是本文所要介紹的重點,主角Spring正式登場,大家鼓掌。(咦,剛才在Struts即hibernate部分不是都提到Spring的嗎?哦,那只是客串!)
?
第五步:配置Spring
?
大家可以看到,上面我們所做的各部分都是各自為陣、分開的,特別是系統設計師的接口、Web工程師的MVC、數據庫開發及主程序員的商業邏輯層的實現,都相互沒有關聯、整合到一起。甚至就連數據庫工程師還不知道數據源從哪兒來,數據庫的訪問用戶名密碼等通通不知道。
那么這些東西怎么相互集成起來,并組合成一個完整的應用拿給用戶跑呢?這就是Spring扮演的角色之一了,Spring不是一直號稱自己提供了這樣那樣的支持嗎?下面我們來看看是怎么支持的。下面看看Spring的配置文件內容,本例中是applicationContext.xml,全部內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
<beans>?
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
?<property name="driverClassName">
??<value>org.gjt.mm.mysql.Driver</value>
?</property>
?<property name="url">
??<value>jdbc:mysql://127.0.0.1:3306/easyjf2</value>
?</property>
?<property name="username">
??<value>root</value>
?</property>
?<property name="password">
??<value>mysql</value>
?</property>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" singleton="true">
?<property name="dataSource">
??<ref local="dataSource" />
?</property>
?<property name="mappingResources">
??<list>
???<value>com/easyjf/example/business/hibernate/User.hbm.xml</value>
??</list>
?</property>
?<property name="hibernateProperties">
??<props>
???<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
???<prop key="hibernate.show_sql">true</prop>
??</props>
?</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
?<property name="sessionFactory">
??<ref local="mySessionFactory" />
?</property>
</bean>
<bean id="userService" class="com.easyjf.example.business.hibernate.UserDao">
?<property name="sessionFactory">
??<ref local="mySessionFactory" />
?</property>
</bean>
</beans>
?
其中dataSource部分是配置的數據源,如用戶名及密碼等,mySessionFactory部分主要針對hibernate的配置文件,userService部分針對hibernateDao的配置,事務的部分沒用到。
當然,要讓Spring跟Java Web應用集成,還需要配置一下web.xml文件,前面的web.xml已經有了,這里再拿出來大家觀摩一下:
?<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>/WEB-INF/applicationContext.xml</param-value>
?</context-param>?
?<listener>??<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
?</listener>?
?
看到了吧,通過配置,就可以把剛才我們的幾個部分集成到了一起。換一句話著,就是一旦有一天(真有這么一天嗎?小樣的,別插嘴!)我們需要改系統的某一部份(比如數據庫中間件不能用hibernate,而要用iBatis、EasyDBO或者是自己的“輪子”時),也只需要直接在配置文件中修改即可, 而系統的其它部分仍然保留不變。而且各部分的測試也相對獨立,因此可以測試到更多的東西(咦,這么說測試工作量不就大了嗎?是啊,但出錯的機會就少,維護成本就低了,笨)。這即是傳說中配置編程,也是面向接口編程乃至面向對象編程的精華之所在。
?
本示例中,我們主要涉及到的只是Spring最簡單的一個IOC容器功能。因此,其值不值得我們HC(喝彩、花癡―涼粉語),我想每人心中都會有一桿秤。對于Spring與struts、hibernate,畢竟是庸俗的黃金組合嘛,他們之間還有更多的合作招式可供大家選擇,這里就不一一介紹了,以后會介紹一些有關hibernate組合的!
?
OK,全文結束。還沒睡著或暈的同學請到EasyJF開源團隊官網下載本例源碼。筆者認識的程序員不少,不過還沒看見有誰僅僅靠“觀摩”、“理解”就能寫出程序的,還要多動手實踐,程序跑起來才是硬道理。
?
(
備注:由于筆者不想拐彎抹角浪費大家玩的時間,有些“表白”難免過于直接,還請不喜歡Spring或者過分喜歡Spring的同行多多見諒! 本文中的“我們”,僅指與筆者有著同樣成長經歷的80后人,對于文章提到的觀點,多數皆屬于筆者個人觀點,不代表任何人。
本文作者:
EasyJF開源團隊大峽 版權歸
EasyJF開源團隊所有,歡迎轉載,轉載請保留作者版權聲明,謝謝!)
?
附
?
posted on 2006-08-07 14:31
SIMONE 閱讀(286)
評論(0) 編輯 收藏 所屬分類:
JSP