2021.10.20 SSM整合 配置相关文件

1.创建web工程,导入相关依赖,创建文件包和类

        com.qf.controller.UserController

        com.qf.pojo.User

        com.qf.service.UserService(接口)

        com.qf.service.impl.Userservice

        com.qf.mapper.UserMapper(接口)

2.创建db.properties,applicationContext.xml,mybatis-config.xml,springmvc.xml文件

3.配置db.properties

4.web.xml

<!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>
<!--1加载applicationContext.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
<!--2配置监听器,服务器启动时就是applicationContext.xml被加载的时机-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
​
​
    <servlet>
        <servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>xml
 

5.配置applicationContext.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:aop="http://www.springframework.org/schema/aop"
       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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
​
<!--        3-->
<!--    (1)引入配置文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--(2)配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="driverClassName" value="${db.driverClassName}"></property>
    </bean>
<!--  (3)配置SqlSessionFactory  -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置dataSource-->
    <property name="dataSource" ref="dataSource"></property>
<!--    配置mybatis-config.xml-->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
​
<!--(4)扫描Mapper.xml文件-->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.mapper"></property>
</bean>
<!--    事务-->
<!--    AOP-->
</beans>

6.配置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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
​
<!--    4-->
<!--扫描对应包下的注释-->
<context:component-scan base-package="com.qf"></context:component-scan>
<!--配置组件-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--方行静态资源-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
​
<!--    配置视图解析器,上传,下载,全局异常处理,拦截器等等-->
</beans>

7.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>
​
</configuration>

8.配置好后,在Controller类返回findAll方法,service层实例化并返回findAll方法,在mapper层接口接收。resources下创建com/qf/mapper/UserMapper,在此实现。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM(Spring + SpringMVC + MyBatis)是一个基于Spring、Spring MVC和MyBatis的轻量级Java Web开发框架组合,它们在web应用中通常会使用`web.xml`文件配置初始化参数和控制器映射等信息。`web.xml`是Servlet规范的一部分,主要负责定义应用程序的全局行为。 在SSM项目中,`web.xml`的主要配置包括以下几个部分: 1. **Servlet和Filter配置**:这里会定义Spring MVC的DispatcherServlet,以及可能使用的过滤器(如字符编码过滤器、日志记录过滤器等)。 ```xml <web-app> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ... <filter> <filter-name>encodingFilter</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> ... </web-app> ``` 2. **Multipart Config**:如果项目需要处理文件上传,会配置MultipartResolver。 ```xml <multipart-config> <location>/WEB-INF/upload</location> <max-file-size>10MB</max-file-size> <max-request-size>100MB</max-request-size> </multipart-config> ``` 3. **ContextLoaderListener**:用于加载Spring的上下文。 ```xml <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 4. **Controller映射**:通过`<url-pattern>`标签指定请求URL和哪个控制器方法关联。 ```xml <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值