数据库
# 创建表
CREATE TABLE Student (
sid INT PRIMARY KEY,
sname VARCHAR(50),
sage INT,
spic VARCHAR(255));
给student表插入数据
INSERT INTO Student (sid, sname, sage, spic)
VALUES
(1, 'John Do', 25, 'path/to/image1.jpg'),
(2, 'Jane Smith', 30, 'path/to/image2.jpg'),
(3, 'Alex Johnson', 35, 'path/to/image3.jpg'),
(4, 'Emily Davis', 28, 'path/to/image4.jpg'),
(5, 'Michael Wilson', 40, 'path/to/image5.jpg'),
(6, 'Sarah Brown', 37, 'path/to/image6.jpg'),
(7, 'David Lee', 45, 'path/to/image7.jpg'),
(8, 'Carolyn Taylor', 22, 'path/to/image8.jpg');
公共页面跳转,配置。。。
package com.zlj.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author zlj
* @create 2023-09-07 14:50
* 处理页面跳转
* 以往
*新增
*新增
*新增
* 现在
*新增
*新增
*新增
*/
@Controller
public class pageController {
@RequestMapping("/page/{page}")
public String toPage(@PathVariable("page") String page){
return page;
}
@RequestMapping("/page/{dir}{page}")
public String toDirPage(
@PathVariable("dir") String dir,
@PathVariable("page") String page){
return dir + "/" + page;
}
// @RequestMapping("/order/presave")
// public String orderpre(){
// return "/order/presave";
// }
// @RequestMapping("/clz/presave")
// public String clzpre(){
// return "/clz/presave";
// }
}
package com.zlj.util;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;
public class PageBean implements Serializable {
private static final long serialVersionUID = 2422581023658455731L;
//页码
private int page=1;
//每页显示记录数
private int rows=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private Map map;
public PageBean() {
super();
}
//设置请求参数
public void setRequest(HttpServletRequest req) {
String page=req.getParameter("page");
String rows=req.getParameter("rows");
String pagination=req.getParameter("pagination");
this.setPage(page);
this.setRows(rows);
this.setPagination(pagination);
this.url=req.getContextPath()+req.getServletPath();
this.map=req.getParameterMap();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public void setPage(String page) {
if(null!=page&&!"".equals(page.trim()))
this.page = Integer.parseInt(page);
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public void setRows(String rows) {
if(null!=rows&&!"".equals(rows.trim()))
this.rows = Integer.parseInt(rows);
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
public boolean isPagination() {
return isPagination;
}
public void setPagination(boolean isPagination) {
this.isPagination = isPagination;
}
public void setPagination(String isPagination) {
if(null!=isPagination&&!"".equals(isPagination.trim()))
this.isPagination = Boolean.parseBoolean(isPagination);
}
/**
* 获取分页起始标记位置
* @return
*/
public int getStartIndex() {
//(当前页码-1)*显示记录数
return (this.getPage()-1)*this.rows;
}
/**
* 末页
* @return
*/
public int getMaxPage() {
int totalpage=this.total/this.rows;
if(this.total%this.rows!=0)
totalpage++;
return totalpage;
}
/**
* 下一页
* @return
*/
public int getNextPage() {
int nextPage=this.page+1;
if(this.page>=this.getMaxPage())
nextPage=this.getMaxPage();
return nextPage;
}
/**
* 上一页
* @return
*/
public int getPreivousPage() {
int previousPage=this.page-1;
if(previousPage<1)
previousPage=1;
return previousPage;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
+ "]";
}
}
package com.zlj.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ResponseUtil {
public static void write(HttpServletResponse response,Object o)throws Exception{
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.println(o.toString());
out.flush();
out.close();
}
public static void writeJson(HttpServletResponse response,Object o)throws Exception{
ObjectMapper om = new ObjectMapper();
// om.writeValueAsString(o)代表了json串
write(response, om.writeValueAsString(o));
}
}
//generatorConfig.xml
//jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://47.100.191.44:3308/mybatis_ssm?useUnicode=true&characterEncoding=UTF-8
jdbc.username=test01
jdbc.password=test01
//log4j2.xml
/root/workspace/lucenedemo/logs
/root/workspace/lucenedemo/logs/error
/root/workspace/lucenedemo/logs/warn
%d{yyyy-MM-dd HH:mm:ss.SSS} [%t-%L] %-5level %logger{36} - %msg%n
//spring-content.xml
//spring-mvc.xml
//spring-mybatis.xml
helperDialect=mysql
//header.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
学生信息
<%
request.setAttribute("ctx",request.getContextPath());
%>
//web.xml
Archetype Created Web Application
contextConfigLocation
classpath:spring-content.xml
org.springframework.web.context.ContextLoaderListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
//zlj.tld
zlj 1.1 core library
zlj core
1.1
zlj
http://jsp.veryedu.cn
page
com.zlj.tag.PageTag
JSP
pageBean
true
true
后台接口准备
ssm的增删改查
1.先逆向生成对应的类(model,mapper.xml,mapper.java)
2.biz层
3.web层(依托于自定义mvc的分页标签==>pom.xml)
4.前端
package com.zlj.model;
import lombok.ToString;
@ToString
public class Stu {
private Integer sid;
private String sname;
private Integer sage;
private String spic="暂无图片";
public Stu(Integer sid, String sname, Integer sage, String spic) {
this.sid = sid;
this.sname = sname;
this.sage = sage;
this.spic = spic;
}
public Stu() {
super();
}
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getSage() {
return sage;
}
public void setSage(Integer sage) {
this.sage = sage;
}
public String getSpic() {
return spic;
}
public void setSpic(String spic) {
this.spic = spic;
}
}
sid, sname, sage, spic
delete from student
where sid = #{sid,jdbcType=INTEGER}
insert into student (sid, sname, sage,
spic)
values (#{sid,jdbcType=INTEGER}, #{sname,jdbcType=VARCHAR}, #{sage,jdbcType=INTEGER},
#{spic,jdbcType=VARCHAR})
insert into student
sid,
sname,
sage,
spic,
#{sid,jdbcType=INTEGER},
#{sname,jdbcType=VARCHAR},
#{sage,jdbcType=INTEGER},
#{spic,jdbcType=VARCHAR},
update student
sname = #{sname,jdbcType=VARCHAR},
sage = #{sage,jdbcType=INTEGER},
spic = #{spic,jdbcType=VARCHAR},
where sid = #{sid,jdbcType=INTEGER}
update student
set sname = #{sname,jdbcType=VARCHAR},
sage = #{sage,jdbcType=INTEGER},
spic = #{spic,jdbcType=VARCHAR}
where sid = #{sid,jdbcType=INTEGER}
package com.zlj.mapper;
import com.zlj.model.Stu;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StuMapper {
int deleteByPrimaryKey(Integer sid);
int insert(Stu record);
int insertSelective(Stu record);
Stu selectByPrimaryKey(Integer sid);
int updateByPrimaryKeySelective(Stu record);
int updateByPrimaryKey(Stu record);
List selectByCon(Stu stu);
}
package com.zlj.biz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import java.util.List;
/**
* @author zlj
* @create 2023-09-07 17:09
*/
public interface StuBiz {
int deleteByPrimaryKey(Integer sid);
int insert(Stu record);
int insertSelective(Stu record);
Stu selectByPrimaryKey(Integer sid);
int updateByPrimaryKeySelective(Stu record);
int updateByPrimaryKey(Stu record);
List ListPager(Stu stu, PageBean PageBean);
}
package com.zlj.biz.Impl;
import com.zlj.biz.StuBiz;
import com.zlj.mapper.StuMapper;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author zlj
* @create 2023-09-08 14:52
*/
@Service
public class StuBizImpl implements StuBiz {
@Autowired
private StuMapper stuMapper;
@Override
public int deleteByPrimaryKey(Integer sid) {
return stuMapper.deleteByPrimaryKey(sid);
}
@Override
public int insert(Stu record) {
return stuMapper.insert(record);
}
@Override
public int insertSelective(Stu record) {
return stuMapper.insertSelective(record);
}
@Override
public Stu selectByPrimaryKey(Integer sid) {
return stuMapper.selectByPrimaryKey(sid);
}
@Override
public int updateByPrimaryKeySelective(Stu record) {
return stuMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Stu record) {
return stuMapper.updateByPrimaryKey(record);
}
@Override
public List ListPager(Stu stu, PageBean pageBean) {
return stuMapper.selectByCon(stu);
}
}
package com.zlj.web;
import com.zlj.biz.StuBiz;
import com.zlj.model.Stu;
import com.zlj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* @author zlj
* @create 2023-09-08 16:55
*/
@Controller
@RequestMapping("/stu")
public class StuController {
@Autowired
private StuBiz stuBiz;
// 增
@RequestMapping("/add")
public String add(Stu stu,HttpServletRequest request) {
int i = stuBiz.insert(stu);
return "redirect:list";
}
// 删
@RequestMapping("/del/{sid}")
public String del(@PathVariable("sid") Integer sid) {
stuBiz.deleteByPrimaryKey(sid);
return "redirect:/stu/list";
}
// 改
@RequestMapping("/edit")
public String edit(Stu stu) {
stuBiz.updateByPrimaryKeySelective(stu);
return "redirect:list";
}
// 查
@RequestMapping("/list")
public String list(Stu stu, HttpServletRequest request) {
//stu是用来接收前台传递后台的参数
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List stus = stuBiz.ListPager(stu, pageBean);
request.setAttribute("lst", stus);
request.setAttribute("pageBean", pageBean);
// WEB-INF/jsp/stu/list.jsp
return "stu/list";
}
//数据回显
@RequestMapping("/preSave")
public String preSave(Stu stu, Model model) {
if (stu != null && stu.getSid() != null && stu.getSid() != 0) {
Stu s = stuBiz.selectByPrimaryKey(stu.getSid());
model.addAttribute("s", s);
}
return "stu/edit";
}
}
<%@ page language="java"
pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
博客的编辑界面
//list.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
学生信息
学生id
学生姓名
学生年龄
学生图片
操作
${s.sid }
${s.sname }
${s.sage }
${s.spic }
修改
删除
<%--
Created by IntelliJ IDEA.
User: 朱
Date: 2023/9/8
Time: 23:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
博客列表