SpringBoot 多模块的小例子

github地址:https://github.com/tanghh0410/module_demo.git

前言:

继上篇文章搭建了一个SpringBoot 分模块项目例子以后,这节我们在此基础上写一个小例子。

整个项目结构如下图:

 

 1.准备一张表

2.在demo_dao 这个项目里建一个实体类 和一个JPA

BosUserModel 

package demo.demo_dao;

import javax.persistence.*;
import java.util.Objects;

/**
    用户表
 * @Author: tanghh18
 */
@Entity
@Table(name = "bos_user", schema = "test3", catalog = "")
public  class BosUserModel  {

    private Integer id;
    private String name;
    private String password;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }



    @Basic
    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BosUserModel that = (BosUserModel) o;
        return id == that.id &&
                Objects.equals(name, that.name) &&
                Objects.equals(password, that.password);

    }

    @Override
    public int hashCode() {
        return Objects.hash(id,name, password);
    }
}

BosUserJPARepositoty

package demo.demo_dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * @Author: tanghh18
 * @Date: 2020/6/28 15:10
 */
public interface BosUserJPARepositoty extends JpaRepository<BosUserModel, Integer>, JpaSpecificationExecutor<BosUserModel> {

}

3. 在demo_service 这个模块下编写业务逻辑方法。

BosUserService 

package demo.demo_service;

import demo.demo_dao.BosUserModel;

import java.util.List;

/**
 * @Author tanghh
 * @Date 2020/6/28 15:11
 */
public interface BosUserService {
    /**
     * 查询所有人员数据
     */
    List<BosUserModel> listBosUserData();
}

BosUserServiceImpl

package demo.demo_service.impl;

import demo.demo_dao.BosUserJPARepositoty;
import demo.demo_dao.BosUserModel;
import demo.demo_service.BosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author tanghh
 * @Date 2020/6/28 15:12
 */
@Service
public class BosUserServiceImpl implements BosUserService {

    @Autowired
    private BosUserJPARepositoty bosUserJPARepositoty;
    /**
     * 查询所有的人员数据
     */
    @Override
    public List<BosUserModel> listBosUserData() {
     return bosUserJPARepositoty.findAll();
    }
}

4. 在demo_web 这个项目下面建一个控制层 DemoController

package demo.demo_web;

import demo.demo_dao.BosUserModel;
import demo.demo_service.BosUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author tanghh
 * @Date 2020/6/28 15:21
 */
@RestController
public class DemoController {
   @Autowired
   private BosUserService bosUserService;

    @GetMapping(value = "/listAllBosUserData")
    public List<BosUserModel> listAllBosUserData(){
     return bosUserService.listBosUserData();
    }
}

可以看到我无法引入 demo_service下的类,刚开始我以为是这个Service这个类没有加上@Service 这个注解,后面通过百度发现启动类上加上 @ComponentScan("对应的包名") 即可,但是我试了一下在这里还是有点行不通,另一种办法就是移动这个启动类的位置


 

 

5.移动启动类的位置如下:

将这个启动类移动到demo包下即可。

 

移动启动类以后 发现引入的值没有报错了。

 

6.接下来我们就可以启动这个项目了。

看到jvm running 多少多少秒 说明就成功了 

7.接下来我们访问一下我们写的测试接口

 

整篇文章到这里就结束了 如果有什么问题的话 欢迎评论区留言,让我们一起加油!。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章