帮我用java写一个ssm的增删改查项目,实现对蔬菜供应商的供应商编号,地址和电话进行增删改查,跳过配置,从写JSP页面调用编写
时间: 2024-02-15 12:03:02 浏览: 30
好的,我可以帮你完成这个任务。首先,我们需要按照SSM框架的要求进行配置,包括Spring、SpringMVC和MyBatis的配置。由于您已经跳过了配置,这里就不再赘述了。
接下来,我们先定义一个Supplier实体类,包含供应商编号、地址和电话三个属性,并编写对应的getter和setter方法。代码如下:
```java
public class Supplier {
private int id; // 供应商编号
private String address; // 供应商地址
private String phone; // 供应商电话
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
接下来,我们需要编写对应的Mapper接口和Mapper.xml文件,实现对Supplier表的增删改查操作。这里我以注解方式实现Mapper接口,代码如下:
```java
@Mapper
public interface SupplierMapper {
@Select("SELECT * FROM supplier WHERE id=#{id}")
Supplier getSupplierById(@Param("id") Integer id);
@Insert("INSERT INTO supplier(address, phone) VALUES(#{address}, #{phone})")
int addSupplier(Supplier supplier);
@Update("UPDATE supplier SET address=#{address}, phone=#{phone} WHERE id=#{id}")
int updateSupplier(Supplier supplier);
@Delete("DELETE FROM supplier WHERE id=#{id}")
int deleteSupplier(@Param("id") Integer id);
}
```
然后,我们需要编写对应的Service类,调用Mapper接口的方法实现对Supplier表的增删改查操作。代码如下:
```java
@Service
public class SupplierService {
@Autowired
private SupplierMapper supplierMapper;
public Supplier getSupplierById(Integer id) {
return supplierMapper.getSupplierById(id);
}
public int addSupplier(Supplier supplier) {
return supplierMapper.addSupplier(supplier);
}
public int updateSupplier(Supplier supplier) {
return supplierMapper.updateSupplier(supplier);
}
public int deleteSupplier(Integer id) {
return supplierMapper.deleteSupplier(id);
}
}
```
最后,我们需要编写对应的Controller类,调用Service类的方法,实现对Supplier表的增删改查操作。代码如下:
```java
@Controller
@RequestMapping("/supplier")
public class SupplierController {
@Autowired
private SupplierService supplierService;
@RequestMapping("/getSupplierById")
@ResponseBody
public Supplier getSupplierById(Integer id) {
return supplierService.getSupplierById(id);
}
@RequestMapping("/addSupplier")
@ResponseBody
public int addSupplier(Supplier supplier) {
return supplierService.addSupplier(supplier);
}
@RequestMapping("/updateSupplier")
@ResponseBody
public int updateSupplier(Supplier supplier) {
return supplierService.updateSupplier(supplier);
}
@RequestMapping("/deleteSupplier")
@ResponseBody
public int deleteSupplier(Integer id) {
return supplierService.deleteSupplier(id);
}
}
```
最后,我们需要编写对应的JSP页面,调用Controller类的方法,实现对Supplier表的增删改查操作。代码如下:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Supplier Management</title>
</head>
<body>
<h1>Supplier Management</h1>
<hr>
<h2>Get Supplier By Id</h2>
<form action="${pageContext.request.contextPath}/supplier/getSupplierById" method="get">
Supplier Id: <input type="text" name="id"><br>
<input type="submit" value="Get Supplier">
</form>
<hr>
<h2>Add Supplier</h2>
<form action="${pageContext.request.contextPath}/supplier/addSupplier" method="post">
Supplier Address: <input type="text" name="address"><br>
Supplier Phone: <input type="text" name="phone"><br>
<input type="submit" value="Add Supplier">
</form>
<hr>
<h2>Update Supplier</h2>
<form action="${pageContext.request.contextPath}/supplier/updateSupplier" method="post">
Supplier Id: <input type="text" name="id"><br>
Supplier Address: <input type="text" name="address"><br>
Supplier Phone: <input type="text" name="phone"><br>
<input type="submit" value="Update Supplier">
</form>
<hr>
<h2>Delete Supplier</h2>
<form action="${pageContext.request.contextPath}/supplier/deleteSupplier" method="post">
Supplier Id: <input type="text" name="id"><br>
<input type="submit" value="Delete Supplier">
</form>
<hr>
</body>
</html>
```
这样,我们就完成了一个简单的SSM增删改查项目。当然,这只是一个示例,实际开发中还需要根据具体业务需求进行适当的修改和优化。