1. 工具準備
i)開發平臺 Eclipse Java EE IDE Mars Release (4.5.0) + JDK 1.7 +
ii)項目管理工具 maven 3.3.3。 http://maven.apache.org/download.cgi
有了Maven,Spring、mybatis這些jar包都不用我們手動下載添加到項目的build path了,如果不熟悉的話可以參考http://blog.csdn.net/shymi1991/article/details/47255407
iii)Tomcat v7.0 http://tomcat.apache.org/
iv)數據庫 MySQL 5.6 http://dev.mysql.com/downloads/mysql/
v)前端JS框架 AngularJS 1.3.9 https://angularjs.org/
vi)前端頁面框架 Bootstrap 3 http://bootstrapuikit.com/
2. 配置eclipse,新建maven 項目
Tomcat設置環境變量,然後在eclipse的server中添加進來。
在Eclipse中安裝m2e插件來集成maven。完成後重啓Eclipse,File-->new-->Maven Project,選擇maven-archetype-webapp。
輸入包名Group ID:com.sommer.sample,項目名稱Artifact ID: webproject。
新建號項目後index.jsp頁面頂端會報錯:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path。
是因爲該JavaWeb工程類中沒有添加Tomcat運行時相關類導致,在項目上右鍵-->Properties-->Java Build Path->Libraries--> Add Libray...-->Server Runtime -->Tomcat Server,完成後錯誤會自動消失。
這時候構建的目錄是不完整的,我們要添加src\main\java源文件夾及需要的包名(可以直接在navigator視圖下添加文件夾),具體項目目錄結構如下:
3. 數據庫表準備。在數據庫中建立3個表:users, posts, comments
create database weibo;
create table posts(
id INT primary key auto_increment ,
date DATE not null,
author_name VARCHAR(20) not null,
likes SMALLINT,
content TEXT);
create table comments(
id INT primary key auto_increment ,
c_date DATE not null,
c_author_name VARCHAR(20) not null,
c_content TEXT,
post_id INT not null);
create table users(
id INT primary key auto_increment,
name VARCHAR(20) not null,
password VARCHAR(20) not null);
接下來一節我們就可以正式開始寫代碼了。