SSM(Spring,Spring MVC,MyBatis)因为Spring MVC本身就是Spring框架的一部分,所以只需要引入框架就行。
如果可以通过前台页面执行查询方法,并且数据可以在页面正确显示,说明三个框架整合成功
用到的包:
流程如下:
第一步,编写配置文件,在项目文件夹下建立一个配置的文件夹,命名config
参照之前MyBatis和Spring框架的整合,配置文件db.properties,applicationConnect.xml基本相同
关于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> <!-- 别名定义 --> <typeAliases> <package name="com.itheima.po" /> </typeAliases> </configuration>
第二步,创建Spring MVC的配置文件springmvc-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 配置包扫描器,扫描@Controller注解的类 --> <context:component-scan base-package="com.itheima.controller" /> <!-- 加载注解驱动 --> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
第三步,编写web.xml文件,配置Spring的文件监视器,编码过滤器以及Spring MVC的前端控制器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 配置加载Spring文件的监听器--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 编码过滤器 --> <filter> <filter-name>encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*.action</url-pattern> //处理以.action为结尾的请求 </filter-mapping> <!-- 配置Spring MVC前端核心控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <!-- 配置服务器启动后立即加载Spring MVC配置文件 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--/:拦截所有请求(除了jsp)--> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第四步,在po包创建持久类
package com.itheima.po; /** * 客户持久化类 */ public class Customer { private Integer id; // 主键id private String username; // 客户名称 private String jobs; // 职业 private String phone; // 电话 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getJobs() { return jobs; } public void setJobs(String jobs) { this.jobs = jobs; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
第五步,dao包,创建接口文件,CustomerDao以及其对应映射文件CustomerDao.xml
package com.itheima.dao; import com.itheima.po.Customer; /** * Customer接口文件 */ public interface CustomerDao { /** * 根据id查询客户信息 */ public Customer findCustomerById(Integer id); }
<?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.itheima.dao.CustomerDao"> <!--根据id查询客户信息 --> <select id="findCustomerById" parameterType="Integer" resultType="Customer"> select * from t_customer where id = #{id} </select> </mapper>
第六步,service包下创建接口文件CustomerService
import com.itheima.po.Customer; public interface CustomerService { public Customer findCustomerById(Integer id); }
第七步,service.impl包下创建CustomerService接口的实现类CustomerServiceImpl
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.itheima.dao.CustomerDao; import com.itheima.po.Customer; import com.itheima.service.CustomerService; @Service @Transactional public class CustomerServiceImpl implements CustomerService { //注解注入CustomerDao @Autowired private CustomerDao customerDao; //查询客户 public Customer findCustomerById(Integer id) { return this.customerDao.findCustomerById(id); } }
第八步,controller包,在包中创建用于处理页面请求的控制器类CustomerController
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 com.itheima.po.Customer; import com.itheima.service.CustomerService; @Controller public class CustomerController { @Autowired private CustomerService customerService; /** * 根据id查询客户详情 */ @RequestMapping("/findCustomerById") public String findCustomerById(Integer id,Model model) { Customer customer = customerService.findCustomerById(id); model.addAttribute("customer", customer); //返回客户信息展示页面 return "customer"; } }
第九步,创建jsp文件
<%@ 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> <table border=1> <tr> <td>编号</td> <td>名称</td> <td>职业</td> <td>电话</td> </tr> <tr> <td>${customer.id}</td> <td>${customer.username}</td> <td>${customer.jobs}</td> <td>${customer.phone}</td> </tr> </table> </body> </html>
el表达式来获取后台控制层返回的客户信息