ssm框架入门(展示数据,修改,删除,批量删除,模糊搜索)

感谢多易大数据www.51doit.com,算是入门ssm框架,有错误的地方请立刻马上指出

ssm框架配置文件关系
这里是ssm框架配置文件,知道配置的略过.下图是项目列表:
在这里插入图片描述

1. 展示数据

  • Controller

/WEB-INF/pages/showAll.jsp 跳转页面的前缀和后缀是在application.xml中的视图解析器中配置的,自动拼接,所以这里返回的是"showAll"

/**将ItemController交给spring管理*/
@Controller
public class ItemController {
	/**自动注入itemService的实例*/
	@Autowired
	ItemService itemService;
	/** 展示所有数据 */
	@RequestMapping("/showall")   // 跳转的url
	public String showAll(Model model) {
		List<Item> allItems = itemService.getAllItems();
		model.addAttribute("items", allItems);
		return "showAll";//跳转到 /WEB-INF/pages/showAll.jsp 页面
	}
  • ItemMapper.java 和ItemMapper.xml
    接口:
	/**展示所有数据*/
	List<Item> getAllItems();

xml:

	<select id="getAllItems" resultType="item">
		select * from items
	</select>
  • showAll.jsp

在WEB-INF下的pages包(没有包创建一个)中创建showAll,jsp

    1. 展示数据的表
    1. 删除当前项按钮
    1. 修改当前项按钮
    1. 批量选中CheckBox,提交按钮
    1. 模糊搜索框架
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="x" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 模糊查找的搜索框 -->
	<form action="getItemByXxx" method="post"> 
		商品名:<input type="text" name="name">&nbsp;&nbsp;
		价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice">&nbsp;&nbsp;
		<input type="submit" value="搜索">
	</form>
	<center>
	<!-- 批量删除的提交表单 -->
	<form action="deleteByChooseId" method="post">
	<!-- 表,展示数据 -->
	<table border="2" cellspacing="0" cellpadding="0"width="100%"height="100">
		<tr>
			<th>ID</th>
			<th>商品名</th>
			<th>价格</th>
			<th>描述</th>
			<th>pic</th>
			<th>生成时间</th>
			<th>操作</th>
		</tr>
		<!-- 遍历数据 -->
		<x:forEach items="${items}" var="item">
			<tr>
				<td><input type="checkbox" name ="chooseId" value="${item.id }"/>${item.id }</td>
				<td>${item.name }</td>
				<td>${item.price }</td>
				<td>${item.detail }</td>
				<td>${item.pic }</td>
				<td>${item.createtime }</td>
				<td><button type="button">
						<!-- 删除当前项 -->
						<a href="deleteItemById?id=${item.id }">删除 | </a>
						<!-- 修改当前项 -->
						<a href="updata?id=${item.id }">修改</a>
					</button></td>
			</tr>
		</x:forEach>
	</table>
			<!-- 批量选择,提交到服务器 -->
			<input type="submit" value="删除选中的">
	</form>
	</center>
</body>
</html>

测试:

展示数据页面效果


2. 删除数据

2.1 删除当前项数据
  • showAll.jsp 删除按钮请求服务器,并且发送当前项的id
	<a href="deleteItemById?id=${item.id }">删除 | </a>
  • Controller
     /**删除指定数据*/
	@RequestMapping("/deleteItemById")
	public String deleteItemById(int id) { //接收页面的请求id
		itemService.deleteItemById(id);
		return "redirect:/showall";  //重定向到展示数据页面
	}
  • Mapper
    接口:
	/**删除指定id数据*/
	void deleteItemById(int id);

xml:

	<delete id="deleteItemById" parameterType="int">
		delete from items
		where id = #{id}
	</delete>

删除是重定向到showAll页面就不贴图


2.2 批量删除
  • showAll.jsp中 添加form表单,增加复选框
	<form action="deleteByChooseId" method="post">
	<table border="2" cellspacing="0" cellpadding="0"width="100%"height="100">..
			<input type="submit" value="删除选中的">
	</form>
<!-- 复选框 -->
<input type="checkbox" name ="chooseId" value="${item.id }"/>
  • Controller
	/** 删除指定数据*/
	@RequestMapping("/deleteByChooseId")
	public String deleteByChooseId(Choose choose,Model model) {
		choose.show();
		itemService.deleteByChooseId(choose);
		return "redirect:/showall";
	}

Choose用来接收页面发送的chooseId,因为数据一个或者多个,所有使用List接收

public class Choose {
	private List<Integer> chooseId;
	public List<Integer> getChooseId() {
		return chooseId;
	}
	public void setChooseId(List<Integer> chooseId) {
		this.chooseId = chooseId;
	}
}
  • Mapper
    接口:
void deleteByChooseId(Choose choose);

xml:
collection是Choose类中的chooseId属性,item是遍历之后的每个值(相当于增强for中的i)

	<delete id="deleteByChooseId" parameterType="choose">
		delete from items where id in
		<foreach collection="chooseId" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</delete>

修改:

开发思路:

  1. 请求服务器,并发送当前项的id
  2. 服务器根据id,在数据库中找到对应id的item信息,返回一个修改页面展示给客户端
  3. 客户端在修改页面修改数据之后,提交数据给服务器
  4. 服务器接收到修改之后的数据在对应地方更新数据
  5. 服务器重定向到showAll页面
  • showAll.jsp
    将当前项的id发送到服务器
<a href="updata?id=${item.id }">修改</a>
  • item_updata.jsp
<center>
		<form action="doupdata?id=${item.id }" method="post">
				<input type="hidden" name="id" value="${item.id }"><br/>
			姓名:<input type="text" name="name" value="${item.name }"><br/>
			价格:<input type="text" name="price" value="${item.price }"><br/>
			描述:<input type="text" name="detail" value="${item.detail }"><br/>
			图片url:<input type="text" name="pic" value="${item.pic }"><br/>
			生成时间:<input type="text" name="createtime" value="${item.createtime }"><br/>
			<input type="submit" value="提交">
		</form>
	</center>
  • Controller
	/** 接收用户id,展示id对应的用户信息*/
	@RequestMapping("/updata")
	public String updata(int id,Model model) {
		Item item = itemService.getItemById(id);
		model.addAttribute("item", item);
		return "item_updata";
	}
	
	/** 接收客户端修改的数据,在数据库中更新,重定向到showAll*/
	@RequestMapping("/doupdata")
	public String doupdata(Item item) {
		itemService.updataItemById(item);
		return "redirect:/showall";
	}
  • Mapper
    接口:
	Item getItemById(int id);
	void updataItemById(Item item);

xml:

	<!-- 根据id查找数据 -->
	<select id="getItemById"  parameterType="int" resultType="item">
		select * from items where id
		= #{id}
	</select>
	<!-- 根据参数,更新数据-->
	<update id="updataItemById" parameterType="item">
		update items set
		name=#{name},
		price=#{price},
		detail=#{detail},
		pic=#{pic},
		createtime=#{createtime}
		where id = #{id}
	</update>

修改页面跳转


模糊搜索

  • showAll.jsp
	<form action="getItemByXxx" method="post"> 
		商品名:<input type="text" name="name">&nbsp;&nbsp;
		价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice">&nbsp;&nbsp;
		<input type="submit" value="搜索">
	</form>
  • Controller
	/**根据用户数据模糊查询*/
	@RequestMapping("/getItemByXxx")
	public String getItemByXxx(SearchItem searchItem ,Model model) {
		List<Item> items = itemService.getItemByXxx(searchItem);
		model.addAttribute("items", items); // 将数据放在model中
		return "showAll"; // 返回视图
	}

SearchItem类

	public class SearchItem {
	private String name; //接收姓名
	private String lowPrice; //最低价格
	private String upPrice; //最高价格
  • Mapper
    接口:
List<Item> getItemByXxx(SearchItem searchItem);

xml:
使用动态SQL来查找信息

<!-- 动态sql -->
	<select id="getItemByXxx" resultType="item"
		parameterType="searchItem">
		select * from items
		<where>
			<if test="name!=null and name!=''">
				and name like '%${name}%'
			</if>
			<if test="lowPrice!=null and lowPrice!=''">
				and price &gt;= #{lowPrice}
			</if>
			<if test="upPrice!=null and upPrice!=''">
				and price &lt;= #{upPrice}
			</if>
		</where>
	</select>

效果测试:
在这里插入图片描述

有兴趣的自己写下,测试下.

  • 5
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值