接入上一篇SSM框架整合示例
需要创建相应的包与文件夹
Book数据表作为本示例数据
搭建项目开始
首先有bean后
private int id; private String name; private int cnt;
第一步拿到业务需求(查询全部书籍信息)
需要写上注解
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包下面处理请求调用的方法
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 ?看下图
会直接通过配置好的文件找到页面
随后创建文件夹>创建包>数据库交互层
看代码吧 我编不下去了
属性
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>
服务器启动后输入地址
结果:
不忘初心