SSM框架整合

Java-SSM框架整合

SSM框架整合后的目录结构:
在这里插入图片描述

1.Spring

- 配置依赖(pom.xml)
配置maven工程中的依赖jar包,如Junit、数据库驱动、数据库连接池、Mybatis等等
junit
mysql-connector-java
c3p0
servlet-api
jsp-api
jstl
mybatis
mybatis-spring
spring-webmvc
spring-jdbc
aspectjweaver
aspectjrt等等,按你的需求导入,导入时注意版本问题,再配置一个静态资源导出问题的build

<build>
	<resources>
		<resource>
            <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
               <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
           		</includes>
           <filtering>false</filtering>
       </resource>
    </resources>
</build>
  • 自动扫描配置(applicationContext.xml)
<context:component-scan base-package="com.huang.ssmbuild">
     <!-- Spring不扫描控制器 -->
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
  • Spring启动监听器(web.xml)
	<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>

2.Mybatis

  1. 引入数据源配置(applicationContext.xml)
    创建db.properties
    在applicationContext.xml中引入db.properties

db.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssmbuild?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.user=
jdbc.password=
jdbc.minPoolSize=10
jdbc.maxPoolSize=20
jdbc.maxIdleTime=1800
jdbc.initialPoolSize=2
jdbc.acquireIncrement=2
jdbc.idleConnectionTestPeriod=1800
jdbc.acquireRetryAttempts=2
<context:property-placeholder location="classpath*:db.properties"/>
    <!-- 数据源配置:c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据库驱动 -->
        <property name="driverClass" value="${jdbc.driver}" />
        <!-- 数据库连接串 -->
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
        <!-- 数据库用户 -->
        <property name="user" value="${jdbc.user}" />
        <!-- 数据库密码 -->
        <property name="password" value="${jdbc.password}" />
        <!-- 最小连接数 -->
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <!-- 最大空闲的时间,单位是秒,无用的链接再过时后会被回收 -->
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
        <!-- 数据库连接池初始化时获取的连接数  -->
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}" />
        <!-- 在当前连接数耗尽的时候,一次获取的新的连接数 -->
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}" />
        <!-- 每隔1800S检查数据库空闲连接 -->
        <property name="idleConnectionTestPeriod"
                  value="${jdbc.idleConnectionTestPeriod}" />
        <!-- 当数据库连接失败以后尝试 重新连接的次数 -->
        <property name="acquireRetryAttempts" value="${jdbc.acquireRetryAttempts}" />
    </bean>
  1. 配置mybatis整合(applicationContext.xml)
    mybatis整合时,mybatis-config.xml中可以自己配置一些全局配置,但要注意配置扫描mapper的地址
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- mybatis全局配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml">
        </property>
        <!-- 配置数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 配置扫描mapper -->
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    </bean>

  
  1. 配置扫描器:将mybatis接口实现加入到ioc容器中(applicationContext.xml)
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有的dao接口的实现,加入到ioc容器中 -->
        <property name="basePackage" value="com.huang.ssmbuild.dao"/>
    </bean>
  1. 事务控制等(applicationContext.xml)
	<bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 开启注解支持的事务控制,比较重要的使用配置实现 -->
    <aop:config>
        <!-- 配置切入点 -->
        <aop:pointcut expression="execution(* com.huang.ssmbuild.service..*(..))" id="txPoint"/>
        <!-- 配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!-- 配置事务增强 -->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <!-- 所有方法都是事务方法-->
            <tx:method name="*" />
            <!-- 所有get开头的方法,配置优化 -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>

3.SpringMVC

  • 扫描容器组件(扫描Controller)(springMVC.xml)
	<context:component-scan base-package="com.huang.ssmbuild" use-default-filters="false">
        <!-- 只扫描controller-->
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
  • 配置视图解析器(springMVC.xml)
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--url前缀-->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <!--url后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 3. 两个标准配置:将springMVC不能处理的请求交给默认处理器处理[tomcat]-->
    <mvc:default-servlet-handler />

    <!-- 添加注解支持、映射动态请求、JSR303校验、快捷AJAX等 -->
    <mvc:annotation-driven/>
  • 在web.xml中配置SpringMVC前端控制器DispatcherServlet、配置编码过滤等
	<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. 配置编码过滤,spring4以后需要配置请求/响应编码 -->
    <filter>
        <filter-name>characterEncoding</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>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值