文章目录
- 🔥Maven聚合案例_搭建dao模块
- 🔥Maven聚合案例_搭建service模块
- 🔥Maven聚合案例_搭建web模块
- 🔥Maven聚合案例_运行项目
- 🔥依赖传递失效及解决方案
 
🔥Maven聚合案例_搭建dao模块
dao子工程中一般写实体类和dao层:
- 在父工程下创建maven模块,不选择模板,创建时一定要选择父工程。
  
- 子模块的pom文件中写入父工程证明继承成功。
  
- 准备数据库
CREATE DATABASE 'student';
USE 'student';
DROP TABLE IF EXISTS 'student';
CREATE TABLE `student` (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'name' varchar(255) DEFAULT NULL,
  'sex' varchar(10) DEFAULT NULL,
  'address' varchar(255) DEFAULT NULL,
  PRIMARY KEY ('id')
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
insert  into
'student'('id','name','sex','address')values (1,'程序员','男','北京'),(2,
'程序猿','女','北京');
- 编写实体类
public class Student {
    private int id;
    private String name;
    private String sex;
    private String address;
    
    // 省略getter/setter/tostring/构造方法
}
- 在resources中编写连接数据库的配置文件db.properties
jdbc.url=jdbc:mysql:///student
jdbc.user=root
jdbc.password=root
- 编写dao方法
public class StudentDao {
    // 查询所有学生
    public List<Student> findAll() throws Exception {
        // 读取配置文件
        Properties properties = new Properties();
        InputStream is = this.getClass().getClassLoader().getResour
ceAsStream("db.properties");
        properties.load(is);
        String url = properties.getProperty("jdbc.url");
        String user = properties.getProperty("jdbc.user");
        String password = properties.getProperty("jdbc.password");
        // 查询数据库
        Connection connection = DriverManager.getConnection(url, user,
password);
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from
student");
        // 处理结果集
        List<Student> students = new ArrayList();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            String address = resultSet.getString("address");
            Student student = new Student(id, name, sex, address);
            students.add(student);
       }
        // 释放资源
        resultSet.close();
        statement.close();
        connection.close();
        return students;
   }
}
- 测试 dao 方法
public class StudentDaoTest {
    // 测试findAll
    @Test
    public void testFindAll() throws Exception {
        StudentDao studentDao = new StudentDao();
        List<Student> all = studentDao.findAll();
        all.forEach(System.out::println);
   }
}
🔥Maven聚合案例_搭建service模块
service子工程中一般写service层的内容,也需要继承父工程,由于需要调用dao子工程的方法,所以需要导入dao子工程的依赖。
- 在父工程下创建maven模块,不选择模板,选择父工程。
- 在service模块的pom文件中引入dao子工程的依赖。
<dependencies>
    <dependency>
     <groupId>com.it</groupId>
	 <artifactId>maven_demo2_dao</artifactId>
     <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
- 编写Service方法
public class StudentService {
    private StudentDao studentDao = new StudentDao();
    public List<Student> findAllStudent() throws Exception {
        return studentDao.findAll();
   }
}
- 测试service方法
public class StudentServiceTest {
    // 测试findAllStudent
    @Test
    public void testFindAll() throws Exception {
       StudentService studentService = new StudentService();
        List<Student> allStudent = studentService.findAllStudent();
      	allStudent.forEach(System.out::println);
   }
}
🔥Maven聚合案例_搭建web模块
web子工程中一般要写控制器和前端页面的内容。它不是普通的java工程,而是一个web工程,需要继承父工程,导入service子工程的依赖。
-  在父工程下创建maven模块,选择web工程模板,选择父工程。 
  
-  创建好后,添加父工程,删除pom文件中的jdk编译版本,删掉junit依赖,引入service依赖。 
<parent>
    <artifactId>maven_demo2</artifactId>
    <groupId>com.itbaizhan</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>com.itbaizhan</groupId>
        <artifactId>maven_demo2_service</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>
- 编写控制器
@WebServlet("/allStudent")
public class FindAllStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentService();
        List<Student> allStudent = null;
        try {
            allStudent = studentService.findAllStudent();
       } catch (Exception e) {
            e.printStackTrace();
		req.setAttribute("allStudent",allStudent);
		req.getRequestDispatcher("allStudent.jsp").forward(req,resp);
   }
}
- 编写JSP页面
<%@ page contentType="text/html;charset=UTF-8" language="java"isELIgnored="false" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	<html>
		<head>
    		<title>所有学生</title>
		</head>
	<body>
		<table align="center" border="1"cellspacing="0" cellpadding="0"
width="500px">
    <tr>
        <th>id</th>
        <th>姓名</th>
        <th>性别</th>
        <th>地址</th>
    </tr>
    <c:forEach items="${allStudent}" 
    			var="student">
      	 <tr>
            <td>${student.id}</td>
            <td>${student.name}</td>
            <td>${student.sex}</td>
            <td>${student.address}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>
🔥Maven聚合案例_运行项目
有以下两种方式可以运行项目:
-  配置tomcat插件运行父工程,此时会自动聚合运行项目。 
 点开配置tomcat7插件:
  
 进行如下配置:
  
 点击运行箭头即可访问 http://localhost:8080/allStudent
-  配置tomcat插件运行web子工程。 
 运行web子工程时会从本地仓库下载依赖的jar包,所以必须将dao模块和service模块发布到本地仓库,我们可以直接发布整个项目。
  
之后按照tomcat插件配置父工程的方式配置web工程即可:
 
点击运行箭头即可访问
 http://localhost:8080/allStudent
🔥依赖传递失效及解决方案

 之前的案例中,junit是在父工程当中引入的,如果在dao工程引入,service工程引入dao工程,即 service --> dao --> junit 。按照依赖的传递性,service工程也可以使用junit。可实际情况却是无法使用。这就是依赖传递失效问题。
 
Maven在定义依赖传递时,一些依赖范围的依赖无法传递,表格如下:
 
在实际开发中,不需要记这张表格,遇到依赖没有传递过来时,我们直接在本工程再次添加一遍依赖即可。
<dependencies>
<dependency>
     <groupId>com.itbaizhan</groupId>
	<artifactId>maven_demo2_dao</artifactId>
     <version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
</dependency>
</dependencies>



















