关于SSM配置的知识总结(自用)

1,创建maven工程
2,导入项目依赖的jar包

  • spring
    • spring-webmvc
    • spring-aspects
  • springMVC
    -spring-jdbc
  • mybatis
  • spring整合mybatis
  • 数据库、连接池
    -c3p0
  • 驱动
    -mysql-connector-java
  • 其他(jstl、servlet-api、junit)

3,引入bootstrap前端框架

  1. 到bootstrap官网下载
  2. 复制到项目
  3. 引入jsp页面<link href="static/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
  4. 下载并引入<script type="text/javascript" src="static/js/jquery-3.5.1.min.js"></script>

4,编写ssm整合的关键配置文件之web.xml

  1. 启动spring容器

    1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点listener和contex-param。
       <!--  启动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>
      
    2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。接着容器会将读取到context-param转化为键值对,并交给ServletContext。
    3. 容器创建listener中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)

    2,配置springMVC拦截器,拦截所有请求

    				<servlet>
    			    <servlet-name>springDispatcherServlet</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>
    			    <load-on-startup>1</load-on-startup>
    			  </servlet>
    			
    			<servlet-mapping>
    			  <servlet-name>springDispatcherServlet</servlet-name>
    			<!--  拦截所以请求-->
    			  <url-pattern>/</url-pattern>
    			</servlet-mapping>
    		```
    		
    3,配置字符编码过滤器(可以解决中文乱码问题)
    	```
    			<filter>
    		    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    		    <url-pattern>/*</url-pattern>
    		  </filter-mapping>
    	```
    

5,编写ssm整合的关键配置文件之springMVC.xml

  1. 配置网站跳转逻辑Contorller

    <!--    配置网站跳转逻辑-->
    <context:component-scan base-package="com.ssmCRUD" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    

    2.配置视图解析器

    <!--配置视图解析器-->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	    <property name="prefix" value="/WEB-INF/"></property>
    	    <property name="suffix" value=".jsp"></property>
    	 </bean>
    

    3.其他标准配置
    <mvc:default-servlet-handler/>
    将springmvc不能处理的请求交给Tomcat
    <mvc:annotation-driven/>
    能支持springMVC更高级的一些功能 jsr303校验,映射动态请求等

6,编写ssm整合的关键配置文件之applicationContext.xml

  1. 配置context:component-scan,管理除了Controller的类。在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean

    <context:component-scan base-package="com.ssmCRUD">
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
  2. 配置数据源

    <context:property-placeholder location="dbconfig.properties"/>
    
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    	<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    	<property name="driverClass" value="${jdbc.driverClass}"></property>
     	<property name="user" value="${jdbc.user}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    </bean>
    
  3. 和mybatis整合

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="pooledDataSource"/>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
   	</bean>
  1. 配置扫描器,扫描所有dao接口的实现加入到ioc中
<!--    配置扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--        扫描所以dao接口的实现,加入到ioc容器中-->
        <property name="basePackage" value="com.ssmCRUD.dao"/>
    </bean>
  1. 配置事务
<!--    配置事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--        控制住数据源-->
        <property name="dataSource" ref="pooledDataSource"/>
    </bean>
  1. 开启基于注解的事务或者基于配置文件的事务(此处为配置文件)
<!--    开启基于注解的事务,使用基于配置的(主要)-->
    <aop:config>
<!--        切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.ssmCRUD.service..*(..))"/>
<!--         配置事务增强-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
  1. 事务增强
<!--    配置事务增强,事务如何切入-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager>
        <tx:attributes>
<!--            所以方法都是事务-->
            <tx:method name="*"/>
<!--            所以以get开头的方法都认为是查询-->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

7,配置mybatis-config.xml

<configuration>
<!--    开启??模式-->
    <settings>
        <setting name="mapUnderscoreToCameLCase" value="true"/>
    </settings>
<!--    类型别名-->
    <typeAliases>
        <package name="com.ssmCRUD.bean"/>
    </typeAliases>
</configuration>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM(Spring + SpringMVC + MyBatis)是一种经典的JavaWeb开发框架,下面简单介绍一下SSM配置。 1. 配置Spring:在Spring配置文件,需要配置数据源、事务管理器、Spring扫描包、Spring MVC等相关内容。 2. 配置SpringMVC:在Spring MVC配置文件,需要配置视图解析器、处理器映射、拦截器等相关内容。 3. 配置MyBatis:在MyBatis配置文件,需要配置数据源、Mapper映射文件、SqlSessionFactory等相关内容。 4. 整合SSM:在整合SSM时,需要在web.xml配置DispatcherServlet和ContextLoaderListener,并将Spring和Spring MVC的配置文件引入。 下面是一个简单的SSM配置示例: 1. Spring配置文件 applicationContext.xml: ``` <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- MyBatis配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mappers/*.xml" /> </bean> <!-- 事务管理器配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Spring扫描包 --> <context:component-scan base-package="com.example" /> ``` 2. Spring MVC配置文件 springmvc.xml: ``` <!-- 视图解析器配置 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 处理器映射配置 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" /> <!-- 拦截器配置 --> <mvc:interceptors> <bean class="com.example.interceptor.LoginInterceptor" /> </mvc:interceptors> ``` 3. MyBatis配置文件 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> <settings> <setting name="cacheEnabled" value="true" /> </settings> </configuration> ``` 4. web.xml配置文件: ``` <!-- Spring MVC配置 --> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值