springboot添加addEmp.jsp页面
1、控制器:
@Controller @RequestMapping("emp") public class EmpController { @Autowired private EmpService empService; @PostMapping("save") public String save(Emp emp){ empService.save(emp); return "redirect:/emp/findAll"; }
2、业务层
public interface EmpService { List<Emp> findAll(); void save(Emp emp); }
@Transactional @Service public class EmpServiceImpl implements EmpService { @Autowired private EmpDAO empDAO; @Override public void save(Emp emp) { emp.setId(UUID.randomUUID().toString()); empDAO.save(emp); } @Override public List<Emp> findAll() { return empDAO.findAll(); } }
3、dao层
@Repository public interface EmpDAO { public List<Emp> findAll() ; // void save(Emp emp); }
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> <insert id="save" parameterType="Emp"> insert into t_emp values (#{id},#{name},#{salary},#{age}) </insert> </mapper>
addEmp.jsp页面
<%-- Created by IntelliJ IDEA. User: chenjl159 Date: 2020/8/24 Time: 17:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" language="java" isELIgnored="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>add Emp</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> add Emp info: </h1> <form action="${pageContext.request.contextPath}/emp/save" method="post"> <table cellpadding="0" cellspacing="0" border="0" class="form_table"> <tr> <td valign="middle" align="right"> name: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="name" /> </td> </tr> <tr> <td valign="middle" align="right"> salary: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="salary" /> </td> </tr> <tr> <td valign="middle" align="right"> age: </td> <td valign="middle" align="left"> <input type="text" class="inputgri" name="age" /> </td> </tr> </table> <p> <input type="submit" class="button" value="Confirm" /> </p> </form> </div> </div> <div id="footer"> <div id="footer_bg"> ABC@126.com </div> </div> </div> </body> </html>
along