SSM(spring+springmvc+mybatis)整合案例

  1. 项目整体结构
  2. 新建一个动态web工程
  3. 加入所需jar包
  4. mybatis配置文件:sqlMapConfig.xml
  5. <?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>
    
    	<!-- 配置映射类的别名 -->
    	<typeAliases>
    		<typeAlias alias="User" type="com.test.domain.User" />
    	</typeAliases>
    	
    </configuration>
  6. 配置db.properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssm_login?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=root
  7. spring配置文件:spring-mybatis.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:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    					http://www.springframework.org/schema/beans/spring-beans-4.0.xsd	
    					http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    
        <!-- 自动扫描 -->  
        <context:component-scan base-package="com.test" />  
        
     	<!-- 引入配置文件 -->
    	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="prototype">
    		<property name="locations">
    			<list>
    				<value>classpath:db.properties</value>
    			</list>
    		</property>
    	</bean>
    	
    	<!-- 数据库连接池 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
              destroy-method="close">
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="initialSize" value="5"/>
            <property name="minIdle" value="5"/>
            <property name="maxActive" value="50"/>
            <!-- 配置获取连接等待超时的时间 -->
            <property name="maxWait" value="10000"/>
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="300000"/>
    
            <property name="validationQuery" value="SELECT 'x'"/>
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="false"/>
            <property name="testOnReturn" value="false"/>
    
            <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
            <property name="poolPreparedStatements" value="true"/>
            <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
    
            <!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
            <property name="filters" value="stat"/>
        </bean>
        
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <!-- 引入 mybatis 配置文件 -->
            <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
            <property name="typeAliasesPackage" value="com.light.ac.domain"></property>
            <!-- sql配置文件 -->
             <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
             <!-- 分页插件 -->
             <property name="plugins">
    	        <array>
    	            <bean class="com.github.pagehelper.PageInterceptor">
    	                <property name="properties">
    	                    <value>
    	                        helperDialect=mysql
    	                        reasonable=true
    	                        supportMethodsArguments=true
    	                        params=count=countSql
    	                        autoRuntimeDialect=true
    	                    </value>
    	                </property>
    	            </bean>
    	        </array>
    	    </property>
        </bean>
        
         <!-- 扫描Mapper,DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.test.mapper"></property>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
        
        <!-- 通用 Mapper -->
        <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
    	    <property name="basePackage" value="com.isea533.mybatis.mapper"/>
    	    <property name="properties">
    	        <value>
    	            mappers=tk.mybatis.mapper.common.Mapper
    	            ORDER=BEFORE
    	        </value>
    	    </property>
    	</bean>
    
        <!-- 事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!-- 通知 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- 传播行为 -->
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="insert*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
                <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            </tx:attributes>
        </tx:advice>
    
        <!-- 切面 -->
        <!--
            proxy-target-class="true" 表示直接使用 类 实现动态代理
         -->
        <aop:config proxy-target-class="true">
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.test.service.impl.*.*(..))"/>
        </aop:config>
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>
    
    8. web.xml配置文件
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">
      <display-name>ssm-login</display-name>
      
      <!-- spring容器 start -->
    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    	</listener>
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>classpath:spring-*.xml</param-value>
    	</context-param>
      <!-- spring容器 end -->
      
      <!-- 解决Post请求乱码 start -->
    	<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>
    		<init-param>
    			<param-name>forceEncoding</param-name>
    			<param-value>true</param-value>
    		</init-param>
    	</filter>
    	<filter-mapping>
    		<filter-name>characterEncodingFilter</filter-name>
    		<url-pattern>/*</url-pattern>
    	</filter-mapping>
    	<!-- 解决Post请求乱码 end -->
      
      <!-- springmvc容器 start -->
    	<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:springmvc.xml</param-value>
    		</init-param>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>springmvc</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping>
    	<!-- springmvc容器 end -->
      
      
      <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    至此框架环境就搭好了,在写个简单的实体类,写个页面,就可以进行测试了。
    也可以 点此下载项目demo,默认运行环境eclipse+jdk1.7+mysql


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值