java
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping("/list")
public String list(Model model) {
List<Customer> customers = customerService.getAllCustomers();
model.addAttribute("customers", customers);
return "customer/list";
}
@RequestMapping("/add")
public String add(Customer customer) {
customerService.addCustomer(customer);
return "redirect:/customer/list";
}
@RequestMapping("/edit/{id}")
public String edit(@PathVariable int id, Model model) {
Customer customer = customerService.getCustomerById(id);
model.addAttribute("customer", customer);
return "customer/edit";
}
@RequestMapping("/update")
public String update(Customer customer) {
customerService.updateCustomer(customer);
return "redirect:/customer/list";
}
@RequestMapping("/delete/{id}")
public String delete(@PathVariable int id) {
customerService.deleteCustomer(id);
return "redirect:/customer/list";
}
}
java
@Service
public class CustomerService {
@Autowired
private CustomerDao customerDao;
public List<Customer> getAllCustomers() {
return customerDao.getAllCustomers();
}
public Customer getCustomerById(int id) {
return customerDao.getCustomerById(id);
}
public void addCustomer(Customer customer) {
customerDao.addCustomer(customer);
}
public void updateCustomer(Customer customer) {
customerDao.updateCustomer(customer);
}
public void deleteCustomer(int id) {
customerDao.deleteCustomer(id);
}
}
java
@Repository
public class CustomerDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<Customer> getAllCustomers() {
String sql = "SELECT * FROM customer";
return jdbcTemplate.query(sql, new CustomerRowMapper());
}
public Customer getCustomerById(int id) {
String sql = "SELECT * FROM customer WHERE id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new CustomerRowMapper());
}
public void addCustomer(Customer customer) {
String sql = "INSERT INTO customer (unit, name, phone, mobile, qq, email) VALUES (?, ?, ?, ?, ?, ?)";
jdbcTemplate.update(sql, customer.getUnit(), customer.getName(), customer.getPhone(),
customer.getMobile(), customer.getQq(), customer.getEmail());
}
public void updateCustomer(Customer customer) {
String sql = "UPDATE customer SET unit = ?, name = ?, phone = ?, mobile = ?, qq = ?, email = ? WHERE id = ?";
jdbcTemplate.update(sql, customer.getUnit(), customer.getName(), customer.getPhone(),
customer.getMobile(), customer.getQq(), customer.getEmail(), customer.getId());
}
public void deleteCustomer(int id) {
String sql = "DELETE FROM customer WHERE id = ?";
jdbcTemplate.update(sql, id);
}
private class CustomerRowMapper implements RowMapper<Customer> {
@Override
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
Customer customer = new Customer();
customer.setId(rs.getInt("id"));
customer.setUnit(rs.getString("unit"));
customer.setName(rs.getString("name"));
customer.setPhone(rs.getString("phone"));
customer.setMobile(rs.getString("mobile"));
customer.setQq(rs.getString("qq"));
customer.setEmail(rs.getString("email"));
return customer;
}
}
}
html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>客户列表</title>
</head>
<body>
<h1>客户列表</h1>
<table>
<tr>
<th>单位</th>
<th>姓名</th>
<th>电话</th>
<th>手机</th>
<th>QQ</th>
<th>Email</th>
<th></th>
<th></th>
</tr>
<c:forEach var="customer" items="${customers}">
<tr>
<td>${customer.unit}</td>
<td>${customer.name}</td>
<td>${customer.phone}</td>
<td>${customer.mobile}</td>
<td>${customer.qq}</td>
<td>${customer.email}</td>
<td><a href="${pageContext.request.contextPath}/customer/edit/${customer.id}">编辑</a></td>
<td><a href="${pageContext.request.contextPath}/customer/delete/${customer.id}">删除</a></td>
</tr>
</c:forEach>
</table>
<h2>添加客户</h2>
<form action="${pageContext.request.contextPath}/customer/add" method="post">
单位: <input type="text" name="unit"><br>
姓名: <input type="text" name="name"><br>
电话: <input type="text" name="phone"><br>
手机: <input type="text" name="mobile"><br>
QQ: <input type="text" name="qq"><br>
Email: <input type="text" name="email"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
html
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>编辑客户</title>
</head>
<body>
<h1>编辑客户</h1>
<form action="${pageContext.request.contextPath}/customer/update" method="post">
<input type="hidden" name="id" value="${customer.id}">
单位: <input type="text" name="unit" value="${customer.unit}"><br>
姓名: <input type="text" name="name" value="${customer.name}"><br>
电话: <input type="text" name="phone" value="${customer.phone}"><br>
手机: <input type="text" name="mobile" value="${customer.mobile}"><br>
QQ: <input type="text" name="qq" value="${customer.qq}"><br>
Email: <input type="text" name="email" value="${customer.email}"><br>
<input type="submit" value="提交">
</form>
</body>
</html>