• SSM框架集成


    SSM

    1 介绍

    SSM框架是指Spring + Spring MVC + MyBatis的组合,是一种常用的Java Web开发框架。这三个框架分别提供了不同的功能:

    • Spring(Spring Framework): 提供了IoC(控制反转)和AOP(面向切面编程)等功能,用于管理Java应用中的对象和组件,并提供了事务管理等企业级特性。

    • Spring MVC: 是Spring框架的一部分,用于构建Web应用程序的MVC框架,提供了基于Java的配置、灵活的URL映射、强大的数据绑定、表单处理和验证等功能。

    • MyBatis: 是一个持久层框架,用于将Java对象与数据库中的记录进行映射,提供了简单的SQL操作和结果映射。

    SSM框架整合这三个框架,可以使得开发者在开发Java Web应用时更加方便和高效。典型的SSM框架的工作流程如下:

    1. 请求到达前端控制器(DispatcherServlet): 所有的请求都会先到达前端控制器,即DispatcherServlet。

    2. DispatcherServlet分发请求: DispatcherServlet根据请求的URL,使用Handler Mapping找到对应的Controller。

    3. Controller处理请求: Controller处理请求,可以调用Service层的方法,Service层包含了业务逻辑。

    4. Service层处理业务逻辑: Service层处理业务逻辑,可能会调用DAO层的方法来访问数据库。

    5. DAO层访问数据库: DAO层使用MyBatis等持久化框架与数据库进行交互。

    6. 返回结果给前端控制器: 数据库返回结果,Service层将结果传递给Controller。

    7. 前端控制器选择视图并返回: Controller选择合适的视图(通常是JSP页面),并将结果传递给前端控制器。

    8. 前端控制器返回响应: 前端控制器将视图渲染成HTML等内容,并返回给客户端。

    2 集成步骤

    目录结构

    在这里插入图片描述

    配置 web.xml

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <filter>
            <filter-name>encodingfilter-name>
            <filter-class>com.kdx.filter.CharSetEncodingfilter-class>
        filter>
    
        <filter-mapping>
            <filter-name>encodingfilter-name>
            <url-pattern>*.actionurl-pattern>
        filter-mapping>
    
        
        <context-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:applicationContext-*.xmlparam-value>
        context-param>
        
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
        listener>
    
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:springmvc.xmlparam-value>
            init-param>
        servlet>
        
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>*.actionurl-pattern>
        servlet-mapping>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    配置 jdbc.properties

    若 mysql 为8.x版本则配置如下

    jdbcs.driver=com.mysql.cj.jdbc.Driver

    若 mysql 为8.x版本之前则配置如下

    jdbcs.driver=com.mysql.jdbc.Driver

    jdbcs.driver=com.mysql.cj.jdbc.Driver 
    jdbcs.url=jdbc:mysql:///mybatis
    jdbcs.uname=root
    jdbcs.pwd=kdx010908
    
    • 1
    • 2
    • 3
    • 4

    配置 SqlMapConfig.xml

    
    DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
            plugin>
        plugins>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    配置 applicationContext-dao.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:context="http://www.springframework.org/schema/context"
    	   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    	   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    	 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    	
    	<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    	
    	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    		  <property name="driverClassName" value="${jdbcs.driver}">property>
    		  <property name="url" value="${jdbcs.url}">property>
    		  <property name="username" value="${jdbcs.uname}">property>
    		  <property name="password" value="${jdbcs.pwd}">property>
    	bean>
    
    	
    	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
    		<property name="dataSource" ref="dataSource">property>
    		<property name="configLocation" value="classpath:SqlMapConfig.xml">property>
    		<property name="typeAliasesPackage" value="com.kdx.entity">property>
    	bean>
    
    	
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="com.kdx.mapper">property>
    	bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    配置 applicationContext-service.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:context="http://www.springframework.org/schema/context"
    	   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    	   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    	 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
    	
    	
    	<context:component-scan base-package="com.kdx.service">context:component-scan>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    配置 log4j.properties

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    编写解决中文乱码的过滤器

    public class CharSetEncoding implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
    
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            chain.doFilter(request,response);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    编写实体类

    public class Items {
        private Integer id;
    
        private String name;
    
        private Float price;
    
        private String pic;
    
        private String detail;
    
        private Date createtime;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name == null ? null : name.trim();
        }
    
        public Float getPrice() {
            return price;
        }
    
        public void setPrice(Float price) {
            this.price = price;
        }
    
        public String getPic() {
            return pic;
        }
    
        public void setPic(String pic) {
            this.pic = pic == null ? null : pic.trim();
        }
    
        public Date getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(Date createtime) {
            this.createtime = createtime;
        }
    
        public String getDetail() {
            return detail;
        }
    
        public void setDetail(String detail) {
            this.detail = detail == null ? null : detail.trim();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    编写 ItemsMapper 接口

    public interface ItemsMapper {
    
        //查询全部的商品
        public List<Items> findAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    编写映射 ItemsMapper.xml

    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.easthome.mapper.ItemsMapper">
    
        <select id="findAll" resultType="com.easthome.entity.Items">
            select * from items
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    编写 ItemsService 接口

    public interface ItemsService {
    
        List<Items> queryAll();
    }
    
    • 1
    • 2
    • 3
    • 4

    编写 ItemsServiceImpl 实现 ItemsService 接口

    @Service
    public class ItemsServiceImpl implements ItemsService {
    
        @Autowired
        private ItemsMapper itemsMapper;
    
        @Override
        public List<Items> queryAll() {
            List<Items> itemsList = itemsMapper.findAll();
            return itemsList;
        } 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    编写 ItemsController

    第一种写法:

    @Controller
    public class ItemsController {
    
        @Autowired
        private ItemsService itemsService;
    
        @RequestMapping("/list")
        public ModelAndView list(){
            ModelAndView mav = new ModelAndView();
            List<Items> itemsList = itemsService.queryAll();
            mav.addObject("itemsList",itemsList);
            mav.setViewName("list");
            return mav;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    第二种写法:

    	@RequestMapping("/list")
        public List<Items> list(){
            //自动封装一个集合数据  itemsList 自动去视图解析器下面找  itemsList.jsp页面
            return itemsService.queryAll();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第三种写法:

        @RequestMapping("/list")
        public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            List<Items> itemsList = itemsService.queryAll();
            request.setAttribute("itemsList",itemsList);
            request.getRequestDispatcher("/WEB-INF/jsps/list.jsp").forward(request,response);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    第四种写法:

        @RequestMapping("/list")
        public void list(Map<String,Object> map) {
            //自动封装一个集合数据  itemsList 自动去视图解析器下面找  itemsList.jsp页面
            map.put("itemsList",itemsService.queryAll());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    最后编写前端页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
        <title>开发视图title>
    head>
    <body>
    <table width="600px" border="1" align="center">
        <tr>
            <td>商品序号td>
            <td>商品名称td>
            <td>商品价格td>
            <td>商品简介td>
            <td>操作td>
        tr>
        <c:forEach items="${requestScope.itemsList}" var="items">
    
            <tr>
                <td>${items.id}td>
                <td>${items.name}td>
                <td>${items.price}td>
                <td>${items.detail}td>
                <td>
                    <a href="${pageContext.request.contextPath}/delItem.action?ids=${items.id}">删除a>
                td>
            tr>
        c:forEach>
    table>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    部署运行:
    在这里插入图片描述

  • 相关阅读:
    蓝牙智能音箱采用哪些音频功放芯片
    WMware Tools安装失败segmentation fault解决方法
    小程序配置服务器域名
    音频驱动嘴型的视频数字人虚拟主播工具motionface replay使用教程
    十三、Django之添加用户(原始方法实现)
    Vue 之 provide和inject的使用
    全波形反演培训的思考与总结
    义乌外贸新趋势:跨境电商引领“内外兼修”
    C语言入门
    Java EasyExcel带格式多线程导出百万数据
  • 原文地址:https://blog.csdn.net/k010908/article/details/133922679