1.整合关键点
Spring:负责对象的创建、维护、管理及对象依赖资源的注入
SpringMVC:负责请求的处理相当于(Servlet)
MyBatis:负责与数据库进行交互
2.整合步骤
2.1.在pom.xml文件中导入依赖
mybatis、spring-webmvc、mybatis-spring、bonecp数据源、mysql、spring-jdbc、log4j、jstl
2.2.编写配置文件
spring-config.xml、 springmvc-config.xml 、mybatis-config.xml、log4j.properties spring-config.xml内容: 数据源|sqlSessionFactory|映射器|扫描支持注解的Bean|事务管理器|基于注解事务 springmvc-config.xml:组件扫描(controller)|注解驱动|视图解析器|文件上传等 mybatis-config.xml内容:日志记录工具|批量取别名|批量加载映射文件
2.3.定义项目结构和代码
com.ssm.entity|com.ssm.dao|com.ssm.service| com.ssm.service.impl|com.ssm.controller
2.4.配这web.xml
加载spring-config.xml|springmvc-config.xml|中文乱码
2.5.测试功能
3.整合实现——查询单个和所有
3.1.导入依赖
<dependencies>
<!--mybatis依赖包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.4</version>
</dependency>
<!--spring-context依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--mybatis-spring依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<!--bonecp数据源-->
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<!--mysql的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--spring-jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--junit单元测试框架依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!--log4j日志记录-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--jstl依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
3.2.编写配置文件
Spring配置文件:对应spring-config.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置数据源-->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost:3306/yndx"
p:username="root" p:password="admin"/>
<!--配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml"/>
<!--注入 映射器 Mapper basePackage指定了扫描的基准包,批量产生映射器的实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="com.ssm.dao"/>
<!--注入业务Bean 扫描注解定义的业务Bean-->
<context:component-scan base-package="com.ssm"/>
<!--配置事务管理器-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<!--基于声明式注解事务支持 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
SpringMVC配置文件:对应springmvc-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ssm.controller"/>
<!--启用注解驱动-mvc配置 -->
<mvc:annotation-driven/>
<!--配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
MyBatis配置文件:对应mybatis-config.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>
<!--设置日志实现 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<!--为实体类取别名 -->
<typeAliases>
<!--为整个包取别名 别名为 默认为实体类首字母小写-->
<package name="com.ssm.entity"/>
</typeAliases>
<!--映射器-告诉mybatis去哪里寻找sql映射文件 -->
<mappers>
<!--批量加载映射关系-映射文件 -->
<package name="com.ssm.dao"/>
</mappers>
</configuration>
日志配置文件:对应log4j.properties
### \u5C06log4j.properties\u6587\u4EF6\u653E\u5165\u6839\u76EE\u5F55\u4E0B
### \u8BBE\u7F6ELogger\u8F93\u51FA\u7EA7\u522B\u548C\u8F93\u51FA\u76EE\u7684\u5730
###
log4j.rootLogger=debug,stdout,logfile
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u6587\u4EF6\uFF1Ajbit.log ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}%l %F %p %m%n
3.3.查询单个和多有功能实现
实体类
//学生类
@Data
public class Student {
private Integer sno;
private String pwd;
private String sname;
private String sex;
private Integer gid;
private Integer age;
private String phone;
private String address;
}
数据层接口:对应StudentMapper接口
//学生映射接口
public interface StudentMapper {
@Select("select * from student where sno=#{sno}")
Student selectBySno(Integer sno); //单个参数
@Select("select * from student")
List<Student> getStulist(); //查询所有学生
}
映射文件:对应StudentMapper.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.ssm.dao.StudentMapper">
</mapper>
业务接口
public interface StudentService {
Student selectBySno(Integer sno); //单个参数
List<Student> getStulist(); //查询所有学生
}
业务实现
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper stuMapper;
@Override
public Student selectBySno(Integer sno) {
return stuMapper.selectBySno(sno);
}
@Override
public List<Student> getStulist() {
return stuMapper.getStulist();
}
}
控制层
@Controller
public class StudentController {
@Autowired
private StudentService stuService;
/**
* 查询所有学生
*/
@RequestMapping("/stulist.do")
public ModelAndView getList(){
List<Student> stulist = stuService.getStulist();
if(stulist!=null){
//去首页并且把查出来的所有学生带到页面去
return new ModelAndView("index","list",stulist);
}
return null;
}
//根据学生编号查询学生
@RequestMapping("/selectOne.do")
public ModelAndView selectStuBySno(Integer sno){
Student stu=stuService.selectBySno(sno);
if(stu!=null){
//去详情页面 并且把 单个学生对象 带到页面去
return new ModelAndView("detail","stu",stu);
}
return null;
}
}
首页页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<div align="center">
<table align="center" border="1" cellpadding="0" cellspacing="0" width="80%">
<tr bgcolor="blue">
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>电话</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="stu">
<tr>
<th>${stu.sno}</th>
<th>${stu.sname}</th>
<th>${stu.sex}</th>
<th>${stu.age}</th>
<th>${stu.phone}</th>
<th>${stu.address}</th>
<th><a href="selectOne.do?sno=${stu.sno}">详情</a>|修改|删去</th>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
详情页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>详情页面</title>
</head>
<body>
<ul style="list-style: none">
<li>学号:${stu.sno}</li>
<li>姓名:${stu.sname}</li>
<li>年龄:${stu.age}</li>
<li>性别:${stu.sex}</li>
<li>电话:${stu.phone}</li>
<li>地址:${stu.address}</li>
</ul>
</body>
</html>
测试:http://localhost:8080/P30_SSM/stulist.do
项目结构

4.删除功能
4.1.首页建立删去的超链接
<a href="deleteStu.do?sno=${stu.sno}">删去</a>
4.2.数据访问层接口
@Delete("delete from student where sno=#{sno}")
int deleteStuBySno(Integer sno);
4.3.业务层接口
int deleteStuBySno(Integer sno);
4.4.业务层实现
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper stuMapper;
//事务处理在类上面
@Override
public int deleteStuBySno(Integer sno) {
return stuMapper.deleteStuBySno(sno);
}
}
4.5.控制层实现
//根据学生编号删去学生
@RequestMapping("/deleteStu.do")
public ModelAndView deleteStu(Integer sno){
int num= stuService.deleteStuBySno(sno);
if(num>0){
//forward:stulist.do 转发到首页的RequestMapping(url)
return new ModelAndView("forward:stulist.do","msg","delete success!");
}
return null;
}
4.6.页面消息提示
在首页index.jsp中
<span style="color:red">${msg==null?'':msg}</span>
5.隔行变色
5.1.导入jquery依赖库
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
5.2.写js代码
在首页index.jsp中
<script type="text/javascript">
$(function(){ //:even选中所有偶数 odd 奇数
$(".base:even").css("background-color","green");
})
</script>
6.修改功能
6.1.去修改页面
超链接:在index.jsp中
<a href="toUpdatePage.do?sno=${stu.sno}">修改</a>
控制层代码
//去修改页面
@RequestMapping("/toUpdatePage.do")
public ModelAndView toUpdatePage(Integer sno){
Student stu=stuService.selectBySno(sno);
if(stu!=null){
//去修改 并且把 单个学生对象 带到修改页面去
return new ModelAndView("update","stu",stu);
}
return null;
}
建立修改页面并进行数据展示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改页面</title>
</head>
<body>
<div align="center">
<form action="doUpdate.do" method="post">
学号:<input type="text" name="sno" value="${stu.sno}" readonly="readonly"/><br>
姓名:<input type="text" name="sname" value="${stu.sname}"/><br>
性别:<input type="text" name="sex" value="${stu.sex}"/><br>
年龄:<input type="text" name="age" value="${stu.age}"/><br>
电话:<input type="text" name="phone" value="${stu.phone}"/><br>
地址:<input type="text" name="address" value="${stu.address}"/><br>
<input type="submit" value="修改"/> <input type="reset" value="重置"/>
</form>
</div>
</body>
</html>
6.2.真正的修改
数据层接口:对应StudentMapper接口
int updateStu(Student stu); //修改学生
映射文件
<?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.ssm.dao.StudentMapper">
<update id="updateStu" parameterType="student">
update student
<set>
<if test="sname!=null and sname!=''">sname=#{sname},</if>
<if test="sex!=null and sex!=''">sex=#{sex},</if>
<if test="age!=null">age=#{age},</if>
<if test="phone!=null and phone!=''">phone=#{phone},</if>
<if test="address!=null and address!=''">address=#{address}</if>
</set>
where sno=#{sno}
</update>
</mapper>
业务接口:对应StudentService接口
int updateStu(Student stu);
业务实现:对应StudentServiceImpl类
@Override
public int updateStu(Student stu) {
return stuMapper.updateStu(stu);
}
控制层代码
//真正的去做修改功能
@RequestMapping("/doUpdate.do")
public ModelAndView updateStu(Student stu){
int num= stuService.updateStu(stu);
if(num>0){
//forward:stulist.do 转发到首页的RequestMapping(url)
return new ModelAndView("forward:stulist.do","msg","update success!");
}
return null;
}
首页页面消息展示:对应index.jsp页面
<span style="color:red">${msg==null?'':msg}</span>
7.增加功能
7.1.增加超链接
<p><a href="toAddPage.do">增加学生</a></p>
7.2.数据接口
@Insert("insert into student(sname,sex,age,phone,address) values(#{sname},#{sex},#
{age},#{phone},#{address})")
int addStu(Student stu); //增加学生
7.3.业务接口
int addStu(Student stu); //增加学生
7.4.业务实现
@Override
public int addStu(Student stu) {
return stuMapper.addStu(stu);
}
7.5.控制层代码
//增加学生
@RequestMapping("/addStu.do")
public ModelAndView addStu(Student stu){
int num= stuService.addStu(stu);
if(num>0){
return new ModelAndView("forward:stulist.do","msg","add success!");
}
return null;
}
7.6.页面消息展示
对应首页index.jsp
<span style="color:red">${msg==null?'':msg}</span>