springboot查询所有以及jsp页面
1、控制层:
@Controller @RequestMapping("emp") public class EmpController { @Autowired private EmpService empService; @GetMapping("findAll") public String findAll(Model model){ List<Emp> emps= empService.findAll(); model.addAttribute("emps",emps); return "ems/emplist"; } }
model类是将查到的东西展示到页面上
2、service业务层
public interface EmpService { List<Emp> findAll(); } //Service
@Transactional @Service public class EmpServiceImpl implements EmpService { @Autowired private EmpDAO empDAO; @Override public List<Emp> findAll() { return empDAO.findAll(); } } //ServiceImpl
3、dao层
@Repository public interface EmpDAO { public List<Emp> findAll() ; }
4、.xml文件
<?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.chinaunicom.ems.dao.EmpDAO"> <select id="findAll" resultType="Emp"> select * from t_emp </select> </mapper>
项目结构:
页面:
emplist.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>emplist</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/ems/css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> Welcome! </h1> <table class="table"> <tr class="table_header"> <td> ID </td> <td> Name </td> <td> Salary </td> <td> Age </td> <td> Operation </td> </tr> <c:forEach items="${requestScope.emps}" var="emp"> <tr class="row1"> <td> ${emp.id} </td> <td> ${emp.name} </td> <td> ${emp.salary} </td> <td> ${emp.age} </td> <td> <a href="emplist.jsp">delete emp</a> <a href="updateEmp.html">update emp</a> </td> </tr> </c:forEach> </table> <p> <input type="button" class="button" value="Add Employee" onclick="location='${pageContext.request.contextPath}/ems/addEmp.jsp'"/> </p> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
login.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>login</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> login </h1> <form action="${pageContext.request.contextPath}/user/login" method="post"> <table cellpadding="0" cellspacing="0" border="0" class="form_table"> <tr> <td valign="middle" align="right"> username: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="username" /> </td> </tr> <tr> <td valign="middle" align="right"> password: </td> <td valign="middle" align="left"> <input type="password" class="inputgri" name="password" /> </td> </tr> </table> <p> <input type="submit" class="button" value="Submit »" /> <input type="button" class="button" onclick="location.href='${pageContext.request.contextPath}/ems/register.jsp'" value="Regist »" /> </p> </form> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
register.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>register</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="css/style.css" /> </head> <body> <div id="wrap"> <div id="top_content"> <div id="header"> <div id="rightheader"> <p> 2009/11/20 <br /> </p> </div> <div id="topheader"> <h1 id="title"> <a href="#">main</a> </h1> </div> <div id="navigation"> </div> </div> <div id="content"> <p id="whereami"> </p> <h1> 注册 </h1> <form action="${pageContext.request.contextPath}/user/register" method="post"> <table cellpadding="0" cellspacing="0" border="0" class="form_table"> <tr> <td valign="middle" align="right"> 用户名: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="username" /> </td> </tr> <tr> <td valign="middle" align="right"> 真实姓名: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="realname" /> </td> </tr> <tr> <td valign="middle" align="right"> 密码: </td> <td valign="middle" align="left"> <input type="password" class="inputgri" name="password" /> </td> </tr> <tr> <td valign="middle" align="right"> 性别: </td> <td valign="middle" align="left"> 男 <input type="radio" class="inputgri" name="sex" value="男" checked="checked"/> 女 <input type="radio" class="inputgri" name="sex" value="女"/> </td> </tr> <tr> <td valign="middle" align="right"> 验证码: <img id="num" src="${pageContext.request.contextPath}/user/getImage" /> <a href="javascript:;" onclick="document.getElementById('num').src = '${pageContext.request.contextPath}/user/getImage?'+(new Date()).getTime()">换一张</a> </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="code" /> </td> </tr> </table> <p> <input type="submit" class="button" value="Submit »" /> </p> </form> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
along