?
第四步,商業(yè)邏輯層(數(shù)據(jù)庫(kù)開(kāi)發(fā)工程師以及主程序員等)
?
本來(lái)還可以分成很多層的,筆者怕把新手給暈得一個(gè)不剩了,所以把N層先暫時(shí)合并到一層,其實(shí)這也是我們?cè)谝话愕膶?shí)際項(xiàng)目中經(jīng)常使用的方式。
本例中采用的是hibernate來(lái)做數(shù)據(jù)庫(kù)訪問(wèn)中間件,因此,請(qǐng)新同學(xué)按以下步驟進(jìn)行:
1、設(shè)計(jì)一個(gè)實(shí)現(xiàn)IUser接口的類,用來(lái)做VO(PO)。
User.java的內(nèi)容全部如下:
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、寫(xiě)hibernate的配置文件
?
然后結(jié)合User.java及數(shù)據(jù)庫(kù)管理員建的表結(jié)構(gòu),寫(xiě)hibernate映射配置文件User.hbm.xml,全部?jī)?nèi)容如下:
<?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>
?
注意其中有關(guān)主鍵生成的部份配置如下:
<generator class="com.easyjf.example.business.hibernate.HibIdGenerator">
筆者這里使用的是自己的主鍵值生成器。你也可以使用hibernate自帶的默認(rèn)主鍵生成器,怎么用就不說(shuō)了。大家可以從本示例完整代碼下載中獲得HibIdGenerator.java類的源代碼,下載地址:
?
3、?寫(xiě)IUserService的實(shí)現(xiàn)
這里由于使用的是hibernate,而Spring中提供了hibernate DAO,可以讓使用hibernate的朋友們更加簡(jiǎn)單使用hibernate訪問(wèn)數(shù)據(jù)庫(kù),該示例中把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類,目的在于方便通過(guò)hibernate操作數(shù)據(jù)庫(kù),同時(shí)實(shí)現(xiàn)了系統(tǒng)設(shè)計(jì)師所設(shè)計(jì)的IUserService接口中的相關(guān)功能。
是對(duì)是錯(cuò),可以自己?jiǎn)卧獪y(cè)試一下。
?
完了嗎?還沒(méi)有呢。下面是最后一步,也是本文所要介紹的重點(diǎn),主角Spring正式登場(chǎng),大家鼓掌。(咦,剛才在Struts即hibernate部分不是都提到Spring的嗎?哦,那只是客串!)
?
第五步:配置Spring
?
大家可以看到,上面我們所做的各部分都是各自為陣、分開(kāi)的,特別是系統(tǒng)設(shè)計(jì)師的接口、Web工程師的MVC、數(shù)據(jù)庫(kù)開(kāi)發(fā)及主程序員的商業(yè)邏輯層的實(shí)現(xiàn),都相互沒(méi)有關(guān)聯(lián)、整合到一起。甚至就連數(shù)據(jù)庫(kù)工程師還不知道數(shù)據(jù)源從哪兒來(lái),數(shù)據(jù)庫(kù)的訪問(wèn)用戶名密碼等通通不知道。
那么這些東西怎么相互集成起來(lái),并組合成一個(gè)完整的應(yīng)用拿給用戶跑呢?這就是Spring扮演的角色之一了,Spring不是一直號(hào)稱自己提供了這樣那樣的支持嗎?下面我們來(lái)看看是怎么支持的。下面看看Spring的配置文件內(nèi)容,本例中是applicationContext.xml,全部?jī)?nèi)容如下:
<?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部分是配置的數(shù)據(jù)源,如用戶名及密碼等,mySessionFactory部分主要針對(duì)hibernate的配置文件,userService部分針對(duì)hibernateDao的配置,事務(wù)的部分沒(méi)用到。
當(dāng)然,要讓Spring跟Java Web應(yīng)用集成,還需要配置一下web.xml文件,前面的web.xml已經(jīng)有了,這里再拿出來(lái)大家觀摩一下:
?<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>?
?
看到了吧,通過(guò)配置,就可以把剛才我們的幾個(gè)部分集成到了一起。換一句話著,就是一旦有一天(真有這么一天嗎?小樣的,別插嘴!)我們需要改系統(tǒng)的某一部份(比如數(shù)據(jù)庫(kù)中間件不能用hibernate,而要用iBatis、EasyDBO或者是自己的“輪子”時(shí)),也只需要直接在配置文件中修改即可, 而系統(tǒng)的其它部分仍然保留不變。而且各部分的測(cè)試也相對(duì)獨(dú)立,因此可以測(cè)試到更多的東西(咦,這么說(shuō)測(cè)試工作量不就大了嗎?是啊,但出錯(cuò)的機(jī)會(huì)就少,維護(hù)成本就低了,笨)。這即是傳說(shuō)中配置編程,也是面向接口編程乃至面向?qū)ο缶幊痰木A之所在。
?
本示例中,我們主要涉及到的只是Spring最簡(jiǎn)單的一個(gè)IOC容器功能。因此,其值不值得我們HC(喝彩、花癡―涼粉語(yǔ)),我想每人心中都會(huì)有一桿秤。對(duì)于Spring與struts、hibernate,畢竟是庸俗的黃金組合嘛,他們之間還有更多的合作招式可供大家選擇,這里就不一一介紹了,以后會(huì)介紹一些有關(guān)hibernate組合的!
?
OK,全文結(jié)束。還沒(méi)睡著或暈的同學(xué)請(qǐng)到EasyJF開(kāi)源團(tuán)隊(duì)官網(wǎng)下載本例源碼。筆者認(rèn)識(shí)的程序員不少,不過(guò)還沒(méi)看見(jiàn)有誰(shuí)僅僅靠“觀摩”、“理解”就能寫(xiě)出程序的,還要多動(dòng)手實(shí)踐,程序跑起來(lái)才是硬道理。
?
(
備注:由于筆者不想拐彎抹角浪費(fèi)大家玩的時(shí)間,有些“表白”難免過(guò)于直接,還請(qǐng)不喜歡Spring或者過(guò)分喜歡Spring的同行多多見(jiàn)諒! 本文中的“我們”,僅指與筆者有著同樣成長(zhǎng)經(jīng)歷的80后人,對(duì)于文章提到的觀點(diǎn),多數(shù)皆屬于筆者個(gè)人觀點(diǎn),不代表任何人。
本文作者:
EasyJF開(kāi)源團(tuán)隊(duì)大峽 版權(quán)歸
EasyJF開(kāi)源團(tuán)隊(duì)所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)保留作者版權(quán)聲明,謝謝!)
?
附
?
posted on 2006-08-07 14:31
SIMONE 閱讀(286)
評(píng)論(0) 編輯 收藏 所屬分類:
JSP