小談:
帖主妥妥的一名"中"白了哈哈哈。軟工的大三狗了,也即將找工作,懷着絲絲忐忑接受社會的安排。這是第一次寫博客(/汗顏),其實之前在學習探索過程中,走了不少彎路,爬過不少坑。真的挺感謝一路上的前輩們的博客也好,隨筆也好,哪怕是評論,或多或少解決了一些問題。我感覺學技術的過程中,記錄下自己解決問題的過程、經驗,如果可以的話能分享,其實也挺好。希望能從“中白”變“大白”,再到佬行列哈哈。
簡介:
這次主要是基於ssm框架和mysql在idea上寫的,restful風格使用起來url感覺比傳統的更簡潔點。就沒有寫前台了,不過在代碼的注釋里包含了ajax的從前台獲取值的說明,也(瞎)寫了一些jsp的名稱。主要還是將ssm的框架搭建完整,記錄當時在配置時出現一些問題及解決。寫個bbs的小實例。(restful戳這 https://blog.csdn.net/qq_21383435/article/details/80032375 ,完整的代碼在github:https://github.com/isMaxaaa/bbs)
步驟:
1.數據庫設計:
1 CREATE TABLE user ( 2 user_id int(11) NOT NULL AUTO_INCREMENT, 3 user_name varchar(50) DEFAULT NULL, 4 email varchar(50) DEFAULT NULL, 5 replys int(11) DEFAULT NULL, 6 topics int(11) DEFAULT NULL, 7 create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 8 praises int(11) DEFAULT NULL, 9 PRIMARY KEY (user_id) 10 ) 11 12 CREATE TABLE post ( 13 topic_id int(11) NOT NULL AUTO_INCREMENT, 14 user_id int(11) NOT NULL, 15 title varchar(100) NOT NULL, 16 content` varchar(20140) NOT NULL, 17 create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 18 lastset_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON 19 UPDATE CURRENT_TIMESTAMP, 20 PRIMARY KEY (topic_id), 21 FOREIGN KEY (use_id) REFERENCES user (user_id) 22 ) 23 24 CREATE TABLE comment ( 25 comment_id int(11) NOT NULL AUTO_INCREMENT, 26 topic_id int(11) NOT NULL, 27 user_id int(11) NOT NULL, 28 comment_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 29 content varchar(200) NOT NULL, 30 PRIMARY KEY (comment_id), 31 FOREIGN KEY (use_id) REFERENCES user (user_id), 32 FOREIGN KEY (topic_id) REFERENCES post(topic_id) 33 ) 34 35 CREATE TABLE reply ( 36 reply_id int(11) NOT NULL AUTO_INCREMENT, 37 comment_id int(11) NOT NULL, 38 reply_user int(11) NOT NULL, 39 reply_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 40 content varchar(200) NOT NULL, 41 PRIMARY KEY (reply_id), 42 FOREIGN KEY (comment_id) REFERENCES comment (comment_id) 43 FOREIGN KEY (reply_user) REFERENCES user (user_id) 44 ) 45 46 CREATE TABLE praise ( 47 id int(11) NOT NULL AUTO_INCREMENT, 48 topic_id int(11) NOT NULL, 49 user_id int(11) NOT NULL, 50 PRIMARY KEY (`id`), 51 FOREIGN KEY (use_id) REFERENCES user (user_id) 52 FOREIGN KEY (topic_id) REFERENCES post (topic_id) 53 )
設計時在reply(回復表)和comment(評論表)的邏輯花不少時間,考慮的是前台每個帖子詳情下面,用戶可以對帖主評論,而在每個評論下面其他用戶可以對該評論回復,就像貼吧的樓主一樣。一般評論或回復都可以刪除,在設計時產生了不少外碼依賴(可能設計的有點水/汗顏),如果刪除帖子或者評論,建立聯級刪除后原來關聯的評論回復記錄都沒有,但這並不是我們想要的,所以就加了外碼的SET NULL依賴,只是把外碼設置成null就行。(mysql解決外碼依賴https://www.cnblogs.com/xiohao/archive/2013/06/28/3160265.html)
1 comment : 2 alter table comment add constraint comment_cons 3 foreign key(topic_id) 4 references post(topic_id) 5 on delete set null; 6 7 reply: 8 alter table reply add constraint reply_cons 9 foreign key(comment_id) 10 references comment(comment_id) 11 on delete set null; 12 13 praise: 14 alter table praise add contraint praise_cons 15 foreign key(topic_id) 16 references post(topic_id) 17 on delete set null;
2.resful設計:

3.idea創建項目:
創建完整后:

具體的項目的創建過程就不展示了(/抱拳)
這個是使用maven創建的,maven最好的就是可以直接在pox.xml寫依賴,可以自動下載所需的依賴包,而不用自己手動導入jar包。不過注意要改一下maven的setting.xml的下載源,添加下載倉庫,這樣下載就很快咯。
4.配置文件的說明:

在main下新建一個resources資源,這里面主要是放spring的mvc的相關核心配置文件的。mapper是dao層的的映射,里面主要是每個dao相關的sql語句。spring里面放的是dao層、service、controller層的spring配置,如置數據庫連接池,掃描包的注解類型,servlet的配置等等。在源碼里有相關配置的簡單的注釋。
5.出現的問題及解決:
1.在配置jdbc.properties時,什么driver、url、username、password都沒問題,但在spring-dao.xml中配置數據庫的連接池,使用數據庫的相關參數也就是jdbc.proerties,最后測試時總體是連接超時拒絕。把相關的參數直接寫在value值時,又沒問題。。。最后整了半天,總算查到了原因:spring4.0 在引入外部property文件需要使用下面的格式。3.0可以直接使用第二種。
<!-- 引入jdbc配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!--要是有多個配置文件,只需在這里繼續添加即可 --> <value>classpath:properties/*.properties</value> </list> </property> </bean>
<context:property-placeholder location="classpath:jdbc.properties" />
2.還有一個是提示,mybatis.xml的什么哪個setting出錯,最后查到只要將settings的那個設置刪掉就行。
3.在pom.xml中添加資源文件路徑配置,沒添加在加載配置文件可能會提示路徑找不到。
<resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>true</filtering> </resource> </resources>
