Spring Boot(三):SpringBoot多模块(module)项目搭建

一、前言

  记录在IDEA中怎么搭建SpringBoot多模块(module)的过程,以一个交党费的项目为例,多模块的目录结构如下:

  dajia-dangfei-api:存放实体类和一些service层接口

  dajia-dangfei-provider:存放dao层接口及其实现类和service的实现类

  dajia-dangfei-consumer:存放controller层

  dajia-dangfei-web:存放前端页面等资源

二、搭建步骤

1、创建父工程

① IDEA 工具栏选择菜单 File -> New -> Project...

② 选择Spring Initializr,Initializr默认选择Default,点击Next

③ 填写输入框,点击Next

④ 这步不需要选择直接点Next

⑤ 点击Finish创建项目

⑥ 最终得到的项目目录结构如下

⑦ 删除无用的.mvn目录、src目录、mvnw及mvnw.cmd文件,最终只留.gitignore和pom.xml

2、创建子模块

① 选择项目根目录dajia-dangfei右键呼出菜单,选择New -> Module

② 选择Maven,点击Next

③ 填写ArifactId,点击Next

④ 修改Module name增加横杠提升可读性,点击Finish

⑤ 同理添加dajia-dangfei-provider、dajia-dangfei-consumer子模块,项目目录结构如下图

⑥添加dajia-dangfei-web的directory

⑦最终的目录结构如下

3、运行项目

① 在dajia-dangfei-consumer中创建com.dajia.income包,添加入口类ConsumerApplicationMain.java,并在income包下创建controller包放BillOrderController.java,目录结构如下

ConsumerApplicationMain.java:

@SpringBootApplication
public class ConsumerApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplicationMain.class,args);
    }
}

因为要通过前端访问BillOrderController中的方法,所以引入spring-boot-starter-web的jar包,在父工程的pom文件中定义,子模块自然可以用到

父工程dajia-dangfei的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>dajia-dangfei-api</module>
        <module>dajia-dangfei-provider</module>
        <module>dajia-dangfei-consumer</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dajia</groupId>
    <artifactId>dajia-dangfei</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dajia-dangfei</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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>

引入springboot starter web的jar包

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

</dependencies>

②BillOrderController中添加测试方法

@RestController
@RequestMapping("/billOrder")
public class BillOrderController {

    @RequestMapping("/test")
    public String test(){
        return "hello World!!!";
    }
}

③ 运行ConsumerApplicationMain类中的main方法启动项目,默认端口为8080,访问http://localhost:8080/billOrder/test得到如下效果

以上的项目虽然能启动,但是模块间的依赖关系还没添加,下面继续完善

4、配置模块间的依赖关系

各个子模块的依赖关系:dajia-dangfei-consumer依赖dajia-dangfei-provider,dajia-dangfei-provider依赖dajia-dangfei-api

①在dajia-dangfei-provider中的pom文件中添加dajia-dangfei-api依赖

<dependencies>
        <dependency>
            <artifactId>dajia-dangfei-api</artifactId>
            <groupId>com.dajia</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
</dependencies>

②在dajia-dangfei-consumer中的pom文件中添加dajia-dangfei-provider依赖

<dependencies>
        <dependency>
            <artifactId>dajia-dangfei-provider</artifactId>
            <groupId>com.dajia</groupId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
</dependencies>

5、各模块见得相互调用,从前端到数据库

①dajia-dangfei-api中定义BillOrder实体类、BillOrderService接口

BillOrder.java

public class BillOrder implements Serializable{
    private static final long serialVersionUID = -1764514095260116666L;
    /*
    * 交费单ID
    * */
    private String billOrderID;
    /*
    * 交费单姓名
    * */
    private String personName;
    /*
    * 交费单证件号
    * */
    private String idNumber;
    /*
    * 应交金额
    * */
    private BigDecimal payAmt;
    /*
    * 交费单创建时间
    * */
    private Date createTime;
}

BillOrderService.java

public interface BillOrderService {
    /**
     * 根据交费单ID查找对应的交费单
     * @param billOrderID
     * @return
     */
    BillOrder getBillOrderByID(String billOrderID);

}

dajia-dangfei-api中的目录结构如下

②dajia-dangfei-provider中定义billOrderServiceImpl实现类,dao层接口,数据库连接

数据库连接

  父工程引入mybatis-spring-boot-starter和mysql-connector-java的jar包

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
</dependency>

<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
</dependency>

  resources下配置application.properties文件,主要配置两部分。一个是数据库的连接,一个是mybatis的mapper文件路径

spring.datasource.url=jdbc:mysql://localhost:3306/databasezfy?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

mybatis.config-locations=mybatis/mybatis-config.xml
mybatis.mapper-locations=mybatis/mapper/*.xml

  填写billOrder-mapper.xml中对应的SQL语句

<?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.dajia.income.dao.BillOrderMapper">
    <sql id="base_column">
      billOrderID,personName,idNumber,payAmt,createTime
    </sql>

    <select id="getBillOrderByID" resultType="com.dajia.income.entity.BillOrder" parameterType="string">
        SELECT <include refid="base_column"/>
        FROM billOrder
        WHERE billOrderID = #{billOrderID}
    </select>

</mapper>

dao层接口,@Repository注解是为了在serviceImpl中可以同过@Autowired的方式注入BillOrderMapper

@Repository
public interface BillOrderMapper {

    /**
     * 根据交费单ID查找对应的交费单
     * @param billOrderID
     * @return
     */
    BillOrder getBillOrderByBillOrderID(String billOrderID);
}

billOrderServiceImpl实现类,@Service注解是为了在controller中可以同过@Autowired的方式注入BillOrderService

@Service
public class BillOrderServiceImpl implements BillOrderService{

    @Autowired
    private BillOrderMapper billOrderMapper;

    @Override
    public BillOrder getBillOrderByID(String billOrderID) {
        return billOrderMapper.getBillOrderByBillOrderID(billOrderID);
    }
}

③dajia-dangfei-consumer中通过调用service中的方法实现查询

@RestController
@RequestMapping("/billOrder")
public class BillOrderController {

    @Autowired
    private BillOrderService billOrderService;

    @RequestMapping("/getBillOrderByID")
    public BillOrder getBillOrderByID(){
        return billOrderService.getBillOrderByID("1");
    }
}

运行ConsumerApplicationMain类中的main方法启动项目,会发现报错

Description:

Field billOrderMapper in com.dajia.income.service.impl.BillOrderServiceImpl required a bean of type 'com.dajia.income.dao.BillOrderMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.dajia.income.dao.BillOrderMapper' in your configuration.

原因:@Repository注解是Spring的,Spring在扫描注解时,取消了扫描抽象类和接口,所以无法找到你用@reponsitory注解的dao接口。两种方法解决:

一是用@Mapper注解替代@Repository注解,二是在启动项目时指定要扫描的包,我们这里使用第二种

@SpringBootApplication
@MapperScan("com.dajia.income.dao")
public class ConsumerApplicationMain {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplicationMain.class,args);
    }
}

重新启动,访问http://localhost:8080/billOrder/getBillOrderByID得到如下效果

至此,一个简单的SpringBoot+Mybatis多模块项目已经搭建完毕,我们也通过启动项目调用接口验证其正确性。

 

posted @ 2019-05-02 11:50  吹灭读书灯  阅读(15618)  评论(0编辑  收藏  举报