Spring:负责对象的创建、维护、管理及对象依赖资源的注入
SpringMVC:负责请求的处理相当于(Servlet)
MyBatis:负责与数据库进行交互
mybatis、spring-webmvc、mybatis-spring、bonecp数据源、mysql、spring-jdbc、log4j、jstl
spring-config.xml、 springmvc-config.xml 、mybatis-config.xml、log4j.properties spring-config.xml内容: 数据源|sqlSessionFactory|映射器|扫描支持注解的Bean|事务管理器|基于注解事务 springmvc-config.xml:组件扫描(controller)|注解驱动|视图解析器|文件上传等 mybatis-config.xml内容:日志记录工具|批量取别名|批量加载映射文件
com.ssm.entity|com.ssm.dao|com.ssm.service| com.ssm.service.impl|com.ssm.controller
加载spring-config.xml|springmvc-config.xml|中文乱码
org.mybatis
mybatis
3.4.4
org.springframework
spring-webmvc
5.2.4.RELEASE
org.mybatis
mybatis-spring
2.0.4
com.jolbox
bonecp
0.8.0.RELEASE
mysql
mysql-connector-java
5.1.47
org.springframework
spring-jdbc
5.2.4.RELEASE
junit
junit
4.13
test
log4j
log4j
1.2.17
javax.servlet
jstl
1.2
Spring配置文件:对应spring-config.xml
SpringMVC配置文件:对应springmvc-config.xml
MyBatis配置文件:对应mybatis-config.xml
日志配置文件:对应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
实体类
//学生类
@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 getStulist(); //查询所有学生
}
映射文件:对应StudentMapper.xml
业务接口
public interface StudentService {
Student selectBySno(Integer sno); //单个参数
List getStulist(); //查询所有学生
}
业务实现
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper stuMapper;
@Override
public Student selectBySno(Integer sno) {
return stuMapper.selectBySno(sno);
}
@Override
public List getStulist() {
return stuMapper.getStulist();
}
}
控制层
@Controller
public class StudentController {
@Autowired
private StudentService stuService;
/**
* 查询所有学生
*/
@RequestMapping("/stulist.do")
public ModelAndView getList(){
List 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" %>
首页
详情页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
详情页面
- 学号:${stu.sno}
- 姓名:${stu.sname}
- 年龄:${stu.age}
- 性别:${stu.sex}
- 电话:${stu.phone}
- 地址:${stu.address}
测试:http://localhost:8080/P30_SSM/stulist.do
项目结构
删去
@Delete("delete from student where sno=#{sno}")
int deleteStuBySno(Integer sno);
int deleteStuBySno(Integer sno);
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper stuMapper;
//事务处理在类上面
@Override
public int deleteStuBySno(Integer sno) {
return stuMapper.deleteStuBySno(sno);
}
}
//根据学生编号删去学生
@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;
}
在首页index.jsp中
${msg==null?'':msg}
在首页index.jsp中
超链接:在index.jsp中
修改
控制层代码
//去修改页面
@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"%>
修改页面
数据层接口:对应StudentMapper接口
int updateStu(Student stu); //修改学生
映射文件
update student
sname=#{sname},
sex=#{sex},
age=#{age},
phone=#{phone},
address=#{address}
where sno=#{sno}
业务接口:对应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页面
${msg==null?'':msg}
@Insert("insert into student(sname,sex,age,phone,address) values(#{sname},#{sex},#
{age},#{phone},#{address})")
int addStu(Student stu); //增加学生
int addStu(Student stu); //增加学生
@Override
public int addStu(Student stu) {
return stuMapper.addStu(stu);
}
//增加学生
@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;
}
add页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
增加页面