2010年12月28日 星期二

Spring MVC Demo

最近研究了一下 Spring MVC 的架構

大該了解基本的用法

在此分享 Spring MVC Demo 程式

DemoWeb 架構以
Spring MVC + Hibernate + Annotation
為主架構, 是目前業界還滿流行的架構之一, 如果前端想使用JQuery也是沒問題
的, 至於後端 DB 的話因為是使用 Hibernate 做為 ORM, 所以基本上並不會有
Databases Dependency 的問題, 只要找到相關 DB 的 JDBC 就可以切換了

請點選下載 DemoWeb 程式
或至 DemoWeb (github) 下載程式

開發工具為 SpringSource Tool Suite

DemoWeb 所用的 Databases 目前有二種, 可任選其中一種作測試
1. 設定 SQLite (DemoWeb程式預設)
  a. 將 DemoWeb 下載解壓後, 資料夾下有一個 SQLite DB 檔 demowebdb.db3
  b. 將 demowebdb.db3 檔放至 C:\DemoWebDB\ 資料夾下就可以了

2. 設定 MySQL
  a. 將 DemoWeb 下載解壓後, 資料夾下有一個 demowebdb_schema.sql
  b. 執行此 SQL 檔後, 將會建立 demowebdb
  c. 開啟 WEB-INF\jdbc.properties 檔, 更改 MySQL 登入的 UserName 跟
      Password 參數
# MySQL Setting
datasource.mysql.driverClassName=com.mysql.jdbc.Driver
datasource.mysql.dialect=org.hibernate.dialect.MySQLDialect
datasource.mysql.demowebdb.url=jdbc:mysql://localhost:3306/demowebdb?useUnicode=true&characterEncoding=UTF-8
datasource.mysql.demowebdb.username=xxxxx
datasource.mysql.demowebdb.password=xxxxx
...
   d. 開啟 WEB-INF\spring\appServlet\servlet-context.xml 檔, 更改
       DataSource 的參數, 先找到以下區段
...
<beans:bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
  <beans:property name="driverClass" value="${datasource.sqlite.driverClassName}"/>
  <beans:property name="jdbcUrl" value="${datasource.sqlite.demowebdb.url}"/>
  <beans:property name="user" value="${datasource.sqlite.demowebdb.username}"/>
  <beans:property name="password" value="${datasource.sqlite.demowebdb.password}"/>
</beans:bean>
...
      修改成
...
<beans:bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
  <beans:property name="driverClass" value="${datasource.mysql.driverClassName}"/>
  <beans:property name="jdbcUrl" value="${datasource.mysql.demowebdb.url}"/>
  <beans:property name="user" value="${datasource.mysql.demowebdb.username}"/>
  <beans:property name="password" value="${datasource.mysql.demowebdb.password}"/>
</beans:bean>
...

    再來就是執行程式作測試啦!!!!

沒有留言:

張貼留言