本次整合是基于Maven整合项目,首先放出整合后的文件目录:
整合的总体思路:先启动SpringMVC,然后在MVC中配置引入spring的配置文件从而整合spring,最后在spring配置文件中引入配置mybatis的sqlsessionfactorybean来整合启动mybatis 正式开始 1.引入依赖2.正常流程启动MVC4.0.0 org.example module21.0-SNAPSHOT war 11 11 org.springframework spring-webmvc5.3.1 org.springframework spring-aop5.3.1 org.springframework spring-web5.3.1 org.springframework spring-tx5.2.6.RELEASE org.springframework spring-jdbc5.3.1 javax.servlet javax.servlet-api3.1.0 provided javax.servlet.jsp javax.servlet.jsp-api2.3.3 mysql mysql-connector-java8.0.25 org.thymeleaf thymeleaf-spring53.0.12.RELEASE org.springframework spring-context5.2.5.RELEASE junit junit4.13.2 test com.fasterxml.jackson.core jackson-databind2.13.0 org.projectlombok lombok1.18.22 compile org.mybatis mybatis3.5.7 log4j log4j1.2.17 org.slf4j slf4j-api1.7.32 org.slf4j slf4j-log4j121.7.32 test org.slf4j slf4j-simple1.7.32 test com.alibaba druid1.2.8 org.mybatis mybatis-spring2.0.6
web.xml最终的配置文件如下,正常启动MVC时就是在下方的配置文件去掉前两个
org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:applicationContext.xml DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:SpringMVC.xml 1 DispatcherServlet / characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceResponseEncoding true characterEncodingFilter @Controller @RequestMapping("/Employee") public class EmployeeController { @Autowired private EmployeeServiceImpl employeeService; @RequestMapping("/") public String index() { System.out.println("这是控制层显示首页的方法"); return "index"; } @RequestMapping("/all") public ModelAndView findAllEmployees() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("list"); List employeeList = employeeService.showAllEmployees(); modelAndView.addObject("employeeList",employeeList); System.out.println("这是控制层查询所有账户的方法,查询到的内容为:"+employeeList); return modelAndView; } }
FirstPageController
package cn.ideal.controller;
import cn.ideal.service.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FirstPageController {
@Autowired
private EmployeeServiceImpl employeeService;
@RequestMapping("/")
public String index(){
System.out.println("进入首页控制器");
return "index";
}
}
EmployeeDao
package cn.ideal.dao;
import cn.ideal.domain.Employee;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository(value = "employeeDao")
public interface EmployeeDao {
public List findAllEmployees();
}
Employee
使用了lombok简化了实体类创建
package cn.ideal.domain;
import lombok.Data;
@Data
public class Employee {
private int id;
private String name;
private String gender;
private String email;
private int dept_id;
}
EmployeeService
package cn.ideal.service;
import cn.ideal.domain.Employee;
import java.util.List;
public interface EmployeeService {
public List showAllEmployees();
}
EmployeeServiceImpl
package cn.ideal.service;
import cn.ideal.dao.EmployeeDao;
import cn.ideal.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
@Qualifier("employeeDao")
private EmployeeDao employeeDao;
@Override
public List showAllEmployees() {
System.out.println("进入service层,查询所有员工的数据");
List allEmployees = employeeDao.findAllEmployees();
return allEmployees;
}
}
index.html
ssm
Welcome to SSM
点击查询所有employee
list.html
employeeList
| id | name | gender | dept_id | |
主要参考文章:SSM 框架整合完整流程讲解(IDEA + Maven) - 云+社区 - 腾讯云 (tencent.com)https://cloud.tencent.com/developer/article/1610326
待我们,别时相见时,都已有所成。



