Maven聚合工程项目具体流程示范

Maven聚合工程项目具体流程示范

1 AccountParent

1.1 创建AccountParentMaven工程

1.2 配置AccountParent的pom.xml

<?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>com.itheima</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>AccountDAO</module>
        <module>AccountService</module>
        <module>AccountWeb</module>
    </modules>

    <!--
    1.由于没有编写业务逻辑的场景,所以src目录不要了
    2.打包方式 pom
    -->
    <packaging>pom</packaging>

    <!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.10</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
        <httpclient.version>4.3.5</httpclient.version>
        <jstl.version>1.2</jstl.version>
        <servlet-api.version>2.5</servlet-api.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>1.3.2</commons-io.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.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-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis.spring.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>3.7.5</version>
            </dependency>
            <dependency>
                <groupId>com.github.jsqlparser</groupId>
                <artifactId>jsqlparser</artifactId>
                <version>0.9.1</version>
            </dependency>
            
            <!-- MySql -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <!-- Jackson Json处理工具包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>

            <!--C3P0连接池-->
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1.2</version>
            </dependency>

            <!--Dbutils 简化JDBC-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <!--mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!--c3p0-->
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1.2</version>
            </dependency>
            <!-- httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>

            <!-- JSP相关 -->
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>${servlet-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>

            <!-- 时间操作组件 -->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>${joda-time.version}</version>
            </dependency>

            <!-- Apache工具组件 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-io</artifactId>
                <version>${commons-io.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2 AccountDAO

2.1 准备数据库数据

create database maven_db;

use maven_db;

create table account(
	name varchar(50),
	money double
);

insert into account(name, money) values('aaa', 1000);
insert into account(name, money) values('bbb', 1000);

2.2 db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///maven_db
jdbc.username=root
jdbc.password=root

2.3 SqlMapConfig.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>

    <properties resource="db.properties"></properties>

    <typeAliases>
        <package name="com.itheima.pojo"/>
    </typeAliases>

    <environments default="default">
        <!--环境变量-->
        <environment id="default">
            <!--事务管理器:由JDBC管理事务 -->
            <transactionManager type="JDBC"/>
            <!--数据源配置信息:POOLED 使用连接池 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>


    <!-- 加载其他的映射文件 -->
    <mappers>
        <package name="com.itheima.mapper"/>
    </mappers>
</configuration>

2.4 配置AccountDAO的pom.xml

<?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>parent</artifactId>
        <groupId>com.itheima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>AccountDAO</artifactId>

    <dependencies>
        <!--
            使用 mysql  mybatis
        -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

2.5 AccountMapper

package com.itheima.mapper;

import org.apache.ibatis.annotations.Param;

public interface AccountMapper {

    //转入
    public void transferIn(@Param("inName") String inName, @Param("money") double money);

    //转出
    public void transferOut(@Param("outName") String outName, @Param("money") double money);
}

2.6 Test

package com.itheima.mapper;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.junit.Assert.*;

public class AccountMapperTest {
    private SqlSessionFactory factory;
    //初始化SqlSession 会话对象
    @Before
    public void init() throws IOException {
        //1.获取SqlSessionFactoryBuider
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //2.Resources 获取流
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //3.调用build方法获取SqlSessionfactory工厂对象
        factory = builder.build(inputStream);
    }
    //测试入账
    @Test
    public void transferInTest() {
        //4.获取openSession 获取sqlSession
        SqlSession sqlSession = factory.openSession(true);
        //5.sqlSession getMapper 获取接口的子类代理对象
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        //6.调用transferIn 方法
        mapper.transferIn("aaa",200);
    }

    //测试出账
    @Test
    public void transferOut() {
        //4.获取openSession 获取sqlSession
        SqlSession sqlSession = factory.openSession(true);
        //5.sqlSession getMapper 获取接口的子类代理对象
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        //6.调用transferOut 方法
        mapper.transferOut("bbb",200);
    }
}

3 AccountService

3.1配置AccountService的pom.xml

<?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>parent</artifactId>
        <groupId>com.itheima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>AccountService</artifactId>

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

</project>

3.2 AccountService接口

package com.itheima.service;

import java.io.IOException;

public interface AccountService {

    //转账
    public void transfer(String inName ,String outName ,double money) throws IOException;
}

3.3 AccountService实现类

package com.itheima.service.Impl;

import com.itheima.mapper.AccountMapper;
import com.itheima.service.AccountService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class AccountServiceImpl implements AccountService {
    @Override
    public void transfer(String inName, String outName, double money) throws IOException {
        //1.获取SqlSessionFactoryBuider
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //2.Resources 获取流
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //3.调用build方法获取SqlSessionfactory工厂对象
        SqlSessionFactory factory = builder.build(inputStream);
        //4.获取openSession 获取sqlSession
        SqlSession sqlSession = factory.openSession(true);
        //5.sqlSession getMapper 获取接口的子类代理对象
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        //6.调用transferIn 方法
        mapper.transferIn(inName,money);
        mapper.transferOut(outName,money);
    }
}

3.4 Test

package com.itheima.service.Impl;

import com.itheima.service.AccountService;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public class AccountServiceImplTest {

    //测试转账
    @Test
    public void transfer() throws IOException {
        AccountService accountService = new AccountServiceImpl();
        accountService.transfer("aaa","bbb",300);
    }
}

4 AccountWeb

4.1配置AccountWeb的pom.xml

<?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>parent</artifactId>
        <groupId>com.itheima</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>AccountWeb</artifactId>

    <packaging>war</packaging>

    <!--
    1.引入service
    2.引入servlet
    3.引入tomcat
    -->
    <dependencies>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>AccountService</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.2.1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>


    <!--引入tomcat插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/account</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

4.2 AccountServlet

package com.itheima.web;

import com.itheima.service.AccountService;
import com.itheima.service.Impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AccountServlet")
public class AccountServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //2.获取参数
        //最终: 浏览器访问  http://localhost:8080/AccountServlet?inName=aaa&outName=bbb&money=300
        String inName = request.getParameter("inName");
        String outName = request.getParameter("outName");
        String money = request.getParameter("money");

        //2.创建service
        AccountService service = new AccountServiceImpl();
        //3.调用service的转账方法transfer方法
        service.transfer(inName,outName,Double.parseDouble(money));
        //4.页面跳转到success.jsp 成功页面
        request.getRequestDispatcher("success.jsp").forward(request,response);
    }
}

4.3 success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!--设置字符集-->
    <meta charset="utf-8">
    <!--设置: 支持IE浏览器-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--
        viewport: 视口
         width=device-width  页面的宽度等于设备的宽度.
         initial-scale=1     设置缩放比(范围:1-5), 但是只针对于手机端有效.
    -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->

    <!--标题-->
    <title>transfer  success</title>

    <!-- 引入BootStrap样式 -->
    <link href="./bootstroop/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="./bootstroop/css/bootstrap-theme.min.css" rel="stylesheet"/>
    <!-- 引入jQuery文件 -->
    <script src="./bootstroop/js/jquery-1.11.3.min.js"></script>
    <!-- 引入BootStrap的js脚本文件(插件) -->
    <script src="./bootstroop/js/bootstrap.min.js"></script>
</head>
<body>

<!--布局方式-->
<div class="container-fluid">
    <!---->
    <div class="row">
        <h3>transfer  success  ....</h3>
    </div>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值