1.1 爲何要用Maven搭建項目? Maven能夠對項目依賴的jar包進行管理,須要的jar包只須要到Maven倉庫裏面去拿到版本信息複製到pom.xml文件便可。同時,它也能對項目進行編譯、測試、打包等功能。 1.2 新建一個Maven工程(這裏用的是Eclipse): 1.2.1 點擊File -> New Project -> Maven Project,點擊Next; 1.2.2 彈出一個界面,這裏直接點擊Next,而後選擇webapp那一欄,點擊Next; 1.2.3 此時,將進入另外一個界面,會叫你輸入一些有關項目的信息。主要的分別是: Group Id: 這個能夠看作項目的座標,通常爲域和公司名組成 Artifact Id: 這個能夠看作成項目名 Version: 這個是項目版本
點擊Finish後,一個Maven項目就建立完成。此時,須要注意的是這個Maven項目可能用的是JDK1.5,這可能致使項目的目錄不正確,解決辦法就是:右鍵項目 -> 選擇Properties -> 選擇Java Build Path -> 選擇Libraries那一欄 -> 選擇JRE Library那一欄 -> Edit -> 把默認的JDK版本換成本身下載的版本便可。
1.1 首先須要依賴整合SSM相關的jar包
<properties> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <!-- 測試所須要的包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- 日誌所須要的包 --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <!-- 數據庫所須要的包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> <scope>runtime</scope> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- Mybatis的包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- JavaWeb的包 --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- 4.Spring的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!-- SpringMVC的包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- AOP配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.10.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.10.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>3.2.5</version> </dependency> <!-- Map工具類 --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2</version> </dependency> <!-- 圖片縮放的包 --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency> <!-- 文件上傳的包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> </dependencies>
這裏我分爲三個XML配置文件,一個是spring-dao(數據庫的配置以及DAO層的掃描),第二個是spring-service(掃描Service層以及事務的配置),第三個是spring-web(掃描controller層和配置文件視圖等)
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置相關的properties過程 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置數據庫鏈接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置鏈接池屬性 --> <property name="driverClass" value="${jdbc.driver}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- c3p0鏈接池的私有屬性 --> <property name="maxPoolSize" value="30" /> <property name="minPoolSize" value="10" /> <!-- 關閉鏈接後不自動commit --> <property name="autoCommitOnClose" value="false" /> <!-- 獲取鏈接超時時間 --> <property name="checkoutTimeout" value="10000" /> <!-- 當獲取鏈接失敗重試次數 --> <property name="acquireRetryAttempts" value="2" /> </bean> <!-- 配置連sqlSessionFactory對象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數據庫鏈接池 --> <property name="dataSource" ref="dataSource" /> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 掃描entity包 使用別名 --> <property name="typeAliasesPackage" value="com.yangxiang.pojo" /> <!-- 掃描sql配置文件:mapper須要的xml文件 --> <property name="mapperLocations" value="classpath:mapper/*.xml" /> </bean> <!-- 配置DAO接口包 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <!-- 給出須要掃描Dao接口包 --> <property name="basePackage" value="com.yangxiang.dao" /> </bean> </beans>
其中spring-dao引用了定義的jdbc.properties和mybatis-config.xml
jdbc.properties內容以下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/course?useUnicode=true&characterEncoding=utf8 jdbc.username=root jdbc.password=password
mybatis-config.xml內容以下:html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置全局屬性 --> <settings> <!-- 是否使用主鍵生成 --> <setting name="useGeneratedKeys" value="true" /> <!-- 別名比列標籤有限 --> <setting name="useColumnLabel" value="true"/> <!-- 是否使用下劃線與駝峯命名轉換 --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> </configuration>
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 掃描service包下全部使用註解的類型 --> <context:component-scan base-package="com.yangxiang.service" /> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數據庫鏈接池 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置基於註解的聲明式事務 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置SpringMVC --> <!-- 1.開啓SpringMVC註解模式 --> <mvc:annotation-driven /> <context:component-scan base-package="com.yangxiang.web" /> <!-- 定義靜態資源的處理 --> <!-- 2.靜態資源默認servlet配置 (1)加入對靜態資源的處理:js,gif,png (2)容許使用"/"作總體映射 --> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-handler /> <!-- 定義視圖解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/html/"></property> <property name="suffix" value=".html"></property> </bean> <!-- 文件上傳解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="10485760000"></property><!-- 最大上傳文件大小 --> <property name="maxInMemorySize" value="10960"></property> </bean> <!-- 在spring-mvc.xml文件中加入這段配置後,spring返回給頁面的都是utf-8編碼了 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> </beans>
在上面把SSM的配置寫完後,須要在web.xml引入
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 設置UTF-8格式 --> <filter> <filter-name>characterEncoding</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> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置SpringMVC核心攔截器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 初始化spring-dao,spring-service,spring-web配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
當完成以上配置後,就能夠開始寫代碼了!(小弟新手,以上所寫或許有錯,請包涵)