spring-mybatis項目練習 - 通訊錄系統,ssm配置文件部分
好久好久沒來這裏了,這段時間一直在實習及忙各種麻煩事,暮然回首,覺得有點失去了當初靜靜學習的感覺。亡羊補牢,還是來這裏貼上一點東西,找找學習的感覺。回想當初熱血沸騰地開博,後來竟驚覺自己不是一個可以靜下心來寫博的人。感覺自己都不是當初那個悶騷的猿了T_T
這段時間的實習,主要是幹web開發,負責給公司的項目寫一點點功能。公司的項目都是ssm框架的maven的項目。可憐我初來時連ssm其中一個字母都不懂T_T,就spring是聽過的。所以能夠做的事情也就非常地有限。在空余的時間裏,一路在看別人的代碼,還有mybatis,spring的書籍。在工作學習的過程中,我覺得要想成長,無論如何都要自己由頭到尾地寫一個小項目。於是,便有了寫這個東西的想法,無奈朝九晚七時間有限。真正開始動工,是在十幾天前,過程可謂相當坎坷,趁國慶長假,躲在家裏潛心修煉,終於略有小成,瞬間有種技術宅的感覺了。
在開始之前,還是想多啰嗦幾句這段時間的感悟(反正沒人看),做一個網頁開發的java攻城獅,有幾點是必需堅持的:
1.永遠不要忽略基礎,java基礎的東西雖然不多,但深入學習了解卻是非常重要的,尤其像io,異常處理,多線程這樣的知識。
2.在工作中,永遠不要停留在應用和解決問題的層面上,看到很多前輩的博客個性簽名都會寫類似“不想當碼農的程序猿”這樣的話,就是這個道理。我始終覺得,對一個java攻城獅而言,對一些框架及java技術,例如反射,往深處探究其原理及實現方式,是一件相當cool的事。所以在接下的blog裏,我會逐漸逐漸地把自己總結(東拉西扯)的一些東西發出來,主要是關於spring,mybatis的一些簡單的東西。
3.學習。
4.學習。。
5.學習。。。
就這些,接下來入正題。
----------------------------------------一直不理解為什麽有人會在分割線上寫“我是華麗的分割線”這麽蠢的話-----------------------------------------
這個項目繼承自己之前寫的一個練習xml操作的項目,後來變成一個ajax練習的項目,所以前端的處理會相當極端,一個html處理所有的請求。再後來,變成mybatis的練習項目,這個項目我可能將來也會貼出來,不過意義不大,對於單純用mybatis而言,用spring對其進行管理會使代碼優雅得多,就像是直接調用dao一樣便捷。這個項目的最後版,便是這個ssm框架的版本。標題是spring-mybatis項目,主要是因為spring-mvc只用了異步傳輸的功能,model都沒有出現,所以項目是以spring及mybatis為主。考慮到自己的攻城獅生涯還很長,我這裏用到的框架都是接近最新版的。為了對spring裏面各種包的作用有更深入的認識,這裏不用maven構建項目,而是自己把需要的包一個一個搬過來。
項目地址:https://pan.baidu.com/s/1bpARdoV ,目錄下book_SSM.zip便是。
項目名:通訊錄系統(book_SSM)
相關框架版本:spring-4.3.10(spring-mvc也在裏面),mybatis-3.4.4,mybatis-spring-1.3.0
ide:eclipse-jee-mars
jdk:1.7
server:tomcat v8.0(7.0也可以)
功能:用戶註冊,登錄,登出,聯系人增刪查改,聯系人批量導出為xml文件,也可以將xml文件導入到系統。
項目結構目錄:
控制層:controller,業務層:service,數據庫操作層:dao,dto及vo是pojo類
需要的包:
首先放ssm三大框架的配置文件,個人覺得這才是最有學習價值的地方。寫的時候最費心機。
spring:applicationContext.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <!-- 支持註解 --> <context:annotation-config/> <!-- 自動掃描的包 --> <context:component-scan base-package="com.hg.book" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- 導入數據源 --> <!-- 方式一,不可用??? --> <!-- <context:property-placeholder location="classpath:jdbc.properties" /> --> <!-- 方式二 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <array> <!-- 可以放多個properties --> <value>classpath:jdbc.properties</value> </array> </property> </bean> <!-- 集成c3p0連接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> </bean> <!-- mybatis的sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis_config.xml"/> </bean> <!-- sqlSessionTemplate --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 事務管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用註解管理事務 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 自動掃描創建mapper --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.hg.book.dao"/> <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> </bean> </beans>
mybatis:mybatis_config.xml,這裏相當簡單,因為很多內容,例如sqlsessionfactory,mapper,數據源都交由spring管理了。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 使全局的映射器啟用或禁用緩存 --> <setting name="cacheEnabled" value="true"/> <!-- 允許JDBC支持生成的鍵。需要適合的驅動。如果設置為true,則這個設置強制生成的鍵被使用 --> <setting name="useGeneratedKeys" value="true"/> <!-- 配置默認的執行器。SIMPLE執行器沒有什麽特別之處。REUSE執行器重用預處理語句。BATCH執行器重用語句和批量更新。 --> <setting name="defaultExecutorType" value="REUSE"/> <!-- 全局啟用或禁用延遲加載。當禁用時,所有關聯對象都會即時加載 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 設置超時時間,它決定驅動等待一個數據庫響應的時間 --> <setting name="defaultStatementTimeout" value="25000"/> </settings> <!-- 定義別名 --> <typeAliases> <typeAlias alias="member" type="com.hg.book.dto.Memberdto"/> <typeAlias alias="book" type="com.hg.book.dto.Bookdto"/> <typeAlias alias="update" type="com.hg.book.dto.Updatedto"/> </typeAliases> <!-- 映射器 --> <mappers> <mapper resource="com/hg/book/dao/Bookmapper.xml"/> </mappers> </configuration>
spring mvc:springmvc-servlet.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "> <!-- 啟用default-servlet --> <mvc:default-servlet-handler/> <!-- 使視圖攔截器能夠工作 --> <mvc:annotation-driven> <!-- 處理返回String編碼 --> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="utf-8"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 自動掃描包 --> <context:component-scan base-package="com.hg.book.controller"/> <context:annotation-config /> <!-- spring視圖攔截器 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- 《加入編碼攔截器後已失效》處理返回值為String類型的結果;防止返回String時轉換為json自動添加雙引號 --> <!-- <ref bean="stringHttpMessageConverter" /> --> <!-- 讀取到@ResponseBody的時候觸發 --> <ref bean="mappingJacksonHttpMessageConverter" /> </list> </property> </bean> <!-- <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" index="0"/> </bean> --> <!-- json轉化器,可以將結果轉化 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </beans>
最後是web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <!-- spring配置文件路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 編碼處理 --> <filter> <filter-name>encodingfiller</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingfiller</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring mvc的dispatcherservlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:springController.xml </param-value> </init-param> --> <load-on-startup>2</load-on-startup> </servlet> <!-- defaultservlet使springmvc不攔截某些頁面 --> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>/view.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> <!-- <url-pattern>*.do</url-pattern> --> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>view.html</welcome-file> </welcome-file-list> <error-page> <error-code>404</error-code> <location>/resources/404.jpg</location> </error-page> </web-app>
至此結束,配置寫的時候遇到問題都是翻來覆去的問度娘,寫完後覺得其實spring的設計者已經將種種情況都考慮過了,而初級開發者用起來卻問題多多,主要是因為對框架一知半解,同時框架的知識又非常多。所以系統地學習框架的知識是非常有必要的。要熟悉框架,學習和實戰都是必需的,不要停留在簡單的應用和解決問題的層面上。
項目的其余部分,可能以後會貼出來。
spring-mybatis項目練習 - 通訊錄系統,ssm配置文件部分