Ssm整合
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
org.springframework.web.servlet.DispatcherServlet
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="inter.service"/>
xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="inter.controller"/>
<mvc:annotation-driven>mvc:annotation-driven>
<mvc:resources mapping="/css/**" location="/res/css/"/>
<mvc:resources mapping="/js/**" location="/res/js/"/>
<mvc:resources mapping="/img/**" location="/res/img/"/>
xml version="1.0" encoding="UTF-8" ?>
configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
包结构: inter
-->
controller
-->service
-->pojo
-->dao
-->util
Controller层下面的代码
package inter.controller;
import inter.pojo.StuTable;
import inter.pojo.eduCation;
import inter.service.EduService;
import inter.service.StuTableService;
import inter.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
public class StuTableController {
@Autowired
StuTableService stuTableService;
@Autowired
EduService eduService;
//新增数据页面
@RequestMapping(value = "/insertStuT")
public ModelAndView insertStuT() {
ModelAndView mv = new ModelAndView();
List
mv.addObject("eduList",eduList);
mv.setViewName("insertStuT");
return mv;
}
//保存新增数据
@RequestMapping(value = "/saveStuT")
public ModelAndView saveStuT(StuTable stuTable) {
int num = stuTableService.saveStuT(stuTable);
ModelAndView mv = new ModelAndView();
mv.setViewName("redirect:stuList");
return mv;
}
//显示所有记录
@RequestMapping(value = "/stuList")
public ModelAndView showAllStu(String pageNow,StuTable stuTable) {
ModelAndView mv = new ModelAndView();
try {
//获取总行数
int totalCount = stuTableService.stuCount();
Page page = null;
if (pageNow!=null){
page = new Page(totalCount,Integer.parseInt(pageNow));
}else {
page = new Page(totalCount,1);
}
StuTable stu = new StuTable();
stu.setPageSize(page.getPageSize());
stu.setStart(page.getStartPos());
if (stuTable.getTel()!=null){
stu.setTel(stuTable.getTel());
}
if (stuTable.getSname()!=null){
stu.setTel(stuTable.getSname());
}
List
List
mv.addObject("eduList",eduList);
mv.addObject("list", list);
mv.addObject("page",page);
mv.setViewName("stuList");
}catch (Exception e){
e.printStackTrace();
}
return mv;
}
//删除记录
@RequestMapping(value = "/delStut")
public ModelAndView delStu(int bh) {
int sta = stuTableService.delStu(bh);
ModelAndView mv = new ModelAndView();
if (sta > 0) {
mv.setViewName("redirect:stuList");
}
return mv;
}
//根据编号显示一条记录
@RequestMapping(value = "/editStu1")
public ModelAndView getStuByBh(int bh) {
ModelAndView mv = new ModelAndView();
StuTable stu1 = stuTableService.getStuByBh(bh);
List
mv.addObject("eduList",eduList);
mv.addObject("stu", stu1);
mv.setViewName("editStu");
return mv;
}
//修改数据
@RequestMapping(value = "/updateStu")
public String updateStu(StuTable stuTable) {
int sta1 = stuTableService.updateStu(stuTable);
return "redirect:stuList";//重定向,自动携带数据
}
//显示详情页面
@RequestMapping (value = "/tipStu")
public ModelAndView tipStu(int bh){
ModelAndView mv = new ModelAndView();
StuTable stu2 = stuTableService.getStuByBh(bh);
mv.addObject("stu1", stu2);
mv.setViewName("tipStu");
return mv;
}
}
Service下面的代码
package inter.service;
import inter.dao.IStuTableDao;
import inter.pojo.StuTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class StuTableService {
@Autowired
IStuTableDao tableDao;
//新增
@Transactional
public int saveStuT(StuTable stu){
int num = tableDao.saveStuT(stu);
return num;
}
//显示所有
public List
return tableDao.showAllStu(stuTable);
}
//删除记录
public int delStu(int bh){
return tableDao.delStu(bh);
}
//显示一条记录
public StuTable getStuByBh(int bh){
return tableDao.getStuByBh(bh);
}
//修改
public int updateStu(StuTable stuTable){
return tableDao.updateStu(stuTable);
}
//显示记录数
public int stuCount(){
return tableDao.stuCount();
}
public IStuTableDao getTableDao() {
return tableDao;
}
public void setTableDao(IStuTableDao tableDao) {
this.tableDao = tableDao;
}
}
Pojo 代码
package inter.pojo;
public class StuTable {
private int bh;
private String sname;
private String tel;
private String birthday;
private String gender;
private String xl;
private double cj;
private String grqk;
//下面两个参数,只是供传参使用
private int start;//索引开始位置
private int pageSize;//数据显示记录数
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getBh() {
return bh;
}
public void setBh(int bh) {
this.bh = bh;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getXl() {
return xl;
}
public void setXl(String xl) {
this.xl = xl;
}
public double getCj() {
return cj;
}
public void setCj(double cj) {
this.cj = cj;
}
public String getGrqk() {
return grqk;
}
public void setGrqk(String grqk) {
this.grqk = grqk;
}
}
Dao 代码
package inter.dao;
import inter.pojo.StuTable;
import java.util.List;
public interface IStuTableDao {
//数据保存
int saveStuT(StuTable stuTable);
//显示所有
List
//删除
int delStu(int bh);
//根据编号显示一条记录
StuTable getStuByBh(int bh);
//修改
int updateStu(StuTable stuTable);
//显示记录数
int stuCount();
}
Util 工具类 里面
package inter.util;
public class Page {
private int pageNow=1; //当前页数
private int pageSize=5; //每页显示的数量
private int totalCount; //总记录数
private int totalPageCount; //总页数
private int startPos; //开始位置,从0开始
private boolean hasFrist; //是否有首页
private boolean hasPre; //是否有前一页
private boolean hasNext; //是否有后一页
private boolean hasLast; //是否有尾页 /**
//通过构造函数,传入总记录数和当前页
public Page(int totalCount, int pageNow){
this.totalCount=totalCount;
this.pageNow=pageNow;
}
//获取总页数
public int getTotalPageCount() {
totalPageCount=getTotalCount()/getPageSize();
return (totalCount%pageSize==0)?totalPageCount:totalPageCount+1;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
//获取选择记录的初始位置
public int getStartPos() {
return (pageNow-1)*pageSize;
}
public void setStartPos(int startPos) {
this.startPos = startPos;
}
//判断是否有第一页
public boolean isHasFrist() {
return (pageNow==1) ? false:true;
}
public void setHasFrist(boolean hasFrist) {
this.hasFrist = hasFrist;
}
//如果有首页就有前一页
public boolean isHasPre() {
return isHasFrist() ? true:false;
}
public void setHasPre(boolean hasPre) {
this.hasPre = hasPre;
}
//如果有尾页就有下一页
public boolean isHasNext() {
return isHasNext() ? true:false;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
//判断是否有尾页
public boolean isHasLast() {
return (pageNow==getTotalCount()) ? false:true;
}
public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
JSP 页面代码部分
修改页面
<%@ page import="inter.pojo.StuTable" %><%--
Created by IntelliJ IDEA.
User: shun
Date: 2023/6/6
Time: 20:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
method="post" action="/updateStu">border="1" cellpadding="16" cellspacing="0">
colspan="2">修改页面
编号
${stu.bh}
type="hidden" name="bh" value="${stu.bh}">
姓名
type="text" placeholder="请输入姓名" name="sname" value="${stu.sname}">
电话
type="text" placeholder="请输入电话" name="tel" value="${stu.tel}">
生日
type="text" placeholder="请输入生日" name="birthday" value="${stu.birthday}">
性别
<c:if test="${stu.gender==1}">
type="radio" name="gender" value="1" checked>男
type="radio" name="gender" value="0">女
c:if>
<c:if test="${stu.gender==0}">
type="radio" name="gender" value="1">男
type="radio" name="gender" value="0" checked>女
c:if>
学历
name="xl">
<c:forEach items="${eduList}" var="edu">
value="${edu.edubh}" <c:if test="${edu.edubh==stu.xl}">selectedc:if>>${edu.eduname}
c:forEach>
成绩
type="text" placeholder="请输入成绩" name="cj" value="${stu.cj}">
个人情况
type="text" placeholder="请输入个人情况" name="grqk" value="${stu.grqk}">
修改
type="submit" value="修改">
新增页面
<%--
Created by IntelliJ IDEA.
User: shun
Date: 2023/6/5
Time: 19:54
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户输入
action="/saveStuT" method="post">
type="text" name="sname" placeholder="请输入姓名">
type="text" name="tel" placeholder="请输入手机号">
type="text" name="birthday" placeholder="请输入出生日期">
type="radio" value="1" name="gender">男
type="radio" value="0" name="gender">女
name="xl">
<c:forEach items="${eduList}" var="eduList">
value="${eduList.edubh}">${eduList.eduname}
c:forEach>
type="text" name="cj" placeholder="请输入成绩">
type="text" name="grqk" placeholder="请输入个人情况">
type="submit" value="保存">
显示页面
<%--
Created by IntelliJ IDEA.
User: shun
Date: 2023/6/6
Time: 19:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
a {
text-decoration: none;
font-size: 20px;
}
详细页面
<%--
Created by IntelliJ IDEA.
User: shun
Date: 2023/6/6
Time: 20:46
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
border="1" cellpadding="16" cellspacing="0">
colspan="2">详细页面
编号
${stu1.bh}
姓名
${stu1.sname}
电话
${stu1.tel}
生日
${stu1.birthday}
性别
${stu1.gender}
学历
${stu1.xl}
成绩
${stu1.cj}
个人情况
${stu1.grqk}
使用Tomcat运行,在浏览器中显示
显示页面
新增数据页面
修改页面
详细页面
在src下创建一个log4j.properties文件配置日志
# Global logging configuration
log4j.rootLogger = DEBUG,stdout
#Console output...
log4j.appender.stdout= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在 bean.xml 文件中配置
<tx:annotation-driven transaction-manager="transactionManager"/>