文章目录:
2.8 编写Java代码(实体类、dao接口和对应的mapper文件、service类、controller类)
2.8.2 创建实体类对应的dao接口和接口对应的mapper文件
2.9.3 注册成功和失败的页面(success.jsp、fail.jsp)
2.9.4 查询学生页面(queryStudent.jsp)
2.8 编写Java代码(实体类、dao接口和对应的mapper文件、service类、controller类)
package com.songzihao.entity; /** * */ public class Student { private Integer id; private String name; private Integer age; //getter and setter //toString }
2.8.2 创建实体类对应的dao接口和接口对应的mapper文件
package com.songzihao.dao; import com.songzihao.entity.Student; import java.util.List; /** * */ public interface StudentDao { int insertStudent(Student student); List<Student> selectStudent(); }
<?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.songzihao.dao.StudentDao"> <!-- 使用insert、update、delete、select标签编写sql语句 --> <insert id="insertStudent"> insert into student2(name,age) values (#{name},#{age}) </insert> <select id="selectStudent" resultType="com.songzihao.entity.Student"> select id,name,age from student2 order by id asc </select> </mapper>
package com.songzihao.service; import com.songzihao.entity.Student; import java.util.List; /** * */ public interface StudentService { int addStudent(Student student); List<Student> queryStudent(); }
package com.songzihao.service.impl; import com.songzihao.dao.StudentDao; import com.songzihao.entity.Student; import com.songzihao.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * */ @Service public class StudentServiceImpl implements StudentService { /** * studentDao是引用类型,其对象是在spring配置文件中创建 * 引用类型自动注入,这里使用注解 @Autowired 或者 @Resource */ @Autowired private StudentDao studentDao; @Override public int addStudent(Student student) { int rows=studentDao.insertStudent(student); return rows; } @Override public List<Student> queryStudent() { List<Student> list=studentDao.selectStudent(); return list; } }
package com.songzihao.controller; import com.songzihao.entity.Student; import com.songzihao.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * */ @Controller @RequestMapping(value = "/student") public class StudentController { /** * 声明service对象,调用其中的方法 * 引用类型自动注入,这里使用注解 @Autowired 或者 @Resource */ @Autowired private StudentService studentService; //添加学生 @RequestMapping(value = "/addStudent.do") public ModelAndView addStudent(Student student) { ModelAndView mv=new ModelAndView(); //调用service,处理业务逻辑方法,把处理结果返回给用户 int rows=studentService.addStudent(student); String msg=""; if (rows>0) { msg="注册成功!!!"; mv.addObject("msg",student.getName() + "," + student.getAge()); mv.setViewName("success"); }else { msg="注册失败!!!"; mv.addObject("msg",msg); mv.setViewName("fail"); } return mv; } //查询学生 @RequestMapping(value = "/queryStudent.do") @ResponseBody public List<Student> queryStudent() { List<Student> list=studentService.queryStudent(); return list; } }
<%@ page contentType="text/html;charset=utf-8" language="java" %> <% String basePath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"; %> <html> <head> <base href="<%=basePath%>"> <title>SSM</title> </head> <body> <div align="center"> <p>SSM整合开发的例子</p> <table> <tr> <td><a href="addStudent.jsp">注册学生</a></td> <td><br/></td> <td><a href="queryStudent.jsp">查询学生</a></td> </tr> </table> </div> </body> </html>
<%@ page contentType="text/html;charset=utf-8" language="java" %> <html> <head> <title>添加学生</title> </head> <body> <div align="center"> <p>注册学生</p> <form action="student/addStudent.do" method="post"> <table> <tr> <td>姓名:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>年龄:</td> <td><input type="text" name="age"></td> </tr> <tr> <td>提交:</td> <td><input type="submit" value="注册"></td> </tr> </table> </form> </div> </body> </html>
2.9.3 注册成功和失败的页面(success.jsp、fail.jsp)
<%@ page contentType="text/html;charset=utf-8" language="java" %> <html> <head> <title>$</title> </head> <body> <h3>结果:${msg}</h3> </body> </html>
<%@ page contentType="text/html;charset=utf-8" language="java" %> <html> <head> <title>$</title> </head> <body> <h3>结果:${msg}</h3> </body> </html>
2.9.4 查询学生页面(queryStudent.jsp)
<%@ page contentType="text/html;charset=utf-8" language="java" %> <% String basePath=request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"; %> <html> <head> <base href="<%=basePath%>"> <title>查询学生</title> <script type="text/javascript" src="js/jquery-3.6.0.js"></script> <script type="text/javascript"> $(function () { $("#myBtn").on("click",function () { $.ajax({ url: "student/queryStudent.do", dataType: "json", success: function (resp) { $("#stuinfo").empty(); $.each(resp,function (i,n) { $("#stuinfo").append("<tr><td>" + n.id + "</td>" + "<td>" + n.name + "</td>" + "<td>" + n.age + "</td></tr>"); }) } }) }) }) </script> </head> <body> <div align="center"> <p>查询学生 <button id="myBtn">获取学生信息</button></p> <table> <thead> <tr> <td>id</td> <td>姓名</td> <td>年龄</td> </tr> </thead> <tbody id="stuinfo"> </tbody> </table> </div> </body> </html>
3.写在结尾!!!
以上就是整个SSM整合开发的详细步骤,在这当中,只是简单的实现了注册学生、查询学生的功能,还有很多的漏洞、功能不足的地方。。。
毕竟我也是初学者,还希望大佬们勿喷!!!(😅😅😅) 有需要改进、或是哪里写的不太对的地方,也希望大佬可以指出!!!(😄😄😄)






