您好,登錄后才能下訂單哦!
這篇文章主要講解了“SSM如何搭建項(xiàng)目”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“SSM如何搭建項(xiàng)目”吧!
(1)為什么要用Maven搭建項(xiàng)目?
Maven可以對(duì)項(xiàng)目依賴的jar包進(jìn)行管理,需要的jar包只需要到Maven倉庫里面去拿到版本信息復(fù)制到pom.xml文件即可。同時(shí),它也能對(duì)項(xiàng)目進(jìn)行編譯、測試、打包等功能。
(2)新建一個(gè)Maven工程(這里用的是Eclipse):
點(diǎn)擊File -> New Project -> Maven Project,點(diǎn)擊Next;
彈出一個(gè)界面,這里直接點(diǎn)擊Next,然后選擇webapp那一欄,點(diǎn)擊Next;
此時(shí),將進(jìn)入另一個(gè)界面,會(huì)叫你輸入一些有關(guān)項(xiàng)目的信息。主要的分別是:
Group Id: 這個(gè)可以看做項(xiàng)目的坐標(biāo),一般為域和公司名組成
Artifact Id: 這個(gè)可以看做成項(xiàng)目名
Version: 這個(gè)是項(xiàng)目版本
點(diǎn)擊Finish后,一個(gè)Maven項(xiàng)目就創(chuàng)建完成。此時(shí),需要注意的是這個(gè)Maven項(xiàng)目可能用的是JDK1.5,這可能導(dǎo)致項(xiàng)目的目錄不正確,解決辦法就是:右鍵項(xiàng)目 -> 選擇Properties -> 選擇Java Build Path -> 選擇Libraries那一欄 -> 選擇JRE Library那一欄 -> Edit -> 把默認(rèn)的JDK版本換成自己下載的版本即可。
(1)首先需要依賴整合SSM相關(guān)的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> <!-- 數(shù)據(jù)庫所需要的包 --> <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>
這里我分為三個(gè)XML配置文件,一個(gè)是spring-dao(數(shù)據(jù)庫的配置以及DAO層的掃描),第二個(gè)是spring-service(掃描Service層以及事務(wù)的配置),第三個(gè)是spring-web(掃描controller層和配置文件視圖等)
(1)配置spring-dao.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: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"> <!-- 配置相關(guān)的properties過程 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置數(shù)據(jù)庫連接池 --> <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" /> <!-- 關(guān)閉連接后不自動(dòng)commit --> <property name="autoCommitOnClose" value="false" /> <!-- 獲取連接超時(shí)時(shí)間 --> <property name="checkoutTimeout" value="10000" /> <!-- 當(dāng)獲取連接失敗重試次數(shù) --> <property name="acquireRetryAttempts" value="2" /> </bean> <!-- 配置連sqlSessionFactory對(duì)象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入數(shù)據(jù)庫連接池 --> <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內(nèi)容如下:
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內(nèi)容如下:
<?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" /> <!-- 別名比列標(biāo)簽有限 --> <setting name="useColumnLabel" value="true"/> <!-- 是否使用下劃線與駝峰命名轉(zhuǎn)換 --> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings></configuration>
配置spring-service.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: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" /> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入數(shù)據(jù)庫連接池 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置基于注解的聲明式事務(wù) --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
配置spring-web.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: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" /> <!-- 定義靜態(tài)資源的處理 --> <!-- 2.靜態(tài)資源默認(rèn)servlet配置 (1)加入對(duì)靜態(tài)資源的處理: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> <!-- 設(shè)置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>
當(dāng)完成以上配置后,就可以開始寫代碼了。
感謝各位的閱讀,以上就是“SSM如何搭建項(xiàng)目”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)SSM如何搭建項(xiàng)目這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。