首页 > 其他分享 >7.整合SSM(前端+后端)

7.整合SSM(前端+后端)

时间:2022-11-11 19:46:28浏览次数:43  
标签:后端 int 前端 书籍 SSM books Books public bookName

4.MyBatis层编写

1.编写数据库对应的实体类

注:这里的MyBatis层应该和Mybatis-config.xml一同编写,属于同一层,这里编者,喜欢配置文件和Java代码分开编写!自行写的时候建议还是一层一层编写。

//这里使用注解是因为,编者依赖里导入了Lombok插件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Books {
    private int bookID;
    private String bookName;
    private int bookCounts;
    private String detail;
}

2.编写Mapper接口

//Mapper/dao层为了安全最好加-@Param("bookName")
public interface BookMapper {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(int id);
    //更新一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById(int id);
    //查询全部的书
    List<Books> queryAllBook();
    //根据书籍名称查询书籍
    Books queryBookName(@Param("bookName") String bookName);
}

3.编写接口对应的Sql语句——Mapper.xml

注:这里需要导入Mybatis的包!

Sql语句里不能有中文注释!!!!如果需要添加中文注释需要将头文件改成UTF8

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "<http://mybatis.org/dtd/mybatis-3-mapper.dtd>">
<mapper namespace="com.itxiaofei.mapper.BookMapper">

    <!--增加一个book-->
    <insert id="addBook" parameterType="Books">
        insert into ssmbuild.books(bookName, bookCounts, detail)
        values (#{bookName},#{bookCounts},#{detail});
    </insert>
    <!--根据id删除一个book-->
    <delete id="deleteBookById" parameterType="int">
        delete from ssmbuild.books where bookID = #{bookId}
    </delete>
    <!--更新book-->
    <update id="updateBook" parameterType="Books">
        update ssmbuild.books
        set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
        where bookID = #{bookID};
    </update>
    <!--根据id查询一本图书-->
    <select id="queryBookById" parameterType="int" resultType="Books">
        select * from ssmbuild.books 
        where bookID = #{bookID}
    </select>
    <!--查询全部图书-->
    <select id="queryAllBook" resultType="Books" parameterType="int">
        select * from ssmbuild.books
    </select>
    <!--根据图书名称查询图书-->
    <select id="queryBookName" resultType="Books" parameterType="Books">
        select * from ssmbuild.books where bookName = #{bookName}
    </select>
</mapper>

4.编写Service层的接口和实现类

//Service层查询书籍不需要参数-@Param("bookName")
public interface BookService {
    //增加一本书
    int addBook(Books books);
    //删除一本书
    int deleteBookById(int id);
    //更新一本书
    int updateBook(Books books);
    //查询一本书
    Books queryBookById(int id);
    //查询全部的书
    List<Books> queryAllBook();
    //根据书籍名字查询
    Books queryBookName(String bookName);
}

5.编写接口的实现类——ServiceImpl

@Service
public class BookServiceImpl implements BookService{
    //Service调Mapper层:组合Mapper层
    public BookMapper bookMapper;

    public void setBookMapper(BookMapper bookMapper) {
        this.bookMapper = bookMapper;
    }

    public int addBook(Books books) {
        return bookMapper.addBook(books);
    }

    public int deleteBookById(int id) {
        return bookMapper.deleteBookById(id);
    }

    public int updateBook(Books books) {
        return bookMapper.updateBook(books);
    }

    public Books queryBookById(int id) {
        return bookMapper.queryBookById(id);
    }

    public List<Books> queryAllBook() {
        return bookMapper.queryAllBook();
    }

    public Books queryBookName(String bookName) {
        return bookMapper.queryBookName(bookName);
    }
}

到此底层需求操作编写完成!

6.编写Controller层和视图层

再此我先放一个总的Controller层,和全部的JSP界面

  • Controller
@Controller
@RequestMapping("/book")
public class BookController {
    //controller 调用service层
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部的书籍,并且返回到一个书籍展示界面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allBook";
    }
    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){

        return "addBook";
    }
    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";//重定向:提交完数据需要重新刷新再提交
    }
    //跳转到修改页面
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("QBook",books);
        return "updateBook";
    }
    //修改书籍的请求
    @RequestMapping("/updateBook")
    public String updateBook(Books books,Model model){
        bookService.updateBook(books);
        Books book = bookService.queryBookById(books.getBookID());
        model.addAttribute("books",book);
        return "redirect:/book/allBook";
    }
    //删除图书
    @RequestMapping("/delete/{bookID}")
    public String deleteBook(@PathVariable("bookID") int id){
        bookService.deleteBookById(id);
        return "redirect:/book/allBook";
    }
    //根据书名查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        Books books = bookService.queryBookName(queryBookName);
        System.err.println("queryBook=>"+books);//err 打印红色输出
        //将查出的书放入,返回前端输出只显示这一本书,不显示其他书籍
        List<Books> list = new ArrayList<Books>();
        list.add(books);
        if (books==null){
            list = bookService.queryAllBook();
            model.addAttribute("error","请输入书籍名称");
        }

        model.addAttribute("list",list);
        return "allBook";
    }
}
  • 首页JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>首页</title>

    <style>
      a{
        text-decoration: none;/*去掉超链接下面的线*/
        color: hotpink;/*更改a标签字体颜色*/
        font-size: 15px;/*更改字体大小*/
      }
      h3{
        width:150px;
        height: 38px;
        /*将图像宽度和高度分别设置为 150 像素 38像素:*/
        margin: 150px auto;/*h3标签竖行位置*/
        text-align: center;/*h3标签左右居中*/
        line-height: 38px;/*上下居中*/
        background: beige;/*背景颜色*/
        border-radius: 20px;/*圆角边框*/
      }

    </style>

  </head>
  <body>

  <h3>
    <a href="${pageContext.request.contextPath}/book/allBook">进入书籍展示页面</a>
  </h3>

  </body>
</html>

效果图:

 

 

  • 书籍列表首页 allbook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="<http://java.sun.com/jsp/jstl/core>" %>
<html>
<head>
    <title>书籍列表</title>
    <%--导入BootStrap美化界面--%>
    <link href="<https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css>" rel="stylesheet">
</head>
<body>

<div class="container">

    <div class="row clearfix"><%--清楚浮动--%>

        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>书籍列表——显示所有书籍</small>
                </h1>
            </div>
        </div>
    </div>
        <%--增加书籍的按钮--%>
        <div class="row">
            <div class="col-md-4 column">
                <%--toaddBook--%>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
                    <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/test">测试</a>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">返回查询全部书籍</a>
            </div>
            <div class="col-md-8 column">
                <form  class="form-inline" action="${pageContext.request.contextPath}/book/queryBook" method="post" style="float: right">
                    <div class="form-group">
                        <input type="text" class="form-control" name="queryBookName" placeholder="请输入要查询书籍的名称">
                    </div>
                    <button type="submit" class="btn btn-default">查询</button>
                </form>
            </div>
        </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <%--表格头--%>
                <thead>
                <tr>
                    <th>书籍编号</th>
                    <th>书籍名称</th>
                    <th>书籍数量</th>
                    <th>书籍详情</th>
                    <th>操作</th>
                </tr>
                </thead>
                <%--表格身体:书籍从书籍库中查询出来
                从这个list中遍历出来:foreach
                --%>
                <tbody>
                <c:forEach var="book" items="${list}">
                    <tr>
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.detail}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">修改</a>
                            &nbsp; | &nbsp;
                            <a href="${pageContext.request.contextPath}/book/delete/${book.bookID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>

效果图:

 

 

  • 添加书籍页面:addBook.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="<http://java.sun.com/jsp/jstl/core>" %>
<html>
<head>
    <title>增加书籍</title>
    <%--导入BootStrap美化界面--%>
    <link href="<https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css>" rel="stylesheet">
</head>
<body>
<div class="container">

    <div class="row clearfix"><%--清楚浮动--%>

        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>书籍列表——新增书籍</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/book/addBook" method="post">

        <div class="form-group">
            <label>书籍名称</label>
            <input type="text" name="bookName" class="form-control" required>
        </div>

        <div class="form-group">
            <label>书籍数量</label>
            <input type="text" name="bookCounts" class="form-control" required>
        </div>

        <div class="form-group">
            <label>书籍描述</label>
            <input type="text" name="detail" class="form-control" required>
        </div>
        <button type="submit" class="btn btn-default">增加</button>
    </form>

</div>

效果图:

修改书籍页面:updateBook.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="<http://java.sun.com/jsp/jstl/core>" %>
<html>
<head>
    <title>修改书籍</title>
    <%--导入BootStrap美化界面--%>
    <link href="<https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css>" rel="stylesheet">
</head>
<body>
<div class="container">

    <div class="row clearfix"><%--清楚浮动--%>

        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>书籍列表——修改书籍</small>
                </h1>
            </div>
        </div>
    </div>

    <form action="${pageContext.request.contextPath}/book/updateBook" method="post">
        <div class="form-group">
            <label>书籍编号</label>
            <input type="text" name="bookID" value="${QBook.bookID}" class="form-control" required>
        </div>

        <div class="form-group">
            <label>书籍名称</label>
            <input type="text" name="bookName" value="${QBook.bookName}" class="form-control">
        </div>

        <div class="form-group">
            <label>书籍数量</label>
            <input type="text" name="bookCounts" value="${QBook.bookCounts}" class="form-control">
        </div>

        <div class="form-group">
            <label>书籍描述</label>
            <input type="text" name="detail" value="${QBook.detail}" class="form-control">
        </div>
        <button type="submit" class="btn btn-default">修改</button>
    </form>
</div>

效果图:

SSM框架的重要程度是不言而喻的,学到这里,大家已经可以进行基本网站的单独开发。但是这只是增删改查的基本操作。可以说学到这里,大家才算是真正的步入了后台开发的门。也就是能找一个后台相关工作的底线。

或许很多人,工作就做这些事情,但是对于个人的提高来说,还远远不够!

我们后面还要学习一些 SpringMVC 的知识!

标签:后端,int,前端,书籍,SSM,books,Books,public,bookName
From: https://www.cnblogs.com/itxiaofei/p/16881551.html

相关文章