使用maven構(gòu)建SSM項目

準備工作

1.下載maven,配置環(huán)境變量
2.eclipse中設(shè)置本地maven
3.修改eclipse.ini配置文件
4.eclipse修改jre為jkd目錄下的jre

創(chuàng)建maven項目

new -> other -> Maven -> Maven Project -> maven-archetype-webapp

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ztc</groupId>
  <artifactId>ssmdemo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>ssmdemo Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!-- 單元測試 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- 日志 -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.1.1</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.3.0</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.3</version>
    </dependency>
    <!-- servlet -->
    <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.5.4</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!-- spring -->
    <!-- spring核心 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- spring DAO層 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- spring web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- spring test -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- redis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-core</artifactId>
        <version>1.0.8</version>
    </dependency>
    <dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-runtime</artifactId>
        <version>1.0.8</version>
    </dependency>
    <!-- Map工具類 -->
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>ssmdemo</finalName>
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
  </build>
</project>

補全項目結(jié)構(gòu)

Paste_Image.png

開始編程

先在spring文件夾里新建spring-dao.xml文件
spring-dao.xml文件中主要配置:
1.讀取數(shù)據(jù)庫相關(guān)的properties文件
2.配置第三方數(shù)據(jù)庫連接池
3.配置SqlSessionFactory對象
4.掃描dao層接口向胡,動態(tài)實現(xiàn)dao接口气堕,也就是說不需要daoImpl肾筐,sql和參數(shù)都寫在xml文件上
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">
    
    <!-- 加載數(shù)據(jù)庫相關(guān)參數(shù)配置文件 -->
    <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"/>
        
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <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">
        <!-- 注入數(shù)據(jù)庫連接池 -->
        <property name="dataSoutce" ref="dataSource"/>
        <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 掃描po包 使用別名 -->
        <property name="typeAliasesPackage" value=""/>
        <!-- 掃描sql配置文件:mapper需要的xml文件 -->
        <property name="mapperLocation" value="classpath:mapper/*.xml"/>
    </bean>
    
    <!-- 配置掃描Dao接口包良价,動態(tài)實現(xiàn)Dao接口篓像,注入到spring容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注入sqlSessionFactory -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <!-- 給出需要掃描Dao接口包 -->
        <property name="basePackage" value="" />
    </bean>
</beans>```
數(shù)據(jù)庫配置相關(guān)參數(shù)是讀取配置文件触徐,所以在resources文件夾里新建一個jdbc.properties文件。
**jdbc.properties**

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=```
配置mybatis核心文件晌涕,在recources文件夾里新建mybatis-config.xml文件滋捶。

1.使用自增主鍵
2.使用列別名
3.開啟駝峰命名轉(zhuǎn)換 create_time -> createTime
mybatis-config.xml

<?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>
        <!-- 使用jdbc的getGeneratedKeys獲取數(shù)據(jù)庫自增主鍵值 -->
        <setting name="useGeneratedKeys" value="true" />

        <!-- 使用列別名替換列名 默認:true -->
        <setting name="useColumnLabel" value="true" />

        <!-- 開啟駝峰命名轉(zhuǎn)換:Table{create_time} -> Entity{createTime} -->
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>```

在spring文件夾里新建spring-service.xml文件。

1.掃描service包所有注解 @Service
2.配置事務(wù)管理器余黎,把事務(wù)管理交由spring來完成
3.配置基于注解的聲明式事務(wù)重窟,可以直接在方法上@Transaction
**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">

<context:component-scan base-package="com.ztc.ssm.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文件夾里新建spring-web.xml文件。

1.開啟SpringMVC注解模式惧财,可以使用@RequestMapping巡扇,@PathVariable,@ResponseBody等
2.對靜態(tài)資源處理垮衷,如js厅翔,css,jpg等
3.配置jsp 顯示ViewResolver搀突,例如在controller中某個方法返回一個string類型的"login"刀闷,實際上會返回"/WEB-INF/login.jsp"
4.掃描web層 @Controller
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" 
    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.0.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.開啟SpringMVC注解模式 -->
    <!-- 簡化配置: 
        (1)自動注冊DefaultAnootationHandlerMapping,AnotationMethodHandlerAdapter 
        (2)提供一些列:數(shù)據(jù)綁定,數(shù)字和日期的format @NumberFormat, @DateTimeFormat, xml,json默認讀寫支持 
    -->
    <mvc:annotation-driven />

    <!-- 2.靜態(tài)資源默認servlet配置
        (1)加入對靜態(tài)資源的處理:js,gif,png
        (2)允許使用"/"做整體映射
     -->
     <mvc:default-servlet-handler/>

     <!-- 3.配置jsp 顯示ViewResolver -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
     </bean>

     <!-- 4.掃描web相關(guān)的bean -->
     <context:component-scan base-package="com.ztc.ssm.vo" />
</beans>```
修改web.xml文件了仰迁,它在webapp的WEB-INF下甸昏。
**web.xml**

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">


<servlet>
<servlet-name>seckill-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>seckill-dispatcher</servlet-name>

<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>```

有配置日志xml,在resources文件夾里新建logback.xml文件

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>```

##總結(jié)
**pom.xml中配置依賴項:**
單元測試(junit)徐许、日志(log4j)施蜜、數(shù)據(jù)庫鏈接(mysql)、第三方數(shù)據(jù)庫連接池(druid)雌隅、spring包(包含springmvc)花墩、mybatis獨立包、mybatis-spring整合包澄步、servlet包(taglib/jstl)、json解析和泌、redis村缸、aop、上傳文件

**spring-dao.xml:**
配置數(shù)據(jù)庫連接池
配置SqlSessionFactory對象(注入連接池武氓、mapper.xml文件位置梯皿、mybatis全局配置文件)
配置掃描Dao接口包,動態(tài)實現(xiàn)Dao接口县恕,注入到spring容器中(注入dao接口的位置)

**spring-service.xml:**
配置事務(wù)管理器 
配置基于注解的聲明式事務(wù)
aop东羹、通知、傳播行為

**spring-web.xml:**
開啟SpringMVC注解模式
配置視圖解析器
配置掃描controller
文件上傳

**mybatis-config.xml:**
配置settings屬性

####github地址:https://github.com/GITFUCKK/mavenSSM1.git
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忠烛,一起剝皮案震驚了整個濱河市属提,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖冤议,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斟薇,死亡現(xiàn)場離奇詭異,居然都是意外死亡恕酸,警方通過查閱死者的電腦和手機堪滨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蕊温,“玉大人袱箱,你說我怎么就攤上這事∫迕” “怎么了发笔?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長症革。 經(jīng)常有香客問我筐咧,道長,這世上最難降的妖魔是什么噪矛? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任量蕊,我火速辦了婚禮,結(jié)果婚禮上艇挨,老公的妹妹穿的比我還像新娘残炮。我一直安慰自己,他們只是感情好缩滨,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布势就。 她就那樣靜靜地躺著,像睡著了一般脉漏。 火紅的嫁衣襯著肌膚如雪苞冯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天侧巨,我揣著相機與錄音舅锄,去河邊找鬼。 笑死司忱,一個胖子當著我的面吹牛皇忿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播坦仍,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼鳍烁,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了繁扎?” 一聲冷哼從身側(cè)響起幔荒,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后铺峭,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體墓怀,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年卫键,在試婚紗的時候發(fā)現(xiàn)自己被綠了傀履。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡莉炉,死狀恐怖钓账,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情絮宁,我是刑警寧澤梆暮,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站绍昂,受9級特大地震影響啦粹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜窘游,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一唠椭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧忍饰,春花似錦贪嫂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至赢织,卻和暖如春亮靴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背于置。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工台猴, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人俱两。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像曹步,于是被迫代替她去往敵國和親宪彩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容