Maven深入-SSM整合和分模塊構(gòu)建

Maven高級(jí)

1.maven基礎(chǔ)知識(shí)回顧

1.1 maven介紹

maven 是一個(gè)項(xiàng)目管理工具饥努,主要作用是在項(xiàng)目開發(fā)階段對(duì)Java項(xiàng)目進(jìn)行依賴管理和項(xiàng)目構(gòu)建簇捍。

依賴管理:就是對(duì)jar包的管理。通過導(dǎo)入maven坐標(biāo)城丧,就相當(dāng)于將倉庫中的jar包導(dǎo)入了當(dāng)前項(xiàng)目中僵娃。

項(xiàng)目構(gòu)建:通過maven的一個(gè)命令就可以完成項(xiàng)目從清理池颈、編譯、測(cè)試刺彩、報(bào)告、打包枝恋,部署整個(gè)過程创倔。

?

1.2 maven的倉庫類型

1.本地倉庫

2.遠(yuǎn)程倉庫

①maven中央倉庫(地址:http://repo2.maven.org/maven2/

②maven私服(公司局域網(wǎng)內(nèi)的倉庫,需要自己搭建)

③其他公共遠(yuǎn)程倉庫(例如apache提供的遠(yuǎn)程倉庫焚碌,地址:http://repo.maven.apache.org/maven2/

1.3 maven常用命令

clean: 清理

compile:編譯

test: 測(cè)試

package:打包

install: 安裝

1.4 maven坐標(biāo)書寫規(guī)范

1.5 maven的依賴范圍

依賴范圍 對(duì)于編譯classpath有效 對(duì)于測(cè)試classpath有效 對(duì)于運(yùn)行時(shí)classpath有效 例子
compile Y Y Y spring-core
test - Y - Junit
provided Y Y - servlet-api
runtime - Y Y JDBC驅(qū)動(dòng)
system Y Y - 本地的畦攘,maven倉庫之外的類庫

2. maven的依賴傳遞

2.1 什么是依賴傳遞

在maven中,依賴是可以傳遞的十电,假設(shè)存在三個(gè)項(xiàng)目知押,分別是項(xiàng)目A,項(xiàng)目B以及項(xiàng)目C鹃骂。假設(shè)C依賴B台盯,B依賴A,那么我們可以根據(jù)maven項(xiàng)目依賴的特征不難推出項(xiàng)目C也依賴A畏线。

通過上面的圖可以看到静盅,我們的web項(xiàng)目直接依賴了spring-webmvc,而spring-webmvc依賴了sping-aop寝殴、spring-beans等蒿叠。最終的結(jié)果就是在我們的web項(xiàng)目中間接依賴了spring-aop明垢、spring-beans等。

2.2 什么是依賴沖突

由于依賴傳遞現(xiàn)象的存在市咽, spring-webmvc 依賴 spirng-beans-4.2.4痊银,spring-aop 依賴 spring-beans-5.0.2,但是發(fā)現(xiàn) spirng-beans-4.2.4 加入到了工程中施绎,而我們希望 spring-beans-5.0.2 加入工程溯革。這就造成了依賴沖突。

我們肯定是希望使用版本更新的JAR包粘姜,但是在這里鬓照,它會(huì)使用舊的4.2.4的包,新版本的沒有用生效孤紧。

2.3 如何解決依賴沖突

1.使用maven提供的依賴調(diào)解原則

  • 第一聲明者優(yōu)先原則

  • 路徑近者優(yōu)先原則

2.排除依賴

3.鎖定版本

2.4 依賴調(diào)節(jié)原則

第一聲明者優(yōu)先原則

在 pom 文件中定義依賴豺裆,以先聲明的依賴為準(zhǔn)。其實(shí)就是根據(jù)坐標(biāo)導(dǎo)入的順序來確定最終使用哪個(gè)傳遞過來的依賴号显。轉(zhuǎn)換一下順序就行了臭猜。

結(jié)論:通過上圖可以看到,spring-aop和spring-webmvc都傳遞過來了spring-beans押蚤,但是因?yàn)閟pring-aop在前面蔑歌,所以最終使用的spring-beans是由spring-aop傳遞過來的,而spring-webmvc傳遞過來的spring-beans則被忽略了揽碘。

這個(gè)方式并不是很好次屠,因?yàn)楹笃谖覀冏鲰?xiàng)目可能要引用幾十個(gè)坐標(biāo),這么多坐標(biāo)我們能一個(gè)個(gè)去調(diào)整嗎雳刺,調(diào)整之后甚至也會(huì)出現(xiàn)一些問題劫灶。

路徑近者優(yōu)先原則

在 pom 文件定義依賴,以路徑近者為準(zhǔn)掖桦。

還是上述情況本昏,spring-aop 和 spring-webmvc 都會(huì)傳遞過來 spirng-beans,那如果直接把 spring-beans 的依賴直接寫到 pom 文件中枪汪,那么項(xiàng)目就不會(huì)再使用其他依賴傳遞來的 spring-beans涌穆,因?yàn)樽约褐苯釉?pom 中定義 spring-beans要比其他依賴傳遞過來的路徑要近。

2.5 排除依賴

可以使用exclusions標(biāo)簽將傳遞過來的依賴排除出去雀久。(這個(gè)方法可能會(huì)經(jīng)常使用到)

2.6 版本鎖定

采用直接鎖定版本的方法確定依賴jar包的版本宿稀,版本鎖定后則不考慮依賴的聲明順序或依賴的路徑,以鎖定的版本為準(zhǔn)添加到工程中赖捌,此方法在企業(yè)開發(fā)中經(jīng)常使用原叮。

版本鎖定的使用方式:

第一步:在dependencyManagement標(biāo)簽中鎖定依賴的版本

第二步:在dependencies標(biāo)簽中聲明需要導(dǎo)入的maven坐標(biāo)

①在dependencyManagement標(biāo)簽中鎖定依賴的版本

注意:pom文件中使用dependencyManagement標(biāo)簽進(jìn)行依賴jar的版本鎖定,并不會(huì)真正將jar包導(dǎo)入到項(xiàng)目中,只是對(duì)這些jar的版本進(jìn)行鎖定奋隶。項(xiàng)目中使用哪些jar包擂送,還需要在dependencies標(biāo)簽中進(jìn)行聲明。

②在dependencies標(biāo)簽中聲明需要導(dǎo)入的maven坐標(biāo)

注意:由于前面已經(jīng)在dependencyManagement標(biāo)簽中鎖定了依賴jar包的版本唯欣,后面需要導(dǎo)入依賴時(shí)只需要指定groupId和artifactId嘹吨,無須再指定version。

3.基于maven構(gòu)建SSM工程案例

3.1 需求描述

本案例基于maven構(gòu)建 SSM(Spring+SpringMVC+Mybatis)工程境氢,通過maven坐標(biāo)進(jìn)行依賴管理蟀拷。最終實(shí)現(xiàn)根據(jù) id 查詢商品信息的功能。

3.2 構(gòu)建maven工程

1.數(shù)據(jù)庫環(huán)境搭建

? ①創(chuàng)建數(shù)據(jù)庫test

 CREATE DATABSAE
    CHARACTER SET utf8
    COLLATE utf8_general_ci;

? ②創(chuàng)建商品表item

CREATE TABLE `item` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `price` float default NULL,
  `createtime` datetime default NULL,
  `detail` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2.maven項(xiàng)目構(gòu)建

①創(chuàng)建maven web項(xiàng)目


注意:由于創(chuàng)建的是maven 的web工程萍聊,缺少Java目錄问芬,需要手動(dòng)添加,并將Java目錄指定為Sources Root寿桨,如果缺少resources目錄此衅,還需要將resources目錄指定為Resources Root!Mっ挡鞍!

②配置pom.xml文件

③實(shí)現(xiàn)spring+mybatis整合

創(chuàng)建POJO類

public class Item {
private Integer id;
private String name;
private Float price;
private Date createtime;
private String detail;
//省略setter、getter
}

持久層DAO接口編寫

public interface ItemMapper {
       public Item findById(int id);
}

Mapper映射文件編寫

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.dao.ItemMapper">
<select id="findById" parameterType="int" resultType="item">
         select * from item where id=#{id}</select>
</mapper>

業(yè)務(wù)層Service編寫

package com.itheima.ssm.service;
import com.itheima.ssm.pojo.Item;
public interface ItemService {
    public Item findById(int id);
} 
@Service
@Transactional
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
public Item findById(int id) {
return itemMapper.findById(id);
}
}

spring配置文件applicationContext-dao.xml編寫

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/bean       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd      http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!--    加載外部properties-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!-- 數(shù)據(jù)庫連接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="DriverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- mapper配置 -->
    <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數(shù)據(jù)庫連接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!--為指定包下的所有實(shí)體類創(chuàng)建別名-->
        <property name="typeAliasesPackage" value="org.example.domain"/>
    </bean>
    <!-- mapper掃描器 :用來產(chǎn)生代理對(duì)象-->
    <!-- 動(dòng)態(tài)代理 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.example.dao"></property>
    </bean>
</beans>

spring配置文件applicationContext-service.xml編寫

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/bean?       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd      http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!--    配置掃描器,只負(fù)責(zé)掃描Service-->
    <context:component-scan base-package="org.example.service">

    </context:component-scan>

<!--    事務(wù)管理器-->
    <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource"ref="dataSource"></property>
    </bean>

<!--    事務(wù)的注解驅(qū)動(dòng)-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

④加入springmvc相關(guān)配置

表現(xiàn)層Controller編寫

@Controller
@RequestMapping("/item")
public class ItemController {
   @Autowired
   private ItemService itemService;
  @RequestMapping("/showItem/{id}")
  public String showItem(@PathVariable("id") int id, Model model){
        Item item = itemService.findById(id);
        model.addAttribute("item",item);
        return "item";   
  }
}

springmvc.xml文件編寫

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <context:component-scan base-package="com.itheima.ssm.controller"/>
<!--  配置視圖解析器的前綴和后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">           <property name="prefix“ value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
</beans>

jsp頁面編寫

配置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>

  <!--指定Spring配置文件位置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext*.xml</param-value>
  </context-param>

  <!--配置Spring框架啟動(dòng)時(shí)使用的監(jiān)聽器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--配置SpringMVC的前端控制器-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

4.分模塊構(gòu)建maven工程

4.1 分模塊構(gòu)建maven工程分析

在現(xiàn)實(shí)生活中预烙,汽車廠家進(jìn)行汽車生產(chǎn)時(shí)墨微,由于整個(gè)生產(chǎn)過程非常復(fù)雜和繁瑣,工作量非常大扁掸,所以車場(chǎng)都會(huì)將整個(gè)汽車的部件分開生產(chǎn)翘县,最終再將生產(chǎn)好的部件進(jìn)行組裝,形成一臺(tái)完整的汽車谴分。

常見拆分方式有兩種:

  • 按照業(yè)務(wù)模塊進(jìn)行拆分锈麸,每個(gè)模塊拆分成一個(gè)maven工程,例如將一個(gè)項(xiàng)目分為用戶模塊狸剃,訂單模塊等,每個(gè)模塊對(duì)應(yīng)的就是一個(gè)maven工程狗热。
  • 按照層進(jìn)行拆分钞馁,例如持久層、業(yè)務(wù)層匿刮、表現(xiàn)層等每個(gè)層對(duì)應(yīng)的就是一個(gè)maven工程僧凰。

不管哪種拆分方式,通常都會(huì)提供一個(gè)父工程熟丸,將一些公共的代碼和配置提取到父工程中統(tǒng)一管理和配置训措。

4.2 maven工程的繼承

在Java語言中,類之間是可以繼承的,通過繼承绩鸣,子類就可以引用父類中非private的屬性和方法怀大。同樣,在maven工程之間也可以繼承呀闻,子工程繼承父工程后化借,就可以使用在父工程中引入的依賴。繼承的目的是為了消除重復(fù)代碼捡多。

被繼承的maven工程通常稱為父工程蓖康,父工程的打包方式必須為pom,所以我們區(qū)分某個(gè)maven工程是否為父工程就看這個(gè)工程的打包方式是否為pom

繼承其他maven父工程的工程通常稱為子工程垒手,在pom.xml文件中通過parent標(biāo)簽進(jìn)行父工程的繼承


4.3 maven工程的聚合

在maven工程的pom.xml文件中可以使用<modules>標(biāo)簽將其他maven工程聚合到一起蒜焊,聚合的目的是為了進(jìn)行統(tǒng)一操作。

例如拆分后的maven工程有多個(gè)科贬,如果要進(jìn)行打包泳梆,就需要針對(duì)每個(gè)工程分別執(zhí)行打包命令,操作起來非常繁瑣唆迁。這時(shí)就可以使用<modules>標(biāo)簽將這些工程統(tǒng)一聚合到maven工程中鸭丛,需要打包的時(shí)候,只需要在此工程中執(zhí)行一次打包命令唐责,其下被聚合的工程就都會(huì)被打包了鳞溉。

在父親的pom.xml里面填寫

<modules>
        <module>maven_pojo</module>
        <module>maven_service</module>
        <module>maven_dao</module>
        <module>maven_web</module>
    </modules>

執(zhí)行maven命令package

4.4 分模塊構(gòu)建maven工程具體實(shí)現(xiàn)

①父工程maven_parent構(gòu)建

 <?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>maven_parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>maven_pojo</module>
        <module>maven_dao</module>
        <module>maven_service</module>
        <module>maven_web</module>
    </modules>

    <properties>
        <spring.version>5.2.9.RELEASE</spring.version>
        <springmvc.version>5.2.9.RELEASE</springmvc.version>
        <mybatis.version>3.4.6</mybatis.version>
    </properties>
    <!--鎖定jar版本-->
    <dependencyManagement>
        <dependencies>
            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <!-- springMVC -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${springmvc.version}</version>
            </dependency>
            <!-- spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</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-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</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>
        </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

②子工程maven_pojo構(gòu)建

pom.xml

<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>

③3.1子工程maven_dao構(gòu)建

? 3.2 配置maven_dao工程的pom.xml文件

 <dependencies>

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven_pojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- Mybatis和mybatis與spring的整合 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- MySql驅(qū)動(dòng) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <!-- druid數(shù)據(jù)庫連接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- spring相關(guān) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <!-- junit測(cè)試 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
        </dependency>
    </dependencies>

service的pom.xml

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>maven_dao</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

web的pom.xml

  <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

    </dependencies>

? 3.3 創(chuàng)建DAO接口和Mapper映射文件

public interface ItemMapper {
    public Item findById(int id);
}

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.dao.ItemMapper">
    <select id="findById" parameterType="int" resultType="Item">
        select * from item where id = #{id}
    </select>
</mapper>

? 3.4 在resources目錄下創(chuàng)建spring配置文件applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置數(shù)據(jù)源信息,使用druid連接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/ssmtest"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <!--配置spring整合mybatis框架的SQLSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--掃描pojo包鼠哥,為實(shí)體類創(chuàng)建別名-->
        <property name="typeAliasesPackage" value="com.itheima.ssm.pojo"/>
    </bean>

    <!--mapper掃描器熟菲,用于產(chǎn)生代理對(duì)象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.ssm.dao"/>
    </bean>
</bean

④子工程maven_service構(gòu)建

? 第一步:創(chuàng)建maven_service工程

? 第二步:配置maven_service工程的pom.xml文件

<dependencies>
    <dependency>
        <groupId>com.itheima</groupId>
        <artifactId>maven_dao</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

? 第三步:創(chuàng)建Service接口和實(shí)現(xiàn)類

package com.itheima.ssm.service;

import com.itheima.ssm.pojo.Item;

public interface ItemService {
    public Item findById(int id);
}

package com.itheima.ssm.service;

import com.itheima.ssm.dao.ItemMapper;
import com.itheima.ssm.pojo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    public Item findById(int id) {
        return itemMapper.findById(id);
    }
}

第四步:創(chuàng)建spring配置文件applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置掃描器,掃描Service-->
    <context:component-scan base-package="com.itheima.ssm.service"/>

    <!--事務(wù)管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--事物注解驅(qū)動(dòng)-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

⑤子工程maven_web構(gòu)建

? 第一步:創(chuàng)建maven_web工程朴恳,注意打包方式為war

? 第二步:配置maven_web工程的pom.xml文件

<properties>
? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? <maven.compiler.source>1.8</maven.compiler.source>
? <maven.compiler.target>1.8</maven.compiler.target>
? </properties>

<dependencies>
    <dependency>
        <groupId>com.itheima</groupId>
        <artifactId>maven_service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
</dependencies>

<build>
    <finalName>maven_web</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

?

? 第三步:創(chuàng)建Controller

package com.itheima.ssm.controller;

import com.itheima.ssm.pojo.Item;
import com.itheima.ssm.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/showItem/{id}")
    public String findById(@PathVariable("id") int id, Model model){
        Item item = itemService.findById(id);
        model.addAttribute("item",item);
        return "item";
    }
}

? 第四步:創(chuàng)建jsp頁面

? 第五步:配置web.xml

 <!--指定Spring配置文件位置-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext*.xml</param-value>
  </context-param>

  <!--配置Spring框架啟動(dòng)時(shí)使用的監(jiān)聽器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--配置SpringMVC的前端控制器-->
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

? 第六步:創(chuàng)建springmvc配置文件springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置掃描器抄罕,掃描Controller-->
    <context:component-scan base-package="com.itheima.ssm.controller"/>

    <!--視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

項(xiàng)目整體結(jié)構(gòu)如下:

1)maven_parent為父工程,其余工程為子工程于颖,都繼承父工程maven_parent

2)maven_parent工程將其子工程都進(jìn)行了聚合

3)子工程之間存在依賴關(guān)系呆贿,比如maven_dao依賴, maven_pojo森渐、maven_service依賴maven_dao做入、 maven_web依賴maven_service


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市同衣,隨后出現(xiàn)的幾起案子竟块,更是在濱河造成了極大的恐慌,老刑警劉巖耐齐,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浪秘,死亡現(xiàn)場(chǎng)離奇詭異蒋情,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)耸携,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門棵癣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來搅窿,“玉大人芜辕,你說我怎么就攤上這事÷鹕耍” “怎么了刷后?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵的畴,是天一觀的道長。 經(jīng)常有香客問我尝胆,道長丧裁,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任含衔,我火速辦了婚禮煎娇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘贪染。我一直安慰自己缓呛,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布杭隙。 她就那樣靜靜地躺著哟绊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪痰憎。 梳的紋絲不亂的頭發(fā)上票髓,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音铣耘,去河邊找鬼洽沟。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蜗细,可吹牛的內(nèi)容都是我干的裆操。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼炉媒,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼踪区!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起橱野,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤朽缴,失蹤者是張志新(化名)和其女友劉穎善玫,沒想到半個(gè)月后水援,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體密强,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年蜗元,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了或渤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡奕扣,死狀恐怖薪鹦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情惯豆,我是刑警寧澤池磁,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站楷兽,受9級(jí)特大地震影響地熄,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜芯杀,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一端考、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧揭厚,春花似錦却特、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至顽染,卻和暖如春漾岳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背粉寞。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國打工尼荆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人唧垦。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓捅儒,卻偏偏與公主長得像,于是被迫代替她去往敵國和親振亮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子巧还,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

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