• maven第二天 ---聚合工程


    maven第二天   ---聚合工程

    目录

    一、聚合工程

    1.创建父工程

    2.创建子工程(模块)

    ​编辑 

    二、搭建ssm框架(xml版)

    1.创建父工程

    2.创建子工程(maven-generator)逆向工程

    3.创建子工程(maven-common)工具模块

    4.实体类(maven-entity)

    5.业务接口(maven-service-api)

    6.业务实现和mapper层实现(maven-service)

    7.web工程(maven-web) 即controller层

    注意 


    一、聚合工程?

    分布式架构或微服务架构具有继承关系的工程。优点:可以统一对依赖进行管理

    1.创建父工程

    packaging必须是pom,管理依赖版本及子工程,不需要真实的依赖的引用

    一般使用depengdecyManagement管理

    2.创建子工程(模块)

     

    二、搭建ssm框架(xml版)

    一般分布式架构是分两个情况进行拆分 ---1.按照功能拆分   2.按照性能拆分

    1.创建父工程

    主要设置一些共同要用的的依赖的版本设置

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.sofwingroupId>
    6. <artifactId>maven-ssmartifactId>
    7. <packaging>pompackaging>
    8. <version>1.0-SNAPSHOTversion>
    9. <modules>
    10. <module>maven-entitymodule>
    11. <module>maven-common1module>
    12. <module>maven-generatormodule>
    13. <module>maven-service-apimodule>
    14. <module>maven-servicemodule>
    15. <module>maven-webmodule>
    16. modules>
    17. <properties>
    18. <spring-version>5.3.19spring-version>
    19. <mybaits-version>3.5.9mybaits-version>
    20. <druid-version>1.2.8druid-version>
    21. <mysql-version>8.0.28mysql-version>
    22. <mybatis-spring-version>2.0.7mybatis-spring-version>
    23. <mybatis-generator-version>1.3.7mybatis-generator-version>
    24. <pageHelper-version>5.2.0pageHelper-version>
    25. properties>
    26. <dependencyManagement>
    27. <dependencies>
    28. <dependency>
    29. <groupId>org.springframeworkgroupId>
    30. <artifactId>spring-webmvcartifactId>
    31. <version>${spring-version}version>
    32. dependency>
    33. <dependency>
    34. <groupId>org.mybatisgroupId>
    35. <artifactId>mybatisartifactId>
    36. <version>${mybaits-version}version>
    37. dependency>
    38. <dependency>
    39. <groupId>com.alibabagroupId>
    40. <artifactId>druidartifactId>
    41. <version>${druid-version}version>
    42. dependency>
    43. <dependency>
    44. <groupId>mysqlgroupId>
    45. <artifactId>mysql-connector-javaartifactId>
    46. <version>${mysql-version}version>
    47. dependency>
    48. <dependency>
    49. <groupId>org.mybatisgroupId>
    50. <artifactId>mybatis-springartifactId>
    51. <version>${mybatis-spring-version}version>
    52. dependency>
    53. <dependency>
    54. <groupId>org.mybatis.generatorgroupId>
    55. <artifactId>mybatis-generator-coreartifactId>
    56. <version>${mybatis-generator-version}version>
    57. dependency>
    58. <dependency>
    59. <groupId>com.github.pagehelpergroupId>
    60. <artifactId>pagehelperartifactId>
    61. <version>${pageHelper-version}version>
    62. dependency>
    63. dependencies>
    64. dependencyManagement>
    65. project>

    2.创建子工程(maven-generator)逆向工程

    主要是逆向工程生产pojo和mapper

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>maven-ssmartifactId>
    6. <groupId>com.sofwingroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <description>逆向工程description>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>maven-generatorartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>mysqlgroupId>
    15. <artifactId>mysql-connector-javaartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>org.mybatis.generatorgroupId>
    19. <artifactId>mybatis-generator-coreartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>org.mybatisgroupId>
    23. <artifactId>mybatisartifactId>
    24. dependency>
    25. dependencies>
    26. <build>
    27. <plugins>
    28. <plugin>
    29. <groupId>org.mybatis.generatorgroupId>
    30. <artifactId>mybatis-generator-maven-pluginartifactId>
    31. <version>1.3.7version>
    32. <dependencies>
    33. <dependency>
    34. <groupId>mysqlgroupId>
    35. <artifactId>mysql-connector-javaartifactId>
    36. <version>8.0.28version>
    37. dependency>
    38. dependencies>
    39. plugin>
    40. plugins>
    41. build>
    42. project>

    逆向工程的xml配置--必须是generatorConfig.xml的名字 并且在resources中

    1. generatorConfiguration PUBLIC
    2. "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    3. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    4. <generatorConfiguration>
    5. <context id="simple" targetRuntime="MyBatis3">
    6. <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
    7. connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&nullCatalogMeansCurrent=true"
    8. userId="root"
    9. password="root" />
    10. <javaModelGenerator targetPackage="com.sofwin.pojo" targetProject="..\maven-entity\src\main\java"/>
    11. <sqlMapGenerator targetPackage="com.sofwin.mapper" targetProject="..\maven-service\src\main\java"/>
    12. <javaClientGenerator type="XMLMAPPER" targetPackage="com.sofwin.mapper" targetProject="..\maven-service\src\main\java"/>
    13. <table tableName="b_user" />
    14. context>
    15. generatorConfiguration>

     

    3.创建子工程(maven-common)工具模块

    工具模块主要就是一些工具类,这里我们没有就不设置了

    4.实体类(maven-entity)

    逆向工程自动生成

    5.业务接口(maven-service-api)

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>maven-ssmartifactId>
    6. <groupId>com.sofwingroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <description>service接口description>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>maven-service-apiartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>com.sofwingroupId>
    15. <artifactId>maven-entityartifactId>
    16. <version>1.0-SNAPSHOTversion>
    17. dependency>
    18. <dependency>
    19. <groupId>com.github.pagehelpergroupId>
    20. <artifactId>pagehelperartifactId>
    21. dependency>
    22. dependencies>
    23. project>

    在接口中写一些方法

    1. package com.sofwin;
    2. import com.github.pagehelper.PageInfo;
    3. import com.sofwin.pojo.BUser;
    4. /**
    5. * @author : wentao
    6. * @version : 1.0
    7. */
    8. public interface UserService {
    9. /**
    10. * 验证登录
    11. * @param user
    12. * @return 如果返回为空 登录失败 返回不为空登录成功并返回用户的数据库信息
    13. */
    14. BUser checkLogin(BUser user);
    15. /**
    16. * 根据条件查询用户分页信息
    17. * @param user
    18. * @return
    19. */
    20. PageInfo selectUsers(BUser user, Integer pageNumber , Integer pageSize) ;
    21. /**
    22. * 根据id进行查询用户信息
    23. * @param id
    24. * @return
    25. */
    26. BUser selectById(Integer id);
    27. /**
    28. * 保存
    29. * @param user
    30. * @return true操作成功 false操作失败
    31. */
    32. boolean saveOrUpdate(BUser user);
    33. /**
    34. * 根据id来删除用户信息
    35. * @param id
    36. * @return true操作成功 false操作失败
    37. */
    38. boolean removeById(Integer id);
    39. /**
    40. * 批量删除
    41. * @param ids
    42. * @return true操作成功 false操作失败
    43. */
    44. boolean removeByIds(String[] ids);
    45. }

    6.业务实现和mapper层实现(maven-service)

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>maven-ssmartifactId>
    6. <groupId>com.sofwingroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <description>service实现类和dao接口description>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>maven-serviceartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>com.sofwingroupId>
    15. <artifactId>maven-service-apiartifactId>
    16. <version>1.0-SNAPSHOTversion>
    17. dependency>
    18. <dependency>
    19. <groupId>org.mybatisgroupId>
    20. <artifactId>mybatisartifactId>
    21. dependency>
    22. <dependency>
    23. <groupId>org.mybatisgroupId>
    24. <artifactId>mybatis-springartifactId>
    25. dependency>
    26. <dependency>
    27. <groupId>org.springframeworkgroupId>
    28. <artifactId>spring-contextartifactId>
    29. <version>${spring-version}version>
    30. dependency>
    31. <dependency>
    32. <groupId>mysqlgroupId>
    33. <artifactId>mysql-connector-javaartifactId>
    34. dependency>
    35. <dependency>
    36. <groupId>com.alibabagroupId>
    37. <artifactId>druidartifactId>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframeworkgroupId>
    41. <artifactId>spring-jdbcartifactId>
    42. <version>${spring-version}version>
    43. dependency>
    44. <dependency>
    45. <groupId>org.springframeworkgroupId>
    46. <artifactId>spring-testartifactId>
    47. <version>${spring-version}version>
    48. dependency>
    49. <dependency>
    50. <groupId>junitgroupId>
    51. <artifactId>junitartifactId>
    52. <version>4.12version>
    53. <scope>testscope>
    54. dependency>
    55. <dependency>
    56. <groupId>org.apache.logging.log4jgroupId>
    57. <artifactId>log4j-coreartifactId>
    58. <version>2.17.2version>
    59. dependency>
    60. dependencies>
    61. <build>
    62. <resources>
    63. <resource>
    64. <directory>src\main\java\directory>
    65. <includes>
    66. <include>**\*.xmlinclude>
    67. <include>**\*.propertiesinclude>
    68. includes>
    69. resource>
    70. <resource>
    71. <directory>src\main\resources\directory>
    72. <includes>
    73. <include>**\*.xmlinclude>
    74. <include>**\*.propertiesinclude>
    75. includes>
    76. resource>
    77. resources>
    78. build>
    79. project>

    resources文件夹中的配置文件

    applicationContext-mybatis.xml  spring的配置文件

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.sofwin">context:component-scan>
    7. <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    8. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    9. <property name="driverClassName" value="${jdbc.driver}"/>
    10. <property name="url" value="${jdbc.url}"/>
    11. <property name="username" value="${jdbc.username}"/>
    12. <property name="password" value="${jdbc.pwd}"/>
    13. bean>
    14. <bean id="sessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
    15. <property name="dataSource" ref="dataSource"/>
    16. <property name="configLocation" value="classpath:config.xml"/>
    17. bean>
    18. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    19. <property name="sqlSessionFactoryBeanName" value="sessionFactoryBean"/>
    20. <property name="basePackage" value="com.sofwin.mapper"/>
    21. bean>
    22. beans>

     config.xml   mybatis的全局配置文件

    1. configuration
    2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    4. <configuration>
    5. <properties resource="jdbc.properties">
    6. properties>
    7. <settings>
    8. <setting name="logImpl" value="LOG4J2"/>
    9. settings>
    10. <typeAliases>
    11. <package name="com.sofwin.pojo"/>
    12. typeAliases>
    13. <plugins>
    14. <plugin interceptor="com.github.pagehelper.PageInterceptor">
    15. <property name="helperDialect" value="mysql"/>
    16. plugin>
    17. plugins>
    18. <environments default="dev">
    19. <environment id="dev">
    20. <transactionManager type="JDBC">transactionManager>
    21. <dataSource type="POOLED">
    22. <property name="driver" value="${jdbc.driver}"/>
    23. <property name="url" value="${jdbc.url}"/>
    24. <property name="username" value="${jdbc.username}"/>
    25. <property name="password" value="${jdbc.pwd}"/>
    26. dataSource>
    27. environment>
    28. environments>
    29. <mappers>
    30. <package name="com.sofwin.mapper"/>
    31. mappers>
    32. configuration>

    jdbc.properties  

    1. jdbc.driver=com.mysql.cj.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&nullCatalogMeansCurrent=true
    3. jdbc.username=root
    4. jdbc.pwd=root

     log4j2.xml

    1. <Configuration status="debug"
    2. monitorInterval="600">
    3. <Appenders>
    4. <Console name="Console"
    5. target="SYSTEM_OUT">
    6. <PatternLayout
    7. pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t]
    8. %-5level [%L] - %msg%n" />
    9. Console>
    10. Appenders>
    11. <Loggers>
    12. <Root level="DEBUG">
    13. <AppenderRef ref="Console" />
    14. Root>
    15. Loggers>
    16. Configuration>

     UserServiceImpl

    1. package com.sofwin.service.impl;
    2. import com.github.pagehelper.PageHelper;
    3. import com.github.pagehelper.PageInfo;
    4. import com.sofwin.UserService;
    5. import com.sofwin.mapper.BUserMapper;
    6. import com.sofwin.pojo.BUser;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import java.util.List;
    10. /**
    11. * @author : wentao
    12. * @version : 1.0
    13. */
    14. @Service
    15. public class UserServiceImpl implements UserService {
    16. @Autowired
    17. private BUserMapper mapper;
    18. public BUser checkLogin(BUser user) {
    19. return null;
    20. }
    21. public PageInfo selectUsers(BUser user, Integer pageNumber, Integer pageSize) {
    22. PageHelper.startPage(pageNumber,pageSize);
    23. List bUsers = mapper.selectByExample(null);
    24. return new PageInfo(bUsers);
    25. }
    26. public BUser selectById(Integer id) {
    27. return null;
    28. }
    29. public boolean saveOrUpdate(BUser user) {
    30. return false;
    31. }
    32. public boolean removeById(Integer id) {
    33. return false;
    34. }
    35. public boolean removeByIds(String[] ids) {
    36. return false;
    37. }
    38. }

    这里要测试一下是否成功

    1. package com.sofwin.test;
    2. import com.github.pagehelper.PageInfo;
    3. import com.sofwin.UserService;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. /**
    10. * @author : wentao
    11. * @version : 1.0
    12. */
    13. @RunWith(SpringJUnit4ClassRunner.class)
    14. @ContextConfiguration(locations = "classpath:applicationContext-mybatis.xml")
    15. public class TestService {
    16. @Autowired private UserService service;
    17. @Test
    18. public void test01(){
    19. PageInfo pageInfo = service.selectUsers(null, 1, 10);
    20. System.out.println(pageInfo);
    21. }
    22. }

    7.web工程(maven-web) 即controller层

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>maven-ssmartifactId>
    6. <groupId>com.sofwingroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <description>controller 控制层description>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>maven-webartifactId>
    12. <packaging>warpackaging>
    13. <dependencies>
    14. <dependency>
    15. <groupId>com.sofwingroupId>
    16. <artifactId>maven-serviceartifactId>
    17. <version>1.0-SNAPSHOTversion>
    18. dependency>
    19. <dependency>
    20. <groupId>javax.servletgroupId>
    21. <artifactId>servlet-apiartifactId>
    22. <version>2.5version>
    23. <scope>providedscope>
    24. dependency>
    25. <dependency>
    26. <groupId>org.springframeworkgroupId>
    27. <artifactId>spring-webmvcartifactId>
    28. dependency>
    29. <dependency>
    30. <groupId>com.fasterxml.jackson.coregroupId>
    31. <artifactId>jackson-databindartifactId>
    32. <version>2.13.2version>
    33. dependency>
    34. dependencies>
    35. <build>
    36. <plugins>
    37. <plugin>
    38. <groupId>org.apache.tomcat.mavengroupId>
    39. <artifactId>tomcat7-maven-pluginartifactId>
    40. <version>2.2version>
    41. <configuration>
    42. <path>/mavenssmpath>
    43. configuration>
    44. plugin>
    45. plugins>
    46. build>
    47. project>

    contextConfigration-mvc.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:mvc="http://www.springframework.org/schema/mvc"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    5. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    6. <mvc:annotation-driven>mvc:annotation-driven>
    7. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    8. <property name="prefix" value="/WEB-INF/"/>
    9. <property name="suffix" value=".jsp"/>
    10. bean>
    11. beans>

    web.xml 设置前端控制器

    1. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    2. <servlet>
    3. <servlet-name>maven-webservlet-name>
    4. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    5. <init-param>
    6. <param-name>contextConfigLocationparam-name>
    7. <param-value>classpath:applicationContext-mybatis.xml,
    8. classpath:contextConfigration-mvc.xml
    9. param-value>
    10. init-param>
    11. <load-on-startup>1load-on-startup>
    12. servlet>
    13. <servlet-mapping>
    14. <servlet-name>maven-webservlet-name>
    15. <url-pattern>/url-pattern>
    16. servlet-mapping>
    17. web-app>

     controller

    1. package com.sofwin.controller;
    2. import com.github.pagehelper.PageInfo;
    3. import com.sofwin.UserService;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Controller;
    6. import org.springframework.web.bind.annotation.GetMapping;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.ResponseBody;
    9. /**
    10. * @author : wentao
    11. * @version : 1.0
    12. */
    13. @Controller
    14. @RequestMapping("/user")
    15. public class UserController {
    16. @Autowired
    17. private UserService service;
    18. @ResponseBody
    19. @GetMapping("/test01")
    20. public PageInfo test01(){
    21. PageInfo pageInfo = service.selectUsers(null, 1, 5);
    22. return pageInfo;
    23. }
    24. @GetMapping("/test02")
    25. public String test02(){
    26. PageInfo pageInfo = service.selectUsers(null, 1, 5);
    27. return "index";
    28. }
    29. }

    注意 

    这里我们导入自定义的依赖,现在在本地仓库还是没有的,我们要将他们全部加入到本地仓库中

    这里我们直接使用父模块的声明周期的install即可全部加入到本地仓库


  • 相关阅读:
    [Usaco2015 dec]Max Flow 树上差分
    设计模式之观察者模式学习笔记
    TypeError: lineplot() takes from 0 to 1 positional arguments but 2 were given
    Redis知识-基础篇
    Spock单元测试框架介绍及在美团优选的实践_第二章(static静态方法mock方式)
    Python 项目一 数据可视化 02
    Jmeter进阶使用指南-使用断言
    Spring -Spring之依赖注入源码解析(下)
    ELK高级搜索,深度详解ElasticStack技术栈-上篇
    【Linux之Shell脚本实战】Centos最小化安装环境配置脚本
  • 原文地址:https://blog.csdn.net/weixin_52574640/article/details/126024506