war 5.3.1 org.springframework spring-context ${spring.version} org.springframework spring-beans ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-aspects ${spring.version} org.springframework spring-test ${spring.version} org.mybatis mybatis 3.5.7 org.mybatis mybatis-spring 2.0.6 com.alibaba druid 1.0.9 junit junit 4.12 test mysql mysql-connector-java 8.0.16 log4j log4j 1.2.17 com.github.pagehelper pagehelper 5.2.0 ch.qos.logback logback-classic 1.2.3 javax.servlet javax.servlet-api 3.1.0 provided com.fasterxml.jackson.core jackson-databind 2.12.1 commons-fileupload commons-fileupload 1.3.1 org.thymeleaf thymeleaf-spring5 3.0.12.RELEASE
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
SpringMVC
/
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring.xml
springmvc.xml
spring.xml
package com.atguigu.ssm.controller;
/**
* 查询所有的员工信息-->/employee-->get
* 查询员工的分页信息-->/employee/page/1-->get
* 根据id查询员工信息-->/employee/1-->get
* 跳转到添加页面-->/to/add-->get
* 添加员工信息-->/employee-->post
* 修改员工信息-->/employee-->put
* 删除员工信息-->/employee/1-->delete
*/
@Controller
public class EmployeeController {
}
首页
index.html
package com.atguigu.ssm.service;
public interface EmployeeService {
}
package com.atguigu.ssm.service.impl;
import com.atguigu.ssm.service.EmployeeService;
import org.springframework.stereotype.Service;
@Service
public class EmployeeServiceImpl implements EmployeeService {
}
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
package com.atguigu.ssm.mapper;
public interface EmployeeMapper {
}
普通工厂和工厂bean的区别
普通工厂:如果是一个普通的工厂 然后我们把工厂配置到IOC容器中 要先获取IOC 在获取工厂的bean 然后在通过工厂获得工厂所提供的对象
工厂bean: 可以省略一个获取工厂的步骤 我们可以直接获取工厂所提供的对象
所以我们当前把SqlSessionFactoryBean配置到IOC容器中之后 就可从IOC容器直接获取到SqlSessionFactory对象
配置好SqlSessionFactoryBean后 就可以直接装配sqlSessionFactory对象
删除mybatis-config.xml里面的数据源和引入实体所在的包 最后变成如下
package com.atguigu.ssm.pojo;
public class Employee {
private Integer empId;
private String empName;
private Integer age;
private String gender;
private String email;
public Employee() {
}
public Employee(Integer empId, String empName, Integer age, String gender, String email) {
this.empId = empId;
this.empName = empName;
this.age = age;
this.gender = gender;
this.email = email;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", email='" + email + '\'' +
'}';
}
}
查询完信息要把它展示在页面上 所以我们要把它共享到域对象中
package com.atguigu.ssm.controller;
import com.atguigu.ssm.pojo.Employee;
import com.atguigu.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
/**
* Date:2022/7/11
* Author:ybc
* Description:
* 查询所有的员工信息-->/employee-->get
* 查询员工的分页信息-->/employee/page/1-->get
* 根据id查询员工信息-->/employee/1-->get
* 跳转到添加页面-->/to/add-->get
* 添加员工信息-->/employee-->post
* 修改员工信息-->/employee-->put
* 删除员工信息-->/employee/1-->delete
*/
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public String getAllEmployee(Model model){
//查询所有的员工信息
List list = employeeService.getAllEmployee();
//将员工信息在请求域中共享
model.addAttribute("list", list);
//跳转到employee_list.html
return "employee_list";
}
}
package com.atguigu.ssm.service;
import com.atguigu.ssm.pojo.Employee;
import java.util.List;
public interface EmployeeService {
/**
* 查询所有的员工信息
* @return
*/
List getAllEmployee();
}
package com.atguigu.ssm.service.impl;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeMapper employeeMapper;
@Override
public List getAllEmployee() {
return employeeMapper.getAllEmployee();
}
}
package com.atguigu.ssm.mapper;
import com.atguigu.ssm.pojo.Employee;
import java.util.List;
public interface EmployeeMapper {
/**
* 查询所有的员工信息
* @return
*/
List getAllEmployee();
}
这里面不用写实现类 因为是mapper 主需要根据mapper接口的方法在所对应的映射文件创建他的sql语句就可以
员工列表
员工列表
流水号
员工姓名
年龄
性别
邮箱
操作
删除
修改
首页
index.html
查询员工的分页信息
@RequestMapping(value = "/employee/page/{pageNum}", method = RequestMethod.GET)
public String getEmployeePage(@PathVariable("pageNum") Integer pageNum, Model model){
//获取员工的分页信息
PageInfo page = employeeService.getEmployeePage(pageNum);
//将分页数据共享到请求域中
model.addAttribute("page", page);
//跳转到employee_list.html
return "employee_list";
}
/**
* 获取员工的分页信息
* @param pageNum
* @return
*/
PageInfo getEmployeePage(Integer pageNum);
@Override
public PageInfo getEmployeePage(Integer pageNum) {
//开启分页功能
PageHelper.startPage(pageNum, 4);
//查询所有的员工信息
List list = employeeMapper.getAllEmployee();
//获取分页相关数据
PageInfo page = new PageInfo<>(list, 5);
首页
index.html
查询员工的分页信息
员工列表
员工列表
流水号
员工姓名
年龄
性别
邮箱
操作
删除
修改
员工列表
员工列表
流水号
员工姓名
年龄
性别
邮箱
操作
删除
修改
limit index,pageSize
pageSize:每页显示的条数
pageNum:当前页的页码
index:当前页的起始索引,index=(pageNum-1)*pageSize
count:总记录数
totalPage:总页数
totalPage = count / pageSize;
if(count % pageSize != 0){
totalPage += 1; }
pageSize=4,pageNum=1,index=0 limit 0,4
pageSize=4,pageNum=3,index=8 limit 8,4
pageSize=4,pageNum=6,index=20 limit 8,4
首页 上一页 2 3 4 5 6 下一页 末页