随笔 - 134,  文章 - 0,  评论 - 0,  阅读 - 21295

四:SSM框架整合

Spring+Spring Mvc+Mybatis整合:

代码实现:

1.创建maven工程 pom.xml

  <dependencies>
<!--  spring mvc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
<!--    spring JDBC-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
<!--    Spirng Aop-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
<!--    Mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <!--     mysql的驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>
<!--    C3P0-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
<!--    JSTL-->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
<!--    ServletAPI-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <!--      lombox-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.10</version>
    </dependency>
  </dependencies>

2.依次搭建 Web.xml

配置Spring Mvc 、Spring 设置字符编码过滤器、加载静态资源

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--  Spring-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:Spring.xml</param-value>
  </context-param>
  <!--  监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--  SpingMvc-->
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--  字符编码过滤器-->
  <filter>
    <filter-name>character</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>
    <!--    请求相应编码-->
    <init-param>
      <param-name>forceRequestEncoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceResponseEncoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>character</param-name>
      <param-value>/*</param-value>
    </init-param>
  </filter>
  <!--    加载静态资源-->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.js</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.png</url-pattern>
  </servlet-mapping>
</web-app>

3创建相应框架的配置文件

spring.xml

springmvx.xml

mybatis.xml


  • Spring.xml


    xml<br /><br /></p></li> </ul> ¨K9K

    ¨K23K

    ¨K10K

    ¨K24K

    ¨K11K

    ¨K25K

    ¨K12K

    ¨K26K

    ¨K13K

    ¨K27K

    </beans>


    • mybatis.xml

      xml<br /><br /><br /><br /> <br /> <br /> <br /> </p></li> </ul> ¨K14K

      ¨K28K

      ¨K15K

      ¨K29K

      ¨K16K

      ¨K30K

      </configuration>

      • Springmvc.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:mvc="http://www.springframework.org/schema/cache"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
          <mvc:annotation-driven/>
          <!--    配置扫包-->
          <context:component-scan base-package="com.southwind"></context:component-scan>
          <!--    视图解析器-->
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
              <!--        前缀-->
              <property name="prefix" value="/"></property>
              <!--        后缀-->
              <property name="suffix" value=".jsp"></property>
          </bean>
        </beans>
        

      4.实体类

      package com.southwind.entity;
      
      import lombok.Data;
      
      @Data
      public class User {
          private Integer id;
          private String name;
          private Integer money;
      }
      

      5.Handeler

      package com.southwind.controller;
      
      import com.southwind.serivce.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;
      
      @Controller
      @RequestMapping("/user")
      public class UserHandler {
          @Autowired
          private UserService userService;
          @GetMapping("index")
          public ModelAndView index(){
              ModelAndView modelAndView=new ModelAndView();
              modelAndView.setViewName("index");
              modelAndView.addObject("list",userService.findAll());
              return modelAndView;
          }
      }
      

      6.业务Service

      package com.southwind.serivce;
      
      import com.southwind.entity.User;
      
      import java.util.List;
      
      public interface UserService {
          public List<User>  findAll();
      }
      
      package com.southwind.serivce.impl;
      
      import com.southwind.entity.User;
      
      import com.southwind.repository.UserRepository;
      import com.southwind.serivce.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      import java.util.List;
      
      @Service
      public class UserServiceImpl implements UserService {
          @Autowired
          private UserRepository userRepository;
          @Override
          public List<User> findAll() {
              return userRepository.findAll();
          }
      }
      

      7.持久层:Mybatis

      package com.southwind.repository;
      
      import com.southwind.entity.User;
      
      import java.util.List;
      
      public interface UserRepository {
          public List<User> findAll();
      }
      
      <?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.shouthwind.repository.UserRepository" >
          <select id="findAll" resultType="User">
              select *from  people
          </select>
      </mapper>
      

      8.jsp视图

      <%--
        Created by IntelliJ IDEA.
        User: 郝泾钊
        Date: 2022-04-09
        Time: 21:43
        To change this template use File | Settings | File Templates.
      --%>
      <%@ page contentType="text/html;charset=UTF-8" language="java" %>
      <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
      <%@page isELIgnored="false" %>
      <html>
      <head>
          <title>Title</title>
      </head>
      <body>
          <c:forEach items="${list}" var="user">
              ${user.id}-${user.name}-${user.money}
          </c:forEach>
      </body>
      </html>
      
posted on   Steam残酷  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示