Ssm整合
- 注意事项
- Spring mvc+Spring+Mybatis+MySQL
- 项目结构,以web项目结构整合(还有maven、gradle)
- 整合日志、事务一个项目中
- Idea 2019、JDK12、Tomcat9、MySQL 5.5
- 项目结构 D:\java\Idea\Idea_spacework\SSMhzy:不会就去找项目案例重新学习
- web项目为主结构,建立web目录,目录下有index.jsp和WEB-INF目录,WEB-INF目录可以放JSP文件。
- 配置文件建立在src目录下,Spring 容器配置文件bean.xml、Spring mvc请求处理配置springmvc-servlet.xml、Mybatis核心配置文件SqlConfig.xml。
- 程序代码存放位置,src目录下, 创建不同的包,dao、controller、service、pojo、util包存放不同的Java类
- JSP文件存放位置,WEB-INF目录下创建一个jsp目录,此目录下存储。
- 静态资源存放位置,在web目录下,和WEB-INF同一级目录创建res目录。
- 引入jar包
- Spring、springmvc基础包及其依赖包
- Mybatis核心包及其依赖包
- 数据库驱动程序包
- 采用C标签,需要jstl-1.2.jar
- 引入spring框架和mybaits框架结合的jar,mybatis-spring-1.3.0.jar
- 配置Tomcat服务器
- 配置项目结构
- 配置文件
- web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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">
<!-- spring mvc 核心控制器配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:bean.xml</param-value>
</context-param>
<!-- spring容器上下文环境-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<!-- spring mvc 的配置文件,放在当前项目src文件夹下-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- spring mvc 的配置文件表示启动时优先加载本文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- 开启Spring注解-->
<context:component-scan base-package="inter.service"/>
<!-- 初始化数据源-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/school"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 配置mybatis的sqlSessionFactory-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--扫描entity下的实体,方便mapper下的xml中使用别名,列如Depth则会找到zr.ssm.entity.Dept-->
<property name="typeAliasesPackage" value="inter.pojo"/>
<!--配置数据库连接池-->
<property name="dataSource" ref="dataSource"/>
<!--显示控制台SQL语句-->
<property name="configLocation" value="classpath:sqlConfig.xml"></property>
<!--自动扫描mapper下的XX.xml 文件-->
<property name="mapperLocations" value="classpath:inter/dao/*.xml"/>
</bean>
<!--DAO接口所在包名,Spring会自动查找其它的类并注入到Spring的容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="inter.dao"/>
</bean>
</beans>
- Springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<!-- spring 开启注解-->
<context:component-scan base-package="inter.controller"/>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源处理器-->
<!-- 静态资源访问-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- mapping 用户访问的URL路径;location:文件真实存放的路径-->
<mvc:resources mapping="/css/**" location="/res/css/"/>
<mvc:resources mapping="/js/**" location="/res/js/"/>
<mvc:resources mapping="/img/**" location="/res/img/"/>
</beans>
- SqlConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--定义在控制台上面输出sql语句-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<typeAliases>
<typeAlias type="inter.pojo.User" alias="User"></typeAlias>
</typeAliases>
</configuration>
- 代码
包结构: 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<eduCation> eduList = eduService.showAllEdu();
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<StuTable> list = stuTableService.showAllStu(stu);
List<eduCation> eduList = eduService.showAllEdu();
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<eduCation> eduList = eduService.showAllEdu();
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<StuTable> showAllStu(StuTable stuTable) {
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<StuTable> showAllStu(StuTable stuTable);
//删除
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" %>
<html>
<head>
<title>根据编号显示一条记录</title>
</head>
<body>
<form method="post" action="/updateStu">
<table border="1" cellpadding="16" cellspacing="0">
<tr>
<th colspan="2">修改页面</th>
</tr>
<tr>
<th>编号</th>
<td>
${stu.bh}
<input type="hidden" name="bh" value="${stu.bh}">
</td>
</tr>
<tr>
<th>姓名</th>
<td>
<input type="text" placeholder="请输入姓名" name="sname" value="${stu.sname}">
</td>
</tr>
<tr>
<th>电话</th>
<td>
<input type="text" placeholder="请输入电话" name="tel" value="${stu.tel}">
</td>
</tr>
<tr>
<th>生日</th>
<td>
<input type="text" placeholder="请输入生日" name="birthday" value="${stu.birthday}">
</td>
</tr>
<tr>
<th>性别</th>
<td>
<c:if test="${stu.gender==1}">
<input type="radio" name="gender" value="1" checked>男
<input type="radio" name="gender" value="0">女
</c:if>
<c:if test="${stu.gender==0}">
<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="0" checked>女
</c:if>
</td>
</tr>
<tr>
<th>学历</th>
<td>
<select name="xl">
<c:forEach items="${eduList}" var="edu">
<option value="${edu.edubh}" <c:if test="${edu.edubh==stu.xl}">selected</c:if>>${edu.eduname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<th>成绩</th>
<td>
<input type="text" placeholder="请输入成绩" name="cj" value="${stu.cj}">
</td>
</tr>
<tr>
<th>个人情况</th>
<td>
<input type="text" placeholder="请输入个人情况" name="grqk" value="${stu.grqk}">
</td>
</tr>
<tr>
<th>修改</th>
<td>
<input type="submit" value="修改">
</td>
</tr>
</table>
</form>
</body>
</html>
新增页面
<%--
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" %>
<html>
<head>
<title>信息输入</title>
</head>
<body>
<h1>用户输入</h1>
<form action="/saveStuT" method="post">
<input type="text" name="sname" placeholder="请输入姓名"><br>
<input type="text" name="tel" placeholder="请输入手机号"><br>
<input type="text" name="birthday" placeholder="请输入出生日期"><br>
<input type="radio" value="1" name="gender">男
<input type="radio" value="0" name="gender">女<br>
<select name="xl">
<c:forEach items="${eduList}" var="eduList">
<option value="${eduList.edubh}">${eduList.eduname}</option>
</c:forEach>
</select>
<br>
<input type="text" name="cj" placeholder="请输入成绩"><br>
<input type="text" name="grqk" placeholder="请输入个人情况"><br>
<input type="submit" value="保存">
</form>
</body>
</html>
显示页面
<%--
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" %>
<html>
<head>
<title>显示所有记录</title>
<style>
a {
text-decoration: none;
font-size: 20px;
}
</style>
</head>
<body>
<table border="1px" cellspacing="0" cellpadding="10" align="center">
<tr>
<th colspan="8">
<a href="/insertStuT">新增数据</a>
<form method="post" action="/stuList" style="display:inline-block">
<input type="text" name="username" placeholder="请输入查询姓名">
<input type="text" name="tel" placeholder="请输入查询电话">
<input type="submit" value="查询"/>
</form>
</th>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>电话</th>
<th>性别</th>
<th>学历</th>
<th>成绩</th>
<th>个人情况</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="stu">
<tr>
<td>${stu.bh}</td>
<td>${stu.sname}</td>
<td>${stu.tel}</td>
<td>
<c:if test="${stu.gender==1}">
男
</c:if>
<c:if test="${stu.gender==0}">
女
</c:if>
</td>
<td>
<c:forEach items="${eduList}" var="edu">
<c:if test="${edu.edubh==stu.xl}">
${edu.eduname}
</c:if>
</c:forEach>
</td>
<td>${stu.cj}</td>
<td>${stu.grqk}</td>
<td>
<a href="/delStut?bh=${stu.bh}">删除</a>
<a href="/editStu1?bh=${stu.bh}">修改</a>
<a href="/tipStu?bh=${stu.bh}">详情</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="8">
<div style="text-align: center">
<font size=2>
共${page.totalPageCount}页
</font>
<font size=2>
第${page.pageNow}页
</font>
<a href="/stuList?pageNow=1">首页</a>
<c:choose>
<c:when test="${page.pageNow - 1 > 0}">
<a href="/stuList?pageNow=${page.pageNow - 1}">上一页</a>
</c:when>
<c:when test="${page.pageNow - 1 <= 0}">
<a href="/stuList?pageNow=1">上一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="/stuList?pageNow=${page.pageNow}">下一页</a>
</c:when>
<c:when test="${page.pageNow + 1 < page.totalPageCount}">
<a href="/stuList?pageNow=${page.pageNow + 1}">下一页</a>
</c:when>
<c:when test="${page.pageNow + 1 >= page.totalPageCount}">
<a href="/stuList?pageNow=${page.totalPageCount}">下一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${page.totalPageCount==0}">
<a href="/stuList?pageNow=${page.pageNow}">尾页</a>
</c:when>
<c:otherwise>
<a href="/stuList?pageNow=${page.totalPageCount}">尾页</a>
</c:otherwise>
</c:choose>
</div>
</td>
</tr>
</table>
</body>
</html>
详细页面
<%--
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" %>
<html>
<head>
<title>详细</title>
</head>
<body>
<table border="1" cellpadding="16" cellspacing="0">
<tr>
<th colspan="2">详细页面</th>
</tr>
<tr>
<th>编号</th>
<td>
${stu1.bh}
</td>
</tr>
<tr>
<th>姓名</th>
<td>
${stu1.sname}
</td>
</tr>
<tr>
<th>电话</th>
<td>
${stu1.tel}
</td>
</tr>
<tr>
<th>生日</th>
<td>
${stu1.birthday}
</td>
</tr>
<tr>
<th>性别</th>
<td>
${stu1.gender}
</td>
</tr>
<tr>
<th>学历</th>
<td>
${stu1.xl}
</td>
</tr>
<tr>
<th>成绩</th>
<td>
${stu1.cj}
</td>
</tr>
<tr>
<th>个人情况</th>
<td>
${stu1.grqk}
</td>
</tr>
</table>
</body>
</html>
- 运行
使用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 文件中配置文章来源:https://www.toymoban.com/news/detail-490584.html
<!--扫描事务管理器,创建一个事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务注解,标注@Transactional的类和方法将具有事务性
那些方法需要事务就在那个方法上写一个@Transactional 这个标记
-->
<!-- 注解方式配置事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>文章来源地址https://www.toymoban.com/news/detail-490584.html
到了这里,关于SSM 整合案例的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!