SSM整合扩展

1)修改接口CustomerDao和CustomerDao.xml。在CustomerDao.java中编写public List findCustomers()抽象方法,在CustomerDao.xml中编写实现该方法的SQL语句。

package com.itheima.dao;
import com.itheima.po.Customer;
public interface CustomerDao {
public Customer findCustomerById(Integer id);
public List<Customer> findCustomers();
}

CustomerDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.CustomerDao">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
<select id="findCustomers" resultType="customer">
select * from t_customer
</select>
</mapper>

2)修改接口CustomerService和CustomerServiceImp实现类。在CustomerService.java中编写public List findCustomers())抽象方法,在CustomerServiceImp.java中实现该抽象方法。

CustomerService.java
public interface CustomerService{
	public Customer findCustomerById(Integer id);
public List<Customer> findCustomers();
}
CustomerServiceImp.java
@Service
public class  CustomerServiceImp implements CustomerService {
    	@Autowired
	private CustomerDao customerDao;
	public Customer findCustomerById(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	public List<Customer> findCustomers() {
		return customerDao.findCustomers();
	}

}

3)在CustomerController类,在类中添加到findCustomers方法,并通过@RequestMapping注解该方法

@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	/**
	 *根據id查询客户信息
	 */
	@RequestMapping("/findCustomerById")
	public String findCustomerById(Integer id,Model model) {
		Customer customer =customerService.findCustomerById(id);
		model.addAttribute("customer", customer);
		//返回客户信息展示页面
		return "customer";
	}
/**
	 *查找所有的客户信息
	 */
	@RequestMapping("/findCustomers")
	public String findCustomers(Model model) {
		List<Customer> customers =customerService.findCustomers();
		model.addAttribute("customers", customers);
		//返回客户信息展示页面
		return "customers";
	}
}

4)在Web-INF/jsp的文件夹中创建customers.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="=UTF-8">
<title>用户</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/jsp/customer_add.jsp">添加</a>
	<table border="1">
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>职业</td>
			<td>电话</td>
			<td>操作</td>
		</tr>
		<c:forEach var="customer" items="${customers}">
		<tr>
			<td>${customer.id}</td>
			<td>${customer.username}</td>
			<td>${customer.jobs}</td>
			<td>${customer.phone}</td>
			<td>
			<a href="${pageContext.request.contextPath}/findCustomerById?id=${customer.id}">详情</a>
			<a href="${pageContext.request.contextPath}/toEditCustomer?id=${customer.id}">编辑</a>
      	<a href="${pageContext.request.contextPath}/deleteCustomer?id=${customer.id}">删除</a>    
  </td>
		</tr>
      </c:forEach>
	</table>
</body>
</html>

2.添加用户

1)在Web-INF/jsp的文件夹中创建customer_add.jsp页面。

···jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>

添加客户



``` # 2)修改接口CustomerDao和CustomerDao.xml。在CustomerDao.java中编写public void addCustomer(Customer customer);抽象方法,在CustomerDao.xml中编写实现该方法的SQL语句。 ```java CustomerDao.java类 package com.itheima.dao;

import com.itheima.po.Customer;

public interface CustomerDao {
public Customer findCustomerById(Integer id);

public List<Customer> findCustomers();
public int addCustomer(Customer customer);

}

```xml
CustomerDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.CustomerDao">
<select id="findCustomerById" parameterType="Integer" resultType="customer">
select * from t_customer where id=#{id}
</select>
<select id="findCustomers" resultType="customer">
select * from t_customer
</select>
<insert id="addCustomer" parameterType="customer">
INSERT into t_customer(username,jobs,phone) values (#{username}, #{jobs}, #{phone})
</insert>
</mapper>

3)修改接口CustomerService和CustomerServiceImp实现类。在CustomerService.java中编写public int addCustomer(Customer customer)抽象方法,在CustomerServiceImp.java中实现该抽象方法。

CustomerService.java
public interface CustomerService{
	 */
	public Customer findCustomerById(Integer id);

	public List<Customer> findCustomers();
	
	 public int addCustomer(Customer customer);
}
CustomerServiceImp.java
@Service
public class  CustomerServiceImp implements CustomerService {
    	@Autowired
	private CustomerDao customerDao;
	public Customer findCustomerById(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	public List<Customer> findCustomers() {
		return customerDao.findCustomers();
	}
public int addCustomer(Customer customer) {
			return 	 customerDao.addCustomer(customer);
	}

}

4)在CustomerController类,在类中添加到addCustomer方法,并通过@RequestMapping注解该方法

@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	/**
	 *添加用户
	 */
	@RequestMapping("/addCustomer")
	public String addCustomer(Customer customer) {
		int rows =customerService.addCustomer(customer);
		if (rows>0) {
			System.out.println("您成功插入了"+rows+"条数据");
		}else {
			System.out.println("执行插入操作失败");
		}
		return "redirect:findCustomers";
	}

}

3.编辑用户

1)点击customers.jsp页面中的编辑按钮,调用到后端的CustomerController中的toEditCustomer方法。

该方法的代码如下:

	@RequestMapping("/toEditCustomer")
	public String toEditCustomer(Integer id,Model model) {
		Customer customer =customerService.findCustomerById(id);
		model.addAttribute("customer", customer);
		//返回客户信息编辑示页面
		return "customer_edit";
	}

2)在Web-INF/jsp的文件夹中创建customer_edit.jsp页面。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>编辑客户</title>
</head>
<body>
<form  method="post" action="${pageContext.request.contextPath}/updateCustomer">
<input type="hidden" name="id" value="${customer.id}">
<label>客户名称:</label>
<input type="text" name="username" value="${customer.username}"><br>
<label>客户职业:</label>
<input type="text" name="jobs" value="${customer.jobs}"><br>
<label>客户电话:</label>
<input type="text" name="phone" value="${customer.phone}"><br>
<button type="submit">提交</button>
</form>
</body>
</html>

3)修改接口CustomerDao和CustomerDao.xml。在CustomerDao.java中编写 public int updateCustomer(Customer customer);抽象方法,在CustomerDao.xml中编写实现该方法的SQL语句。

CustomerDao.java类
package com.itheima.dao;

import com.itheima.po.Customer;

public interface CustomerDao {
	public Customer findCustomerById(Integer id);

	public List<Customer> findCustomers();
	public int addCustomer(Customer customer);
	public int updateCustomer(Customer customer);
}
CustomerDao.xml文件中添加更新的SQL语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.CustomerDao">
<!-- 修改用户信息 -->
	<update id="updateCustomer"
		parameterType="com.itheima.po.Customer">
		update t_customer
		<set>
			<if test="username!=null and username!=''">
				username=#{username},
			</if>
			<if test="jobs!=null and jobs!=''">
				jobs=#{jobs},
			</if>
			<if test="phone!=null and phone!=''">
				phone=#{phone},
			</if>
		</set>
		where id= #{id}
	</update>
</mapper>

4)修改接口CustomerService和CustomerServiceImp实现类。在CustomerService.java中编写public int updateCustomer(Customer customer)抽象方法,在CustomerServiceImp.java中实现该抽象方法。

CustomerService.java
public interface CustomerService{
	 */
	public Customer findCustomerById(Integer id);

	public List<Customer> findCustomers();
	
	  public int addCustomer(Customer customer);
	 public int updateCustomer(Customer customer);
}
CustomerServiceImp.java
@Service
public class  CustomerServiceImp implements CustomerService {
    	@Autowired
	private CustomerDao customerDao;
	public Customer findCustomerById(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	public List<Customer> findCustomers() {
		return customerDao.findCustomers();
	}
	@Override
	public int addCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return customerDao.addCustomer(customer);
	}

	@Override
	public int updateCustomer(Customer customer) {
		return customerDao.updateCustomer(customer);
	}

}

5)在CustomerController类,在类中添加到updateCustomer方法,并通过@RequestMapping注解该方法


@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	/**
	 *修改用户
	 */
	@RequestMapping("/updateCustomer")
	public String updateCustomer(Customer customer) {
		int rows =customerService.updateCustomer(customer);
		if (rows>0) {
			System.out.println("您成功更新了"+rows+"条数据");
		}else {
			System.out.println("执行更新操作失败");
		}
		return "redirect:findCustomers";
	}
}

3.删除用户

1)点击customers.jsp页面中的删除按钮,调用到后端的CustomerController中的deleteCustomer方法。

该方法的代码如下:

	@RequestMapping("/deleteCustomer")
	public String deleteCustomer(Integer id) {
		int rows =customerService.deleteCustomer(id);
		if (rows>0) {
			System.out.println("您成功删除了"+rows+"条数据");
		}else {
			System.out.println("执行删除操作失败");
		}
		//重定向到findCustomers请求中去
		return "redirect:findCustomers";
	}

2)修改接口CustomerDao和CustomerDao.xml。在CustomerDao.java中编写 public int deleteCustomer(Integer id)抽象方法,在CustomerDao.xml中编写实现该方法的SQL语句。

CustomerDao.java类
package com.itheima.dao;

import com.itheima.po.Customer;

public interface CustomerDao {
	public Customer findCustomerById(Integer id);

	public List<Customer> findCustomers();
	public int addCustomer(Customer customer);
	public int updateCustomer(Customer customer);
	public int deleteCustomer(Integer id);
}
CustomerDao.xml文件中添加删除新的SQL语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.CustomerDao">
<!-- 删除用户信息 -->
<delete id="deleteCustomer" parameterType="Integer">
 delete from t_customer where id=#{id}
</delete>
</mapper>

3)修改接口CustomerService和CustomerServiceImp实现类。在CustomerService.java中编写public int deleteCustomer(Integer id)抽象方法,在CustomerServiceImp.java中实现该抽象方法。

CustomerService.java
public interface CustomerService{
	 */
	public Customer findCustomerById(Integer id);

	public List<Customer> findCustomers();
	
	  public int addCustomer(Customer customer);
	 public int updateCustomer(Customer customer);
	 public int deleteCustomer(Integer id);
}
CustomerServiceImp.java
@Service
public class  CustomerServiceImp implements CustomerService {
    	@Autowired
	private CustomerDao customerDao;
	public Customer findCustomerById(Integer id) {
		return this.customerDao.findCustomerById(id);
	}
	public List<Customer> findCustomers() {
		return customerDao.findCustomers();
	}
	@Override
	public int addCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return customerDao.addCustomer(customer);
	}

	@Override
	public int updateCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return customerDao.updateCustomer(customer);
	}

	@Override
	public int deleteCustomer (Integer id) {
		return customerDao.deleteCustomer(id);
	}

}

4)在CustomerController类,在类中添加到deleteCustomer方法,并通过@RequestMapping注解该方法

@RequestMapping("/deleteCustomer")
	public String deleteCustomer(Integer id) {
		int rows =customerService.deleteCustomer(id);
		if (rows>0) {
			System.out.println("您成功删除了"+rows+"条数据");
		}else {
			System.out.println("执行删除操作失败");
		}
		//重定向到findCustomers请求中去
		return "redirect:findCustomers";
	}

5)点击customer.jsp页面中的删除按钮,数据库中的该条数据会被删除。

posted @   小帆歌  阅读(66)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示