前言:
繼上篇文章搭建了一個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.接下來我們訪問一下我們寫的測試接口
整篇文章到這裏就結束了 如果有什麼問題的話 歡迎評論區留言,讓我們一起加油!。