[求助]这个怎么做添加修改,是MyBatis框架写的文件
bookapp2.0.rar
(3.23 MB)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java. %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>展示书本信息</title> </head> <body> <span style="color: red;">${tip}</span> <table border="1"> <tr> <th>编号</th> <th>封面</th> <th>书名</th> <th>作者</th> <th>价格</th> <th>操作</th> </tr> <c:forEach items="${books}" var="book"> <tr> <td>${book.id }</td> <td><img src="${pageContext.request.contextPath }/images/${book.image}" /></td> <td>${book.name }</td> <td>${book.author}</td> <td>${book.price}</td> <td> <a href="${pageContext.request.contextPath }/books?method=delete&id=${book.id }">删除</a><br> <a href="${pageContext.request.contextPath }/books?method=update&id=${book.id }">修改</a> </td> </tr> </c:forEach> </table> <a href="${pageContext.request.contextPath }/bookAdd.jsp" >添加</a> </body> </html>首先可以在books展示页面分别添加两个a标签用于添加和修改
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java. html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www. http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加书籍</title> <script type="text/javascript"> <% %> </script> </head> <body> <form action="${pageContext.request.contextPath }/books?method=add" method="post"> <input type="hidden" name="id" value="${book.id }"> <table> <tr> <td>封面:</td> <td><input name="image" type="text" value="${book.image }"></td> </tr> <tr> <td>书名:</td> <td><input name="name" type="text" value="${book.name }"></td> </tr> <tr> <td>作者:</td> <td><input name="author" type="text" value="${book.author }"></td> </tr> <tr> <td>价格:</td> <td><input name="price" type="text" value="${book.price }"></td> </tr> <tr> <td><button>提交</button></td> </tr> </table> </form> </body> </html>在WebContent下增加bookAdd.jsp页面
package org.fkjava.bookapp.servlet; import import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.import org.fkjava.bookapp.dto.Book; import org.fkjava.bookapp.service.BookService; import org.fkjava.bookapp.service.BookServiceImpl; @WebServlet("/books") public class BookServlet extends HttpServlet { private static final long serialVersionUID = 1L; private HttpServletRequest request; private HttpServletResponse response; private BookService bookService = new BookServiceImpl(); public BookServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { this.request = request; this.response = response; String method = request.getParameter("method"); if ("list".equals(method)) { list(); } else if ("delete".equals(method)) { delete(); } else if ("add".equals(method)) { add(); } else if ("update".equals(method)) { update(); } } private void update() { String id = request.getParameter("id"); Book book = bookService.findOne(id); request.setAttribute("book", book); try { request.getRequestDispatcher("/bookAdd.jsp").forward(request, response); } catch (ServletException | IOException e) { e.printStackTrace(); } } private void add() throws IOException { String id = request.getParameter("id"); if(id!=null){ delete(); } Map<String, String[]> map = request.getParameterMap(); Book book = new Book(); try { BeanUtils.populate(book, map); bookService.add(book); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } private void delete() { try { // 接收需要删除的数据id String id = request.getParameter("id"); bookService.deleteById(id); request.setAttribute("tip", "删除成功了"); } catch (Exception e) { e.printStackTrace(); request.setAttribute("tip", "删除异常"); } // 删除成功以后需要回查数据 list(); } private void list() { try { // 去查询所有的书本信息 List<Book> books = bookService.findAll(); System.out.println(books); request.setAttribute("books", books); request.getRequestDispatcher("/WEB-INF/jsp/books.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } }在BookServlet中添加add和update方法
package org.fkjava.bookapp.service; import java.util.List; import org.fkjava.bookapp.dto.Book; import org.fkjava.bookapp.dto.Student; public interface BookService { List<Book> findAll(); void deleteById(String id); Student login(String loginName, String passWord); void add(Book book); Book findOne(String id); }
package org.fkjava.bookapp.service; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.fkjava.bookapp.dao.BookMapper; import org.fkjava.bookapp.dao.StudentMapper; import org.fkjava.bookapp.dto.Book; import org.fkjava.bookapp.dto.Student; import org.fkjava.bookapp.utils.SqlSessionFactoryUtils; public class BookServiceImpl implements BookService { @Override public List<Book> findAll() { // 业务层去查询所有的书本信息 // 获取数据访问层接口的对象去操作数据库 SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession() ; // 通知mybatis框架把BookMapper.xml文件转成BookMapper接口的实现类对象 BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); List<Book> books = bookMapper.selectAll(); return books; } @Override public void deleteById(String id) { // 业务层去查询所有的书本信息 // 获取数据访问层接口的对象去操作数据库 SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession() ; // 通知mybatis框架把BookMapper.xml文件转成BookMapper接口的实现类对象 BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); bookMapper.deleteById(id); (); SqlSessionFactoryUtils.close(sqlSession); } @Override public Student login(String loginName, String passWord) { // 业务层去查询所有的书本信息 // 获取数据访问层接口的对象去操作数据库 SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession() ; // 通知mybatis框架把BookMapper.xml文件转成BookMapper接口的实现类对象 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); // Map<String,Object> params = new HashMap<>(); // params.put("loginName", loginName); // params.put("passWord", passWord); // Student student = studentMapper.getStuByLoginNameAndPwd(params); Student student = studentMapper.getStuByLoginNameAndPwd(loginName , passWord ); SqlSessionFactoryUtils.close(sqlSession); return student; } @Override public void add(Book book) { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession() ; // 通知mybatis框架把BookMapper.xml文件转成BookMapper接口的实现类对象 BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); bookMapper.addBook(book); (); SqlSessionFactoryUtils.close(sqlSession); } @Override public Book findOne(String id) { SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession() ; BookMapper bookMapper = sqlSession.getMapper(BookMapper.class); return bookMapper.findOne(id); } }
package org.fkjava.bookapp.dao; import java.util.List; import org.fkjava.bookapp.dto.Book; /** * @author apple * 这个接口是直接操作书这张表的 * * Mybaits接口式编程的核心:mybaits框架 可以为我们的数据访问层接口做实现类 * 程序员无序自己为数据访问层接口定义实现类。 * */ public interface BookMapper { List<Book> selectAll(); void deleteById(String id); void addBook(Book book); Book findOne(String id); }
<!DOCTYPE mapper PUBLIC "-// Mapper 3.0//EN" "http:// namespace="org.fkjava.bookapp.dao.BookMapper"> <select id="selectAll" resultType="Book"> select * from tb_book </select> <delete id="deleteById"> delete from tb_book where id = #{xxxx} </delete> <insert id="addBook" parameterType="Book"> insert into tb_book(name,author,price,image) values(#{name},#{author},#{price},#{image}) </insert> <select id="findOne" resultType="Book" parameterType="String"> select * from tb_book where id = #{id} </select> </mapper>修改配置文件