基于springboot 仓库管理系统出入库实现
时间: 2023-07-11 12:03:48 浏览: 145
实现仓库管理系统的出入库功能,可以分为以下几个步骤:
1. 创建数据库表
根据业务需求,创建相应的数据库表,如仓库表、货物表、出入库记录表等。
2. 定义数据模型
在 Java 中定义相应的数据模型,如仓库、货物、出入库记录等。
3. 编写 DAO 层
使用 Spring Data JPA 或者 MyBatis 等工具编写 DAO 层,实现对数据库的增删改查操作。
4. 编写 Service 层
在 Service 层中实现业务逻辑,并调用 DAO 层提供的方法进行数据操作。
5. 编写 Controller 层
使用 Spring MVC 或者其他框架编写 Controller 层,接收前端请求并调用 Service 层提供的方法进行业务处理,最后返回响应结果。
下面是一个简单的示例:
1. 创建数据库表
```sql
CREATE TABLE warehouse (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
address VARCHAR(100) NOT NULL
);
CREATE TABLE goods (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
type VARCHAR(50) NOT NULL,
quantity INT NOT NULL,
warehouse_id INT NOT NULL,
FOREIGN KEY (warehouse_id) REFERENCES warehouse(id)
);
CREATE TABLE stock_record (
id INT PRIMARY KEY AUTO_INCREMENT,
goods_id INT NOT NULL,
warehouse_id INT NOT NULL,
type VARCHAR(10) NOT NULL,
quantity INT NOT NULL,
time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (goods_id) REFERENCES goods(id),
FOREIGN KEY (warehouse_id) REFERENCES warehouse(id)
);
```
2. 定义数据模型
Warehouse.java
```java
@Entity
@Table(name = "warehouse")
public class Warehouse {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String address;
// getter and setter
}
```
Goods.java
```java
@Entity
@Table(name = "goods")
public class Goods {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String type;
private Integer quantity;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "warehouse_id")
private Warehouse warehouse;
// getter and setter
}
```
StockRecord.java
```java
@Entity
@Table(name = "stock_record")
public class StockRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "goods_id")
private Goods goods;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "warehouse_id")
private Warehouse warehouse;
private String type;
private Integer quantity;
private Date time;
// getter and setter
}
```
3. 编写 DAO 层
WarehouseRepository.java
```java
@Repository
public interface WarehouseRepository extends JpaRepository<Warehouse, Integer> {
}
```
GoodsRepository.java
```java
@Repository
public interface GoodsRepository extends JpaRepository<Goods, Integer> {
List<Goods> findByWarehouseId(Integer warehouseId);
}
```
StockRecordRepository.java
```java
@Repository
public interface StockRecordRepository extends JpaRepository<StockRecord, Integer> {
List<StockRecord> findByGoodsId(Integer goodsId);
List<StockRecord> findByWarehouseId(Integer warehouseId);
}
```
4. 编写 Service 层
WarehouseService.java
```java
@Service
@Transactional
public class WarehouseService {
@Autowired
private WarehouseRepository warehouseRepository;
public List<Warehouse> findAll() {
return warehouseRepository.findAll();
}
public Warehouse findById(Integer id) {
return warehouseRepository.findById(id).orElse(null);
}
public void save(Warehouse warehouse) {
warehouseRepository.save(warehouse);
}
public void deleteById(Integer id) {
warehouseRepository.deleteById(id);
}
}
```
GoodsService.java
```java
@Service
@Transactional
public class GoodsService {
@Autowired
private GoodsRepository goodsRepository;
public List<Goods> findAll() {
return goodsRepository.findAll();
}
public Goods findById(Integer id) {
return goodsRepository.findById(id).orElse(null);
}
public void save(Goods goods) {
goodsRepository.save(goods);
}
public void deleteById(Integer id) {
goodsRepository.deleteById(id);
}
public List<Goods> findByWarehouseId(Integer warehouseId) {
return goodsRepository.findByWarehouseId(warehouseId);
}
}
```
StockRecordService.java
```java
@Service
@Transactional
public class StockRecordService {
@Autowired
private StockRecordRepository stockRecordRepository;
public void save(StockRecord stockRecord) {
stockRecordRepository.save(stockRecord);
}
public List<StockRecord> findByGoodsId(Integer goodsId) {
return stockRecordRepository.findByGoodsId(goodsId);
}
public List<StockRecord> findByWarehouseId(Integer warehouseId) {
return stockRecordRepository.findByWarehouseId(warehouseId);
}
}
```
5. 编写 Controller 层
WarehouseController.java
```java
@RestController
@RequestMapping("/api/warehouse")
public class WarehouseController {
@Autowired
private WarehouseService warehouseService;
@GetMapping("/")
public List<Warehouse> findAll() {
return warehouseService.findAll();
}
@GetMapping("/{id}")
public Warehouse findById(@PathVariable("id") Integer id) {
return warehouseService.findById(id);
}
@PostMapping("/")
public void save(@RequestBody Warehouse warehouse) {
warehouseService.save(warehouse);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") Integer id) {
warehouseService.deleteById(id);
}
}
```
GoodsController.java
```java
@RestController
@RequestMapping("/api/goods")
public class GoodsController {
@Autowired
private GoodsService goodsService;
@GetMapping("/")
public List<Goods> findAll() {
return goodsService.findAll();
}
@GetMapping("/{id}")
public Goods findById(@PathVariable("id") Integer id) {
return goodsService.findById(id);
}
@PostMapping("/")
public void save(@RequestBody Goods goods) {
goodsService.save(goods);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") Integer id) {
goodsService.deleteById(id);
}
@GetMapping("/warehouse/{id}")
public List<Goods> findByWarehouseId(@PathVariable("id") Integer warehouseId) {
return goodsService.findByWarehouseId(warehouseId);
}
}
```
StockRecordController.java
```java
@RestController
@RequestMapping("/api/stock-record")
public class StockRecordController {
@Autowired
private StockRecordService stockRecordService;
@PostMapping("/")
public void save(@RequestBody StockRecord stockRecord) {
stockRecordService.save(stockRecord);
}
@GetMapping("/goods/{id}")
public List<StockRecord> findByGoodsId(@PathVariable("id") Integer goodsId) {
return stockRecordService.findByGoodsId(goodsId);
}
@GetMapping("/warehouse/{id}")
public List<StockRecord> findByWarehouseId(@PathVariable("id") Integer warehouseId) {
return stockRecordService.findByWarehouseId(warehouseId);
}
}
```
这样,一个基于 Spring Boot 的仓库管理系统出入库功能就实现了。你可以根据自己的业务需求,对上述代码进行修改和扩展。
阅读全文