基于继承分模块构建Maven工程的实现(整理)

在maven工程的pom.xml文件中可以使用 标签将其他maven工程聚合到一起,聚合的目的是为了进行统一操作。
例如拆分后的maven工程有多个,如果要进行打包,就需要针对每个工程分别执行打包命令,操作起来非常繁琐。这时就可以使用标签将这些工程统一聚合到maven工程中,需要打包的时候,只需要在此工程中执行一次打包命令,其下被聚合的工程就都会被打包了。

1. 构建父模块ssmparent

在这里插入图片描述
注意:如果更改pom就需要import,如果右下角import没出现就右键Maven模块,手动操作

1.1编辑父模块的pom文件

<?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>sicilia.liu</groupId>
    <artifactId>ssm-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>ssm-pojo</module>
        <module>ssm-dao</module>
        <module>ssm-service</module>
        <module>ssm-web</module>
    </modules>

    <!--打包方式:
    jar包:普通工程,javase
    war包:表示互联网项目
    pom包:表示聚合包,聚合所有的子类,并且只有父工程才能打pom

    父工程不写代码,只会了管理jar包
    -->
    <packaging>pom</packaging>


    <properties>
        <spring.version>5.0.5.RELEASE</spring.version>
        <springmvc.version>5.0.5.RELEASE</springmvc.version>
        <mybatis.version>3.4.5</mybatis.version>
    </properties>
    <!--锁定jar版本

    dependencyManagement:只会锁定版本,不会真正的下载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>
                <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>

创建子模块ssmpojo,在ssmpojo的pom文件中导入jar包

<dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
    </dependencies>

2.构建子模块ssm-dao

2.1 在pojo模块的文件夹中创建Item类** 注意文件路径**

package sicilia.liu.domain;

import java.util.Date;

public class Item {
    private Integer id;
    private String name;
    private Float price;
    private Date createtime;
    private String detail;

在这里插入图片描述
目前的文件夹长这个样子

3. 创建子工程ssmdao

3.1配置ssmdao的pom文件

 <dependencies>
        <dependency>
            <!--依赖ssm-pojo-->
            <groupId>sicilia.liu</groupId>
            <artifactId>ssm-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驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <!-- druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!-- spring相关 -->
        <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测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

4.创建Dao接口

** 映射文件要写在resources中,要与ItemMapper接口平级**

4.1创建Mapper映射文件

package sicilia.liu.dao;

import sicilia.liu.domain.Item;

public interface ItemMapper {
    /**
     * 根据id对象进行查询
     */
    public Item findById(Integer 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="sicilia.liu.dao.ItemMapper">
    <select id="findById" parameterType="java.lang.Integer" resultType="item">
         select * from item where id=#{id}
    </select>
</mapper>

5.构建子模块ssm-service

5.1 配置ssmservice的pom文件

<?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">
    <parent>
        <artifactId>ssm-parent</artifactId>
        <groupId>sicilia.liu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ssm-service</artifactId>

    <dependencies>
        <dependency>
            <groupId>sicilia.liu</groupId>
            <artifactId>ssm-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

6. 创建ssm-service模块

6.1 创建ItemService接口

package com.sicilia.liu;

import sicilia.liudomain.Item;

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

6.2 创建ItemImpl的实现类

@Service
public class ItemImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Item findById(Integer id) {
        return itemMapper.findById(id);
    }
}

** 在实现类中通过AutoWired引入的ItemMapper可能会报红,注意检查有没有写@Service注释**

6.3 在ssm-service中创建spring的配置文件

约束太长就先不写了

<!-- 扫描service包-->
<context:component-scan base-package=s"sicilia.liu.service"></context:component-scan>

<!--配置数据源-->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <!-- url -->
        <property name="url" value="jdbc:mysql://localhost:3306/ssmtest"/>
        <!-- 用户名 -->
        <property name="username" value="root"/>
        <!-- 密码 -->
        <property name="password" value="123456"/>
    </bean>
 	<!--设置事务管理器-->
 	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
   <!--开启注解驱动-->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>

<--设置session工程-->
 <!--设置Session工程-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!--设置别名-->
        <property name="typeAliasesPackage" value="sicilia.liu.domain"></property>
    </bean>

在property标签中ref引用dataSource标签,不是value,细心细心细心

7. 创建子工程ssm-web

7.1 配置ssm-web的pom配置文件

<artifactId>ssm-web</artifactId>

    <packaging>war</packaging>


    <dependencies>
        <dependency>
            <groupId>sicilia.liu</groupId>
            <artifactId>ssm-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

7.2 创建Controller

package sicilia.liu.controller;

import sicilia.liu.domain.Item;
import com.atguigu.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
public class ItemController {

    @Autowired
    private ItemSerivce itemSerivce;

    @RequestMapping("/showItem/{id}")
    public String showItem(@PathVariable(value = "id") Integer id, Model model){
        Item item = itemSerivce.findById(id);
        model.addAttribute("item",item);
        return "success";
    }
}
// 注意包名结构

7.3 新建pages文件夹,创建success.jsp

<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${item}
</body>
</html>

7.4 配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
             <!--配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--配置字符编码乱码的过滤器-->
    <filter>
        <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
<servlet>

7.5 在resources文件夹中创建springmvc.xml配置文件

<context:component-scan base-package="sicilia.liu.controller"></context:component-scan>

    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--给js,css,image文件夹进行放行-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
    <!--开启注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--导入spring.xml配置文件-->
    <import resource="classpath:spring.xml"></import>

</beans>

End…

在这里插入图片描述

因为没有添加index所以需要在地址栏填入参数

结束

总结一下我出现的几个问题,基本上都是自己的马虎,都是一些弱智错误

  1. 注意标注** @Controller,@Service,@Autowried等注解**,否则可能会出现报红的问题
  2. 父模块的的** pom.xml**文件中不用写jar包的引入也可以
  3. 在web标签上的打包方式是** war**,父模块的打包标签是** pom**,父工程不写代码只负责写代码
  4. 父模块的标签是自动的不用手动写
  5. 每个pom标签的包名和各个模块的包名要注意
  6. main下的文件层级要与resources的层级相同
  7. 传参可以传入(@PathVariable(“id”) Integer id,Model model) model.addAttribute(“item”,item)
  8. springmvc配置文件最后要导入sring.xml
  9. 在spring.xml配置文件中配置事务管理器和设置Session工程的property标签要ref:dataSource

细心 细心 细心 减少因为马虎而产生的bug 努力改正错误 放平心态

** 心态缀重要 **

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值