前言
上一篇我们将多模块的骨架基本搭起来了,接下来我们来看看如何使用:我们还需要再创建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;
}
@Override
public 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 {
@Autowired
PersonMapper personMapper;
@Override
public int addPerson(Person person) {
return personMapper.addPerson(person);
}
@Override
public int deletePerson(int id) {
return personMapper.deletePerson(id);
}
@Override
public int updatePerson(Person person) {
return personMapper.updatePerson(person);
}
@Override
public 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 {
@Autowired
PersonService 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 {
@Autowired
PersonService personService;
/**
* 修改
* **/
@RequestMapping("update/{id}")
@ResponseBody
public 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")
@ResponseBody
public 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 {
@Autowired
PersonService personService;
@RequestMapping("getAll")
@ResponseBody
public 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搭建的多模块项目骨架搭建并测试完毕,当然这只是最基础的配置,根据实际业务的需求,我们可以进行优化,比如引进第三方组件,修改项目的打包形式等等,对于我们来说,多模块可以实现基础代码的复用,从而提高开发效率,文章如有不足之处,欢迎指正。