14.SSM整合

一:ContextLoaderListener

Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口,可监听 ServletContext的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器。web 应用中必须在web.xml中配置

    
    
        
        org.springframework.web.context.ContextLoaderListener
    

    
        contextConfigLocation
        classpath:spring.xml
    

二:准备工作

①创建Maven Module

②导入依赖

    war
    
        5.3.23
    
    
    
        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.11
    
    
    
        org.mybatis
        mybatis-spring
        2.0.6
    
    
    
        com.alibaba
        druid
        1.0.9
    
    
    
        junit
        junit
        4.12
        test
    
    
    
        mysql
        mysql-connector-java
        8.0.29
    
    
    
        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
        
    

三:配置web.xml

    
    
        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的配置文件并配置

    
    
    
    
        
        
        
            
                
                    
                        
                        
                        
                        
                        
                        
                    
                
            
        
    
    
    
    
    
    
    
    
    

五:搭建MyBatis环境

①创建属性文件jdbc.properties

jdbc.driver=com.mysql.cj.jdbc
jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC
jdbc.username=root
jdbc.password=abc123

②创建MyBatis的核心配置文件mybatis-config.xml

    
    
        
        
    
    
        
        
    

③创建Mapper接口和映射文件 

public interface EmployeeMapper {
}


④创建日志文件log4j.xml




    
        
        
            
        
    
    
        
    
    
        
    
    
        
        
    

六:创建Spring的配置文件并配置

    
    
        
    
    
    
    
    
        
        
        
        
    
    
    
        
    
    
    
    
    
        
        
        
        
        
        
        
        
    
    
    
        
    

七:测试功能

①创建组件

实体类Employee

public class Employee {
    private Integer empId;
    private String empName;
    private Integer age;
    private String sex;
    private String email;

    public Employee() {
    }

    public Employee(Integer empId, String empName, Integer age, String sex, String email) {
        this.empId = empId;
        this.empName = empName;
        this.age = age;
        this.sex = sex;
        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 getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    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 +
                ", sex='" + sex + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

创建控制层组件EmployeeController

/**
 * 查询所有的员工信息-->/employee-->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";
    }
}

创建接口EmployeeService

public interface EmployeeService {
    /**
     * 查询所有的员工信息
     * @return
     */
    List getAllEmployee();
}

创建实现类EmployeeServiceImpl

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public List getAllEmployee() {
        return employeeMapper.getAllEmployee();
    }
}

在Mapper接口中添加方法

public interface EmployeeMapper {
    /**
     * 查询所有的员工信息
     * @return
     */
    List getAllEmployee();
}

 在Mapper配置文件中写我们需要操作的sql语句


    

②创建页面

index.html

index.html

查询所有的员工信息

employee_list.html

    
流水号 员工姓名 年龄 性别 邮箱 操作
删除 修改

③访问测试分页功能

controller层方法:

    @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";
    }

 service接口以及实现类

    /**
     * 获取员工的分页信息
     * @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);
        return page;
    }

首页,index.html 

index.html

查询所有的员工信息

employee_list.html




    
    员工列表
    


流水号 员工姓名 年龄 性别 邮箱 操作
删除 修改

你可能感兴趣的:(SpringMVC框架,Spring框架,MyBatis框架,mybatis,java,spring)