前言

上一篇我们将多模块的骨架基本搭起来了,接下来我们来看看如何使用:我们还需要再创建3个子模块,我们通过客户端添加数据、管理端修改数据、移动端获取数据的场景来模拟具体的业务系统业务子模块如下:weige-fornt(客户端)、weige-managener(管理端)、manager-mobile(移动端);创建过程与weige-domain相同,需要添加web依赖

1.创建三个子系统,成功后项目结构如下:


修改front、manager、mobile三个模块中pom文件的父级依赖

<parent><groupId>com</groupId><artifactId>weige-parent</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent>

并在weige-parent.xml中添加三个子模块的声明:

<!--在父工程中声明子模块--><modules><module>weige-domain</module><module>weige-dao</module><module>weige-service</module><module>weige-front</module><module>weige-manager</module><module>weige-mobile</module></modules>
2.添加测试用的实体类,以及对应的mapper、service

(1)weige-domain模块中新建entity包,包下创建Person.java文件

package com.weige.entity;public class Person {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person{" +"id=" + id +", name='" + name + '\'' +'}';}
}

(2)weige-mapper模块中新建mapper包,包下创建PersonMapper.java文件和PersonMapper.xml文件(该文件位于resource下的mapper文件夹下)

package com.weige.mapper;import com.weige.entity.Person;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface PersonMapper {int addPerson(Person person);int deletePerson(int id);int updatePerson(Person person);List<Person> getAll();
}

PersonMapper.xml文件位置在这里

<?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="com.weige.mapper.PersonMapper"><!-- 通用查询映射结果 --><resultMap id="BaseResultMap" type="com.weige.entity.Person"><id column="id" property="id" /><result column="name" property="name" /></resultMap><!-- 通用查询结果列 --><sql id="Base_Column_List">id,name</sql><select id="getAll" resultType="Person">select * from person</select><insert id="addPerson" parameterType="Person">insert into person (name) values (#{name})</insert><delete id="deletePerson" parameterType="integer">delete from person where id= #{id}</delete><update id="updatePerson" parameterType="Person">update person set name =#{name} where id=#{id}</update>
</mapper>

(3)weige-service模块中新建service包,包下创建Personservice.java和PersonserviceImpl.java;

package com.weige.service;import com.weige.entity.Person;import java.util.List;public interface PersonService {int addPerson(Person person);int deletePerson(int id);int updatePerson(Person person);List<Person> getAll();
}

PersonserviceImpl.java;

package com.weige.service.impl;import com.weige.entity.Person;
import com.weige.mapper.PersonMapper;
import com.weige.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
@Service
public class PersonServiceImpl implements PersonService {@AutowiredPersonMapper personMapper;@Overridepublic int addPerson(Person person) {return personMapper.addPerson(person);}@Overridepublic int deletePerson(int id) {return personMapper.deletePerson(id);}@Overridepublic int updatePerson(Person person) {return personMapper.updatePerson(person);}@Overridepublic List<Person> getAll() {return personMapper.getAll();}
}
3.配置weige-front项目,pom.xml中添加weige-service的依赖
<!-- 添加 weige-service 的依赖 --><dependency><groupId>com</groupId><artifactId>weige-service</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency>

在weige-front中新建controller包,包下创建PersonController.java

package com.weige.controller;import com.weige.entity.Person;
import com.weige.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.List;@Controller
@RequestMapping("/front")
public class PersonController {@AutowiredPersonService personService;@ResponseBody@RequestMapping("add/{name}")private String add(@PathVariable(value="name") String name){Person person = new Person();person.setName(name);personService.addPerson(person);return "success";}}

weige-front的配置文件application.properties:

#端口
server.port=8092
#数据库,本地
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#日志打印
logging.level.com.weige.mapper=debug
#mybatis配置
#指定路径,扫描时可以加载到该路径下的xml文件
mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml
#指定类的别名,可以在mapper.xml的查询中直接使用类名而无需写明全路径
mybatis.typeAliasesPackage=com.weige.entity
#使全局的映射器启用或禁用缓存
mybatis.configuration.cache-enabled=false
#将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true

这里有一个细节,需要把项目的启动类所在的位置往上移动一层,否则启动时候因为加载机制,可能会因为找不到某个组件而报错,

4.将front项目运行起来,访问添加接口



查询数据库,数据已经添加成功

weige-front的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><!--在子模块中声明父级依赖--><parent><groupId>com</groupId><artifactId>weige-parent</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com</groupId><artifactId>weige-front</artifactId><version>0.0.1-SNAPSHOT</version><name>weige-front</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!-- 添加 weige-service 模块的依赖 --><dependency><groupId>com</groupId><artifactId>weige-service</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

模拟客户端添加功能已经实现
管理端weige-manager配置文件除了端口之外,其他与front一致
application.properties

#管理端端口
server.port=8093
#数据库,本地
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#日志打印
logging.level.com.weige.mapper=debug
#mybatis配置
#指定路径,扫描时可以加载到该路径下的xml文件
mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml
#指定类的别名,可以在mapper.xml的查询中直接使用类名而无需写明全路径
mybatis.typeAliasesPackage=com.weige.entity
#使全局的映射器启用或禁用缓存
mybatis.configuration.cache-enabled=false
#将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true

将启动类往上移动一级,同样新建controller包,在包下新建PersonManagerController.java:

package com.weige.controller;import com.weige.entity.Person;
import com.weige.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.HashMap;
import java.util.Map;@Controller
@RequestMapping("/manager")
public class PersonManagerController {@AutowiredPersonService personService;/*** 修改* **/@RequestMapping("update/{id}")@ResponseBodypublic Object updatePerson(@PathVariable(value = "id") int id) {Map<String,Object> ret=new HashMap<String,Object>();ret.put("retCode","0001");ret.put("retMsg","失败");int con=0;Person person=new Person();person.setId(id);person.setName("我的名字被修改了");con=personService.updatePerson(person);if(con>0){ret.put("retCode","0000");ret.put("retMsg","成功");}return ret;}@RequestMapping("hello")@ResponseBodypublic Object helllo(){return "hello wang !";}
}

启动项目,访问8093端口:
修改之前数据库信息


修改后

移动端weige-mobile新建controller包,包下新建PersonAppController,配置文件application.properties修改端口为8094,其他与管理端一致,
PersonAppController.java:

package com.weige.controller;import com.weige.entity.Person;
import com.weige.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Controller
@RequestMapping("/app")
public class PersonAppController {@AutowiredPersonService personService;@RequestMapping("getAll")@ResponseBodypublic Object getAll(){List<Person> list=new ArrayList<>();Map<String,Object> ret=new HashMap<String,Object>();ret.put("retCode","0001");ret.put("retMsg","失败");list=personService.getAll();if (list.size()>0){ret.put("retCode","0000");ret.put("retMsg","成功");ret.put("data",list);}return ret;}
}

启动项目,访问8094端口:
至此,基于springboot搭建的多模块项目骨架搭建并测试完毕,当然这只是最基础的配置,根据实际业务的需求,我们可以进行优化,比如引进第三方组件,修改项目的打包形式等等,对于我们来说,多模块可以实现基础代码的复用,从而提高开发效率,文章如有不足之处,欢迎指正。

创建springboot多模块项目(下)相关推荐

  1. 【记录】idea创建springboot多模块项目

    创建maven项目后删除src文件目录 将pom.xml文件修改如下: <?xml version="1.0" encoding="UTF-8"?> ...

  2. 创建springboot多模块项目

    https://www.hangge.com/blog/cache/detail_2833.html

  3. springboot 多模块项目构建【创建√ + 启动√ 】

    一.多模块项目构建 1. 先建立父级目录demo-parent 2. 把父级目录src删除,再建立子级模块 3. 建立子级模块model,dao,service,common.utils等相同步骤 4 ...

  4. eclipse创建maven多模块项目(单个类似)

    2019独角兽企业重金招聘Python工程师标准>>> 1.下载安装maven 1.1.下载 注意:maven的版本,要根据你的jdk版本来下载.要不会安装失败,提示版本问题哦 Jd ...

  5. eclipse创建springboot项目_创建SpringBoot自动配置项目:Starter测试使用

    Starter 测试使用 完成了 starter 项目的创建.发布之后,在 Spring Boot 项目中便可以直接使用了,下面简单介绍一-下 Starter 测试使用步骤,其中省略掉了 Spring ...

  6. 创建springboot+mybatis+mysql项目

    创建springboot+mybatis+mysql项目的源码https://download.csdn.net/download/qq_34297287/85245127可以点击上方下载创建spri ...

  7. springboot多模块项目创建及添加子模块过程

    问题产生:之前没有自己创建过多模块项目导致 首先创建一个project.这里选择maven项目,一般父模块就是一个容器,把子模块给管理起来,所以直接创建一个空的maven项目就行.创建模块时,骨架的选 ...

  8. SpringBoot 精通系列-创建SpringBoot的入门项目

    导语   在之前的博客中介绍过一些关于SpringBoot的使用方式,对于SpringBoot来说是一个全新的框架,它出现的目的是用来简化新的Spring应用的初始搭建以及开发过程.通过特殊的控制方式 ...

  9. gitlab-ci docker maven 自动化流水线部署 springboot多模块项目

    一.准备 首先 需要两台服务器(这里为了下面方便理解,我们约定这两台服务器地址.名称和系统) 1.gitlab 服务器 服务器A(地址10.10.10.7)(内存大于4g不然会一直死)( CentOS ...

最新文章

  1. 机器人学习--Mobile robot国内外优秀实验室
  2. Introduction to Cryto Crptocurrencies Lecture 1
  3. OpenSuSe使用相关
  4. alias--linux
  5. 【Android】自定义环形菜单View
  6. 万兆以太网测试仪应该具备什么功能
  7. web 后台返回json格式数据的方式(status 406)
  8. 想转行人工智能?哈佛博士后有话说!
  9. 使用request获取访问者的真实IP
  10. Think PHP(TP)框架基础知识
  11. 航天信息上传参数设置服务器设置,金税盘上传参数怎么设置?
  12. ong拼音汉字_儿童拼音汉字入门
  13. 解决双启动GRLDR missing故障的方法
  14. 基于神经网络的目标检测论文之结尾:总结与展望
  15. BIGEMAP如何发布百度离线地图及二次开发API
  16. 和专业计算机男生谈恋爱,和不同专业的男生谈恋爱是什么感觉?
  17. 正则表达式之前瞻后顾
  18. JavaScript制作网页时钟
  19. ERP系统类毕业论文文献都有哪些?
  20. 利用TVS及1R电阻保护后级电路

热门文章

  1. ubuntu 系统狠慢 或者很卡的原因
  2. 百度wz搜索竞价推广关键词转化成本计算
  3. 为android模拟器加速
  4. 用户管理“明星”工具——在线客服系统
  5. matlab怎么复数相位,怎么求复数相位
  6. linux去除重复字符,Linux去除重复项命令uniq
  7. excel怎么设置打印区域_excel:将多个表格的不同区域打印在一张纸上
  8. 你所需要的java基础提升篇大总结
  9. 微型计算机的五大硬件组成,计算机系统的组成,计算机硬件的五大部分是什么...
  10. Unity3d自学之路(一)