标题:[求助]这个怎么做添加修改,是MyBatis框架写的文件
只看楼主
qq995354156
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-11-18
结帖率:100%
已结贴  问题点数:30 回复次数:9 
[求助]这个怎么做添加修改,是MyBatis框架写的文件
bookapp2.0.rar (3.23 MB)
搜索更多相关主题的帖子: 添加 修改 框架 文件 
2018-05-01 20:36
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:30 
程序代码:
<%@ 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页面

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 20:58
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
程序代码:
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方法

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 20:59
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
程序代码:
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);
    }

}

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 21:01
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
程序代码:
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);

}

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 21:01
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
程序代码:
<!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>
修改配置文件

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 21:02
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
commons-beanutils-1.8.3.jar.zip (226.95 KB)
用到了beanutis工具类,把这个压缩包的zip后缀去掉就可以用了

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 21:04
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
目前已知bug,增加和修改时只能是英文,有中文乱码问题!希望楼主能够自行解决!come on!

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 21:05
qq995354156
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-11-18
得分:0 
回复 8楼 疯狂的小a
谢谢
2018-05-03 22:11
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
得分:0 
不客气,有空请帮忙结下贴

假如人生没有梦想,和咸鱼有什么区别!
2018-05-03 23:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486546-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.246989 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved