接入上一篇SSM框架整合示例

 須要建立相應的包與文件夾css

Book數據表做爲本示例數據html

搭建項目開始前端

 

首先有bean後java

  private int id;
    private String name;
    private int cnt;

第一步拿到業務需求(查詢所有書籍信息)web

須要寫上註解spring

package com.gdnf.ssm.dao;

import com.gdnf.ssm.entity.Book;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BookDAO {

    List<Book> getBookAll();

}

在service包下面建立接口數據庫

package com.gdnf.ssm.service;

import com.gdnf.ssm.entity.Book;

import java.util.List;

public interface BookService {

    List<Book> getBookAll();

}

建立實現類服務器

package com.gdnf.ssm.service;

import com.gdnf.ssm.dao.BookDAO;
import com.gdnf.ssm.entity.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookDAO bookDAO = null;


    @Override
    public List<Book> getBookAll() {
        return bookDAO.getBookAll();
    }

}

web包下面處理請求調用的方法mybatis

package com.gdnf.ssm.web;

import com.gdnf.ssm.dao.BookDAO;
import com.gdnf.ssm.entity.Book;
import com.gdnf.ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.xml.ws.RequestWrapper;
import java.util.List;

@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    @Autowired
    private BookDAO bookDAO;

//請求路徑
    @RequestMapping("/bookAll")
    public String getBookAll(Model model) {
        List<Book> bookAll = bookService.getBookAll();
        model.addAttribute("books", bookAll);
        return "book_list";
    }
}

爲何返回String ?看下圖app

會直接經過配置好的文件找到頁面

 

隨後建立文件夾>建立包>數據庫交互層

看代碼吧 我編不下去了

屬性 

parameterType輸入參數類型,resultType爲返回參數類型
寫入參數格式 #{參數名} 可防注入
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.gdnf.ssm.dao.BookDAO">
  

    <select id="getBookAll" resultType="com.gdnf.ssm.entity.Book" >
        select * from book
    </select>


</mapper>

而後編寫前端渲染

 
<%--
  Created by IntelliJ IDEA.
  User: DZ
  Date: 2018/9/26
  Time: 15:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>書籍詳情</title>
</head>
<!-- CSS goes in the document HEAD or added to your external stylesheet -->
<style type="text/css">
    table.hovertable {
        font-family: 宋體;
        font-size: 18px;
        color:#333333;
        border-width: 1px;
        border-color: #999999;
        border-collapse: collapse;
    }
    table.hovertable th {
        background-color:#c3dde0;
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #a9c6c9;
    }
    table.hovertable tr {
        background-color:#d4e3e5;
    }
    table.hovertable td {

        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #a9c6c9;
    }
</style>
<body>
<a href="/book">添加書籍</a>
全部書籍:

<table class="hovertable" align = "center" width="450">
    <thead>
    <tr>
        <th>編號</th>
        <th>書名</th>
        <th>庫存</th>
        <th>其餘</th>
    </tr>
    </thead>

    <tbody>
    <c:forEach items="${books}" var="book">
        <tr>
            <td>${book.id}</td>
            <td>${book.name}</td>
            <td>${book.cnt}</td>
            <td></td>
        </tr>
    </c:forEach>
    </tbody>

</table>
<script !src="">

</script>
</body>
</html>

 

服務器啓動後輸入地址

結果:

相關文章
相關標籤/搜索