• 15、spring+mybatis+servlet框架搭建


    1、新建一个maven项目

    选择模板

    image-20211027064211337

    添加参数

    archetypeCatalog=internal

    image-20211027064522748

    等待maven项目构建完成

    控制台出现BUILD SUCCESS提示,并且项目结构完整(有src,main,webapp等几个包)即构建完成

    image-20211027064845071

    2、导入依赖

    导入ssm框架项目中需要用到的jar包,替换项目中的properties标签和dependencies标签

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.7maven.compiler.source>
        <maven.compiler.target>1.7maven.compiler.target>
        <spring.version>5.2.12.RELEASE spring.version>
      properties>
    
      <dependencies>
        <dependency>
          <groupId>junitgroupId>
          <artifactId>junitartifactId>
          <version>4.11version>
          <scope>testscope>
        dependency>
    
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatisartifactId>
          <version>3.4.6version>
        dependency>
    
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>4.0.1version>
        dependency>
    
        
        <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>jsp-apiartifactId>
          <version>2.0version>
        dependency>
    
        
        <dependency>
          <groupId>javax.servlet.jsp.jstlgroupId>
          <artifactId>jstl-apiartifactId>
          <version>1.2version>
        dependency>
        
        <dependency>
          <groupId>taglibsgroupId>
          <artifactId>standardartifactId>
          <version>1.1.2version>
        dependency>
    
        
        <dependency>
          <groupId>mysqlgroupId>
          <artifactId>mysql-connector-javaartifactId>
          <version>5.1.48version>
        dependency>
    
        
        <dependency>
          <groupId>net.sf.json-libgroupId>
          <artifactId>json-libartifactId>
          <version>2.4version>
          <classifier>jdk15classifier>
        dependency>
    
        
        <dependency>
          <groupId>com.alibabagroupId>
          <artifactId>fastjsonartifactId>
          <version>1.2.76version>
        dependency>
    
    
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>${spring.version}version>
        dependency>
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-coreartifactId>
          <version>${spring.version}version>
        dependency>
        
        <dependency>
          <groupId>org.aspectjgroupId>
          <artifactId>aspectjweaverartifactId>
          <version>1.9.4version>
        dependency>
        
        <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-jdbcartifactId>
          <version>${spring.version}version>
        dependency>
    
        
        <dependency>
          <groupId>org.mybatisgroupId>
          <artifactId>mybatis-springartifactId>
          <version>2.0.5version>
        dependency>
    
      dependencies>
    
    • 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
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    3、配置web.xml文件

    image-20211027071226381

    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 Applicationdisplay-name>
      <context-param>
        
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
      context-param>
      
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
      listener>
    web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    4、在项目的main目录下创建Java文件夹和resources文件夹

    image-20211027071657059

    如果没有图示的文件夹,则先创建一个普通的文件夹,然后再选中文件夹按鼠标右键,把文件夹标记为对应的类型即可

    image-20211027071858123

    5、在resources根目录下创建Spring和mybatis的配置文件

    image-20211027072048431

    mybatis-config.xml

    配置mybatis中写sql语句的xml文件

    
    DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        
        <mappers>
            <mapper resource="mapper/ForeUserMapper">mapper>
        mappers>
    configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    applicationContext

    配置扫描器,以及和mybatis的整合

    
    <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"
           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">
        
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/train12306-2?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        bean>
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="datasource"/>
            
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
        bean>
    
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
            <property name="basePackage" value="com.cykj.mapper">property>
        bean>
    
    
        
        <context:component-scan base-package="com.cykj"/>
    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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    6、在java目录创建包com.cykj并在cykj包下面创建四个包

    image-20211027072836310

    7、编写测试的所有代码,测试spring与mybatis是否可用

    8、Servlet中本来不能使用@Autowired注入bean,解决办法修改每个servlet中的init方法

    package com.cykj.web;
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.support.SpringBeanAutowiringSupport;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    
    public class BaseServlet extends HttpServlet {
        private ApplicationContext app;
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            //Servlet中本来不能使用@Autowired注入bean,解决办法是在Servlet的init(ServletConfig)方法中调用
            SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
            app = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        }
        public Object getObject(Class c) {
            return app.getBean(c);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    anAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
    app = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    }
    public Object getObject(Class c) {
    return app.getBean©;
    }
    }

    
    
    • 1
  • 相关阅读:
    虚拟机上安装集群kafka
    元宇宙区块链协议Meta0宣布与Polygon建立合作关系
    MFC Windows 程序设计[130]之页面常用控件组
    【机器学习】近邻类模型:KNN算法在数据科学中的实践与探索
    python爬虫之app爬取-mitmproxy 的使用
    c语言入门--数组
    中端酒店迈入“30+”,维也纳酒店如何化解行业的三大难关
    探讨安科瑞低压备自投装置AM5-DB的应用-安科瑞 蒋静
    20 - 欲知JVM调优先了解JVM内存模型
    哈夫曼编码与二叉字典树
  • 原文地址:https://blog.csdn.net/qq_37917691/article/details/126413460