SSM(Spring、SpringMVC、MyBatis)是一种流行的Java开发框架,用于构建企业级应用程序。在实现简易的发票管理系统时,我们可以使用SSM框架来简化开发过程。以下是一个简单的示例:
1. 首先,创建一个Spring Boot项目,并添加必要的依赖项。在pom.xml文件中添加以下依赖:
```xml
```
2. 创建实体类(Entity):
```java
public class Invoice {
private Long id;
private String customerName;
private Double amount;
private Date issuedDate;
// getter和setter方法
}
```
3. 创建Mapper接口(Mapper):
```java
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface InvoiceMapper {
@Select("SELECT * FROM invoice WHERE customer_name = #{customerName}")
Invoice selectByCustomerName(String customerName);
}
```
4. 创建Service接口(Service):
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class InvoiceService {
@Autowired
private InvoiceMapper invoiceMapper;
public Invoice getInvoiceByCustomerName(String customerName) {
return invoiceMapper.selectByCustomerName(customerName);
}
}
```
5. 创建Controller类(Controller):
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class InvoiceController {
@Autowired
private InvoiceService invoiceService;
@GetMapping("/invoice/{customerName}")
public Invoice getInvoiceByCustomerName(@PathVariable String customerName) {
Invoice invoice = invoiceService.getInvoiceByCustomerName(customerName);
return invoice;
}
}
```
6. 运行项目,访问`http://localhost:8080/invoice/{customerName}`,例如`http://localhost:8080/invoice/张三`,将返回与张三相关的发票信息。