ssm框架入门(展示数据,修改,删除,批量删除,模糊搜索)
感谢多易大数据www.51doit.com,算是入门ssm框架,有错误的地方请立刻马上指出
这里是ssm框架配置文件,知道配置的略过.下图是项目列表:
1. 展示数据
- Controller
/WEB-INF/pages/showAll.jsp 跳转页面的前缀和后缀是在application.xml中的视图解析器中配置的,自动拼接,所以这里返回的是"showAll"
/**将ItemController交给spring管理*/
@Controller
public class ItemController {/**自动注入itemService的实例*/@AutowiredItemService itemService;/** 展示所有数据 */@RequestMapping("/showall") // 跳转的urlpublic 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
- 展示数据的表
- 删除当前项按钮
- 修改当前项按钮
- 批量选中CheckBox,提交按钮
- 模糊搜索框架
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib uri="" prefix="x" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".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"> 价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice"> <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) { //接收页面的请求iditemService.deleteItemById(id);return "redirect:/showall"; //重定向到展示数据页面}
- Mapper
接口:
/**删除指定id数据*/void deleteItemById(int id);
xml:
<delete id="deleteItemById" parameterType="int">delete from itemswhere 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>
修改:
开发思路:
- 请求服务器,并发送当前项的id
- 服务器根据id,在数据库中找到对应id的item信息,返回一个修改页面展示给客户端
- 客户端在修改页面修改数据之后,提交数据给服务器
- 服务器接收到修改之后的数据在对应地方更新数据
- 服务器重定向到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 setname=#{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"> 价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice"> <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 >= #{lowPrice}</if><if test="upPrice!=null and upPrice!=''">and price <= #{upPrice}</if></where></select>
效果测试:
有兴趣的自己写下,测试下.
ssm框架入门(展示数据,修改,删除,批量删除,模糊搜索)
感谢多易大数据www.51doit.com,算是入门ssm框架,有错误的地方请立刻马上指出
这里是ssm框架配置文件,知道配置的略过.下图是项目列表:
1. 展示数据
- Controller
/WEB-INF/pages/showAll.jsp 跳转页面的前缀和后缀是在application.xml中的视图解析器中配置的,自动拼接,所以这里返回的是"showAll"
/**将ItemController交给spring管理*/
@Controller
public class ItemController {/**自动注入itemService的实例*/@AutowiredItemService itemService;/** 展示所有数据 */@RequestMapping("/showall") // 跳转的urlpublic 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
- 展示数据的表
- 删除当前项按钮
- 修改当前项按钮
- 批量选中CheckBox,提交按钮
- 模糊搜索框架
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib uri="" prefix="x" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ".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"> 价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice"> <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) { //接收页面的请求iditemService.deleteItemById(id);return "redirect:/showall"; //重定向到展示数据页面}
- Mapper
接口:
/**删除指定id数据*/void deleteItemById(int id);
xml:
<delete id="deleteItemById" parameterType="int">delete from itemswhere 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>
修改:
开发思路:
- 请求服务器,并发送当前项的id
- 服务器根据id,在数据库中找到对应id的item信息,返回一个修改页面展示给客户端
- 客户端在修改页面修改数据之后,提交数据给服务器
- 服务器接收到修改之后的数据在对应地方更新数据
- 服务器重定向到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 setname=#{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"> 价格区间:<input type="text" name="lowPrice">-<input type="text" name="upPrice"> <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 >= #{lowPrice}</if><if test="upPrice!=null and upPrice!=''">and price <= #{upPrice}</if></where></select>
效果测试:
有兴趣的自己写下,测试下.