maven-聚合工程

原创
2019/04/24 22:20
阅读数 1

聚合工程

所谓聚合项目,实际上就是对项目分模块,互联网项目一般来说按照业务分(订单模块、VIP模块、支付模块、CMS模块....),传统的软件项目,大多采用分层的方式(Dao、Service、Controller)

一、多模块结构

在这里插入图片描述 在这里插入图片描述

二、创建项目

1.创建sxt-parent

==pom文件不需要写代码,只是起管理作用== pom 在这里插入图片描述

2.创建sxt-manager项目

==指定sxt-parent为其父项目== pom 在这里插入图片描述

3.分别创建四个jar项目

==项目类型为:maven model== jar 在这里插入图片描述

4.创建sxt-manager-web

==项目类型为:maven model== war 在这里插入图片描述

war项目有错误提示,需要在pom.xml中添加servlet 和Tomcat插件

在这里插入图片描述 servlet:

	<!-- 因为是web项目所以需要servlet --> <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>

Tomcat插件:

<build>
		<plugins> <!-- tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration> <!-- 端口号 -->
					<port>8082</port> <!-- /表示访问路径 省略项目名 -->
					<path>/</path> <!-- 设置编码方式 -->
					<uriEncoding>utf-8</uriEncoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
添加web.xml文件:

在这里插入图片描述

三:设置相互依赖

1.依赖关系介绍:

==完整依赖关系:== 在这里插入图片描述 ==控制层的依赖关系:== 在这里插入图片描述 ==service层的依赖关系== 在这里插入图片描述 ==dao层的依赖关系== 在这里插入图片描述

2.实现依赖

2.1添加依赖关系

==在各层的pom.xml中添加依赖== dao pom.xml: 在这里插入图片描述

<!-- 配置对common和Model的依赖 -->
  <dependencies>
  	<dependency>
  		<groupId>com.sxt</groupId>
    	<artifactId>sxt-manager-common</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<dependency>
  		<groupId>com.sxt</groupId>
    	<artifactId>sxt-manager-model</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>

service 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.sxt</groupId>
    <artifactId>sxt-manager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sxt-manager-service</artifactId>
  
  <!-- 添加对dao层的依赖 -->
  <dependencies>
  	<dependency>
  		<groupId>com.sxt</groupId>
    	<artifactId>sxt-manager-dao</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  </dependencies>
</project>

web pom.xml:

<!-- 添加对service的依赖 -->
  <dependencies>
  	<dependency>
  		<groupId>com.sxt</groupId>
    	<artifactId>sxt-manager-service</artifactId>
   	 	<version>0.0.1-SNAPSHOT</version>
  	</dependency>

2.2 项目打包

==先打包sxt-parent,再打包sxt-manager. 注意打包sxt-manager的时候会一起打包它的五个子项目。能一起打包成功是最好的情况,但失败的话就一个一个打包没打包成功的子项目。==

项目打包: 在这里插入图片描述 打包成功: 在这里插入图片描述 打包成功后的本地仓库: 在这里插入图片描述 打包成功的jar包: 在这里插入图片描述

3.Spring和SpringMVC整合

3.1在dao中引入spring

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
   	   <groupId>org.springframework</groupId>
  	   <artifactId>spring-context</artifactId>
   		<version>4.3.21.RELEASE</version>
	</dependency>

  	<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
	<dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
   		 <version>5.1.47</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
	<dependency>
    	<groupId>org.springframework</groupId>
   	 	<artifactId>spring-aop</artifactId>
    	<version>4.3.21.RELEASE</version>
	</dependency>
	
  	<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
	<dependency>
    	<groupId>org.aspectj</groupId>
    	<artifactId>aspectjweaver</artifactId>
    	<version>1.8.9</version>
	</dependency>
  	
  	
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
	<dependency>
    	<groupId>org.springframework</groupId>
   		 <artifactId>spring-orm</artifactId>
   		 <version>4.3.21.RELEASE</version>
	</dependency>

3.2在controller中引入springmvc

在controller中添加的是其他项目不需要用的jar包,比如servlet

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>4.3.21.RELEASE</version>
	</dependency>
		
		<!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
	<dependency>
    	<groupId>javax.servlet.jsp.jstl</groupId>
    	<artifactId>jstl</artifactId>
   		 <version>1.2</version>
	</dependency>
		
		
	<!-- https://mvnrepository.com/artifact/taglibs/standard -->
	<dependency>
    	<groupId>taglibs</groupId>
    	<artifactId>standard</artifactId>
    	<version>1.1.2</version>
	</dependency>
	
	<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
	<dependency>
   		 <groupId>com.alibaba</groupId>
   		 <artifactId>fastjson</artifactId>
  		  <version>1.2.47</version>
	</dependency>

3.3添加Spring和SpringMVC配置文件:

在这里插入图片描述

Spring配置文件:

<?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:aop="http://www.springframework.org/schema/aop"
	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-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.service.impl,com.sxt.dao.impl" use-default-filters="true">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

	<!-- 配置数据源 -->
	<!-- 配置数据源 -->
	<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
		id="dataSource">
		<!-- 配置数据库的相关信息 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://localhost:3306/login?characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="tang123" />
	</bean>


	<!-- 配置JdbcTemplate -->
	<bean class="org.springframework.jdbc.core.JdbcTemplate" id="template">
		<!-- 构造注入关联数据源 -->
		<constructor-arg ref="dataSource"/>
	</bean>
	
</beans>

SpringMVC:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.controller" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 开启SpringMVC注解的方式 -->
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
</beans>

3.4在controller中配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC-01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 这里配置Spring配置文件的位置,
		 param-name是固定的,
		 param-value是文件位置 这个配置可以省略,
		 如果省略, 系统默认去/WEB-INF/目录下查找applicationContext.xml作为Spring的配置文件 -->
		 <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<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>
  	<!-- 容器启动的时候就加载 -->
  	<load-on-startup>2</load-on-startup>
  </servlet>
  <!-- 配置SpringMVC的映射地址 -->
  <servlet-mapping>
  	<servlet-name>SpringMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- spring框架提供的字符集过滤器 -->
	<!-- spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用于解决POST方式造成的中文乱码问题 -->
	<!-- 只能处理post方式的中文乱码问题 -->
	<filter>
		<filter-name>encodingFilter</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>
		<init-param> 
			<param-name>forceRequestEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
		<init-param>
			<param-name>forceResponseEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
  
  <servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
</web-app>

实现查询的操作:

1.在sxt-manager-model中创建User对象

在这里插入图片描述 User:

package com.sxt.pojo;

public class User {
	private int id;
	private String username;
	private String password;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {`在这里插入代码片`
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}

2.在sxt-manager-dao实现数据库操作

在这里插入图片描述 dao:

package com.sxt.dao;

import java.util.List;

import com.sxt.pojo.User;

public interface IUserDao {
	public List<User> query();
}

dao.impl:

package com.sxt.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.sxt.dao.IUserDao;
import com.sxt.pojo.User;
@Repository
public class UserDaoImpl implements IUserDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<User> query() {
		String sql = "select * from t_user";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
	}

}

3.在sxt-manager-service中操作Dao

在这里插入图片描述 service:

package com.sxt.service;

import java.util.List;

import com.sxt.pojo.User;

public interface IUserService {
	public List<User> query();
}

service.impl:

package com.sxt.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sxt.dao.IUserDao;
import com.sxt.pojo.User;
import com.sxt.service.IUserService;

@Service
public class UserServiceImpl implements IUserService {
	
	@Resource
	private IUserDao dao;

	@Override
	public List<User> query() {
		// TODO Auto-generated method stub
		return dao.query();
	}

}

4.在sxt-manager-web中处理请求响应

在这里插入图片描述 controller:

package com.sxt.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.pojo.User;
import com.sxt.service.IUserService;

@RestController
public class UserController {

	@Resource
	private IUserService userService;
	
	@RequestMapping("/query")
	public List<User> query(){
		return userService.query();
	}
}


5.实现:

在这里插入图片描述

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部