整合SSM-增删改查(下)

9 篇文章 0 订阅
3 篇文章 0 订阅

增删改:

新增:indexController

package com.liuxi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liuxi.mapper.ProductMapper;
import com.liuxi.pojo.Product;

@Controller
@RequestMapping("/product")
public class IndexController {

	@Autowired
	private ProductMapper productMapper;

	@RequestMapping("list")
	private String list(ModelMap map, String msg) {
		List<Product> list = productMapper.selectAll();
		map.put("list", list);
		map.put("msg", msg);
		return "list";
	}

	@RequestMapping("toInsert")
	private String toInsert() {
		return "edit";
	}

	// 增删改
	@RequestMapping("insert")
	private String insert(Product product, ModelMap map) { // spring mvc会自动封装接收参数的对象。
		String msg = "";
		if (productMapper.insert(product) > 0) {
			msg = "新增成功!";
		} else {
			msg = "新增失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}

}

mapper.xml:

	<insert id="insert">
		insert into product (pid,pname,price)
		values
		(#{pid},#{pname},#{price})
	</insert>

Mapper接口:

	Integer insert(Product product);

edit.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

<form action="${pageContext.request.contextPath }/product/insert.do">
	商品名称:<input type="text" name="pname" value=""><br>
	商品价格:<input type="text" name="price" value=""><br>
		<input type="submit" value="提交">
</form>
</body>
</html>

list.jsp添加:

<a href="${pageContext.request.contextPath }/product/toInsert.do">新增</a>
${msg }

修改和删除:

controller:

package com.liuxi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liuxi.mapper.ProductMapper;
import com.liuxi.pojo.Product;

@Controller
@RequestMapping("/product")
public class IndexController {

	@Autowired
	private ProductMapper productMapper;

	@RequestMapping("list")
	private String list(ModelMap map, String msg) {
		List<Product> list = productMapper.selectAll();
		map.put("list", list);
		map.put("msg", msg);
		return "list";
	}

	@RequestMapping("toInsert")
	private String toInsert() {
		return "edit";
	}

	// 增删改
	@RequestMapping("insert")
	private String insert(Product product, ModelMap map) { // spring mvc会自动封装接收参数的对象。
		String msg = "";
		if (productMapper.insert(product) > 0) {
			msg = "新增成功!";
		} else {
			msg = "新增失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}

	// 修改
	@RequestMapping("toUpdate")
	private String toUpdate(Integer pid, ModelMap map) {
		Product product = productMapper.selectOne(pid);
		map.put("product", product);
		return "edit";
	}

	@RequestMapping("update")
	private String update(Product product, ModelMap map) {
		String msg = "";
		if (productMapper.update(product) > 0) {
			msg = "修改成功!";
		} else {
			msg = "修改失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}
	
	@RequestMapping("delete")
	private String delete(Integer pid, ModelMap map) {
		String msg = "";
		if (productMapper.deleteOne(pid) > 0) {
			msg = "删除成功!";
		} else {
			msg = "删除失败!";
		}
		map.put("msg", msg);
		return "redirect:/product/list.do";
	}


}

mapper接口:

package com.liuxi.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.liuxi.pojo.Product;

public interface ProductMapper {

	// 查询所有数据
	List<Product> selectAll();

	Product selectOne(int pid);

	Integer update(Product product);

	int deleteOne(int pid);

	Integer insert(Product product);
}

mapper.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.liuxi.mapper.ProductMapper">
	<select id="selectAll" resultType="com.liuxi.pojo.Product">
		select * from product
	</select>

	<select id="selectOne" resultType="com.liuxi.pojo.Product">
		select * from product where
		pid = #{pid}
	</select>

	<update id="update">

		update product set pname =
		#{pname},price = #{price}
		where pid = #{pid}
	</update>

	<insert id="insert">
		insert into product (pid,pname,price)
		values
		(#{pid},#{pname},#{price})
	</insert>

	<delete id="deleteOne">
		delete from product where pid
		= #{pid}
	</delete>
</mapper>

list.jsp:

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/product/toInsert.do">新增</a>
	<table border="1px" style="border-collapse: collapse;">
		<tr>
			<td>PID</td>
			<td>商品名称</td>
			<td>价格</td>
			<td>操作</td>
			
		</tr>
		<c:forEach items="${list }" var="item">
			<tr>
				<td>${item.pid }</td>
				<td>${item.pname }</td>
				<td>${item.price }</td>
				<td><a href="${pageContext.request.contextPath }/product/toUpdate.do?pid=${item.pid}">修改</a>
					<a href="${pageContext.request.contextPath }/product/delete.do?pid=${item.pid}">删除</a>
				</td>
			</tr>
		</c:forEach>
	</table>
${msg }
</body>
</html>

edit.jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>

	<form
		action="${pageContext.request.contextPath }${product==null?'/product/insert.do':'/product/update.do'}">
		<c:if test="${product!=null }">
			<input type="hidden" name="pid" value="${product.pid }">
		</c:if>
		商品名称:<input type="text" name="pname" value="${product.pname }"><br>
		商品价格:<input type="text" name="price" value="${product.price }"><br>
		<input type="submit" value="提交">
	</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值