• Spring + Spring Mvc + MyBatis 整合


    1. Spring 和 SpringMvc的整合

    1.1 先新建一个 JavaWeb 项目,目录结构如下:

    工程结构

    我们要做的有:
    1. 配置 web.xml
    2. 配置 spring:Application-main.xml
    3. 配置 springmvc: Application-mvc.xml

    1.2 首先需要修改 web.xml,让其能加载spring,springmvc 和 log4j的配置文件。添加如下三部分:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring/ApplicationContext-main.xml
            </param-value>
        </context-param>
    
        <context-param>
            <param-name>log4jConfigLocation</param-name>
            <param-value>classpath:log4j.properties</param-value>
        </context-param>
    
        <servlet>
            <servlet-name>springMvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>springMvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    

    1.3 接下来配置 spring 的配置文件,我想用注解开发,所以配置如代码注释所示:

    <?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/aop 
                            http://www.springframework.org/schema/aop/spring-aop.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
        ">
    
        <!-- 启用注解开发 -->
        <context:annotation-config/>
    
        <!-- 启用组件扫描,但是排除 @Controller 组件,该组件由 SpringMvc 配置文件扫描 -->
        <context:component-scan base-package="com.shuiyujie">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    </beans>

    1.4 spring mvc 配置文件配置如下:,注意视图解析器的写法:

    <?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:mvc="http://www.springframework.org/schema/mvc"
        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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <mvc:annotation-driven/>
        <mvc:default-servlet-handler/>
    
        <context:component-scan base-package="com.shuiyujie.controller"/>
    
        <!-- SpringMvc 视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"/>
            <property name="suffix"     value=".jsp"/>
        </bean>
    </beans>
    

    接下来在 WEB-INF 中添加页面,编写 Controller 测试。如果能够正常访问页面表示spring + spring mvc 整合成功:

    整合成功1

    2. 与 MyBatis整合

    要完成以下内容:
    1. 配置 mybatis: mybatis-congig.xml
    2. 需要连接数据库进行操作,所以要添加数据库的配置文件: dbconfig.properties
    2. 添加 Applciation-datasource.xml : 扫描 mybatis 的配置文件,加载数据库连接池等
    3. 还需要修改 web.xml 让其能够加载 Applciation-datasource.xml

    2.1 mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"  
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <settings> 
            <setting name="cacheEnabled" value="true" /><!-- 全局映射器启用缓存 -->   
            <setting name="useGeneratedKeys" value="true" /> 
            <setting name="defaultExecutorType" value="REUSE" /> 
        </settings>
    
        <typeAliases>
            <!-- 这里添加生成的实体类 -->
    
            <typeAlias type="com.shuiyujie.entity.system.User" alias="User"/>
    
            <!-- 添加生成的实体类结束 -->
    
        </typeAliases>
    
    </configuration>

    2.2 dbconfig.properties

    这里我用的是阿里 druid数据库连接池

    #数据源
    url:jdbc:mysql://localhost:3306/nonglang?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 
    driverClassName:com.mysql.jdbc.Driver
    username:root
    password:123
    filters:stat
    maxActive:20
    initialSize:1
    maxWait:60000
    minIdle:10
    maxIdle:15
    timeBetweenEvictionRunsMillis:60000
    minEvictableIdleTimeMillis:300000
    validationQuery:SELECT 'x'
    testWhileIdle:true
    testOnBorrow:false
    testOnReturn:false
    maxOpenPreparedStatements:20
    removeAbandoned:true
    removeAbandonedTimeout:1800
    logAbandoned:true

    2.3 Applciation-datasource.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/aop 
                            http://www.springframework.org/schema/aop/spring-aop.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
        ">
    
        <!-- sql会话模版 -->
        <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
            <constructor-arg ref="sqlSessionFactory" />
        </bean>
        <!-- 配置mybatis -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:mybatis1/mybatis-config.xml"></property>
            <!-- mapper扫描 -->
            <property name="mapperLocations" value="classpath:mybatis1/*/*.xml"></property>
        </bean>
        <!-- 阿里 druid数据库连接池 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
             <!-- 数据库基本信息配置 -->
             <property name="url" value="${url}" />  
             <property name="username" value="${username}" />  
             <property name="password" value="${password}" />  
             <property name="driverClassName" value="${driverClassName}" />  
             <property name="filters" value="${filters}" />  
             <!-- 最大并发连接数 -->
             <property name="maxActive" value="${maxActive}" />
             <!-- 初始化连接数量 -->
             <property name="initialSize" value="${initialSize}" />
             <!-- 配置获取连接等待超时的时间 -->
             <property name="maxWait" value="${maxWait}" />
             <!-- 最小空闲连接数 -->
             <property name="minIdle" value="${minIdle}" />  
             <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
             <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
             <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
             <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />  
             <property name="validationQuery" value="${validationQuery}" />  
             <property name="testWhileIdle" value="${testWhileIdle}" />  
             <property name="testOnBorrow" value="${testOnBorrow}" />  
             <property name="testOnReturn" value="${testOnReturn}" />  
             <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" />
             <!-- 打开removeAbandoned功能 -->
             <property name="removeAbandoned" value="${removeAbandoned}" />
             <!-- 1800秒,也就是30分钟 -->
             <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
             <!-- 关闭abanded连接时输出错误日志 -->   
             <property name="logAbandoned" value="${logAbandoned}" />
        </bean>  
        <!-- 事物处理 -->
        <aop:config>
            <aop:pointcut id="pc" expression="execution(* com.shuiyujie.service..*(..))" />
            <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
        </aop:config>
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="delete*" propagation="REQUIRED" read-only="false" 
                           rollback-for="java.lang.Exception"/>
                <tx:method name="insert*" propagation="REQUIRED" read-only="false" 
                           rollback-for="java.lang.Exception" />
                <tx:method name="update*" propagation="REQUIRED" read-only="false" 
                           rollback-for="java.lang.Exception" />
                <tx:method name="save*" propagation="REQUIRED" read-only="false" 
                           rollback-for="java.lang.Exception" />
            </tx:attributes>
        </tx:advice>
        <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    </beans>

    注意:这里对配置文件的加载

    <property name="configLocation" value="classpath:mybatis1/mybatis-config.xml"></property>

    为了方便方便以后配置文件统一配置,我把配置文件的加载统一写到了 Application-main.xml 中,即 spring 的配置文件中,添加如下代码加载数据库配置文件:

    <!-- 读取参数配置 -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations">  
                <list>  
                     <value>/WEB-INF/classes/dbconfig.properties</value>
                </list>  
            </property>  
        </bean> 
    

    以上就是 SSM 框架的配置,接下来是 SSM 框架实现增删改查

  • 相关阅读:
    收藏的网站
    记录
    在我的收藏列表里取消收藏功能的实现(不使用直接操作dom的方法)
    uniapp预览图片
    uni-app 中如何打开外部应用,如:浏览器、淘宝、AppStore、QQ等
    uniapp打包上架ios
    uniapp实现倒计时
    uniapp实现支付功能
    uniapp关闭页面回弹效果
    uniapp中使用websocket实现实时聊天功能
  • 原文地址:https://www.cnblogs.com/shuiyj/p/13185245.html
Copyright © 2020-2023  润新知