照常理來說~這幾天討厭鬼應該是要發文的

可是~壓母溝,因為暗黑3的出現~

所以我忘記惹~所以現在趕快補一下

討厭鬼依稀記得(遠目)上次教的應該是SpringMVC的基本架構

不過還沒有撘配資料庫的使用

所以這次就發SpringMVC+Hibernate配置

所以~還沒有看的,快點去看Spring MVC 配置教學

其實跟ssh配置時都差不多

首先是用Hibernate tools產生entity

不知道的可以去看這篇Struts2+Spring+Hibernate架構配置教學(Hibernate)上

再來就是與資料庫連線可以看這篇Struts2+Spring+Hibernate架構配置教學(Hibernate+JNDI)下

 

寫到這邊,大家可能會覺得討厭鬼很懶

其實我被誤會了~因為我是個程式設計師

在寫code時會把同樣寫很多遍的code抽出來

要用到時再去呼叫,這樣就可以少寫很多code

同理~在這邊我也是用了同樣的方法

這樣就可以達到衝人氣Reuse的概念

很多人都說態度決定您的高度

但討厭鬼覺得對程式設計師來說觀念決定您的價值

抱歉有點離題了~

 

到這邊配置完~會有人發現~

討厭鬼都還沒有配置spring的設定檔

就算看完了hibernate上下的文章後

也只配置了一個data-layer的spring設定檔

難道都不用設定service的bean嗎?

沒錯~不用設定,不過還是要加入下列簡單的設定

<context:annotation-config/>
<context:component-scan base-package="xxxx.xxxx.xxxx" />

xxxx.xxxx.xxxx代表的是你serviceImpl的package

 

討厭鬼的範例

<?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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <context:annotation-config/>
    <context:component-scan base-package="com.nerdy.service.impl" />

</beans>

 

配置到這~其實你的spring要用一個還是分成兩個都無所謂

不過我還是建議還是要有一個service-layer的spring設定檔

因為還是會設定到transation manager的

 

接下來將ServiceImpl的class檔加入@Service

 

討厭鬼的範例

package com.nerdy.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.nerdy.service.IndexService;

@Service
public class IndexServiceImpl implements IndexService{

}

 

這樣的話在controller中就可以使用@Resource 取得service了

再來就是使用方法了~請參考一下Spring+Hibernate用法教學

ok~完工

 

arrow
arrow
    全站熱搜

    討厭鬼 發表在 痞客邦 留言(0) 人氣()