SSM练习--CURD之jar包导入及配置文件

 

1.新建maven项目,在pom.xml下添加需要导入的包:来源:http://mvnrepository.com/

   1.1 spring和springmvc:

    spring-webmvc,spring-jdbc,spring-aspects,aspectjweaver,

    2.1 mybatis:

       mybatis,mybatis-generator(逆向工程)

 3.1 mybatis整合spring

       myybatis-spring

 4.1 数据库连接池和驱动

     c3p0,mchange-commons-java,mysql 

 5.1 其他包

    jstl,servlet-api,junit

 6.1 分页插件

   pagehelper

 7.1 jackson

    jackson-databind

 8.1 JSR303校验

          hibernate-validator

2.src/main/webapp/WEB-INF/web.xml的配置

  2.1 启动Spring容器:ContextLoaderListener

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<context-param> <param-name>contextConfigLocation</param-name>
      <!--spring文件位置--> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--负责启动spring容器的监听器,获得spring配置文件地址--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

  2.2 SpringMVC前端控制器:DispatcherServlet

<servlet>  
        <servlet-name>dispatch</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <!-- 未指定SpringMVC的配置文件,所以在与web同级文件下建立一个与servlet-name同名+servlet的xml文件 -->
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:dispatch-servlet.xml</param-value>  
        </init-param> 
        <load-on-startup>1</load-on-startup>
    </servlet>  
    <servlet-mapping>  
            <servlet-name>dispatch</servlet-name>  
            <url-pattern>/</url-pattern>  
    </servlet-mapping>  

  2.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>
        
        <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>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

  2.4使用REST风格的URI,请求转换:HiddenHttpMethodFilter

<filter>
             <filter-name>HiddenHttpMethodFilter</filter-name>
              <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
   
</filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <!-- 过滤所有请求 --> <url-pattern>/*</url-pattern> </filter-mapping>

  2.5 使页面ajax成功发put请求
               在Spring MVC过滤器-HiddenHttpMethodFilter中我们提到,
               jsp或者说html中的form的method值只能为post或get,我们可以通过HiddenHttpMethodFilter获取put表单中的参数-值,
               而在Spring3.0中获取put表单的参数-值还有另一种方法,即使用HttpPutFormContentFilter过滤器。
               HttpPutFormContentFilter过滤器的作为就是获取put表单的值,并将之传递到Controller中标注了method为RequestMethod.put的方法中。

<filter>
           <filter-name>HttpPutFormContentFilter</filter-name>
           <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
   </filter>
   
   <filter-mapping>
           <filter-name>HttpPutFormContentFilter</filter-name>
           <url-pattern>/*</url-pattern>
   </filter-mapping>
   
   
</web-app>

3.Spring的配置文件applicationContext.xml

  3.1 配置文件添加的约束

    

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:tx="http://www.springframework.org/schema/tx"
 
       xmlns:aop="http://www.springframework.org/schema/aop"
 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
       xmlns:context="http://www.springframework.org/schema/context" 
 
       xmlns:p="http://www.springframework.org/schema/p"
       
  xsi:schemaLocation="
 
              http://www.springframework.org/schema/beans    
 
              http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
     
              http://www.springframework.org/schema/tx      
 
              http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
 
              http://www.springframework.org/schema/context
 
              http://www.springframework.org/schema/context/spring-context-4.3.xsd
               
              http://www.springframework.org/schema/mvc
               
              http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
 
              http://www.springframework.org/schema/aop
 
              http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

  3.2 外部配置的数据库文件 dbconfig.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm
jdbc.jdbcdriverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123

  3.3 扫描包,设置不扫描控制器,因为在SpringMVC中会配置扫描

   

<!-- 扫描包 -->
            <context:component-scan base-package="com.ssm">
                        <!-- 不扫描@Controller注解  -->
                    <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
            
            </context:component-scan>
            
                        <!-- 引入外部配置文件 -->
            <context:property-placeholder  location="classpath:dbconfig.properties"/>

  3.4 数据源

<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
                    <property name="driverClass" value="${jdbc.jdbcdriverClass}"></property>
                    <property name="user" value="${jdbc.user}"></property>
                    <property name="password" value="${jdbc.password}"></property>
 </bean>

  3.5 与mybatis的整合

  

<bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">
            
                                <!-- 指定Mybatis全局配置文件的位置 -->
            
            <!-- 加载mybatis的配置文件 -->
            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
            <!-- 配置数据源 -->
            <property name="dataSource" ref="pooledDataSource"></property>
            
            <!-- 指定mybatis.mapper文件的位置 -->
            <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
            
</bean>
            
            <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                       
               <!-- 扫描所有dao接口的实现,加入到ioc容器 -->
              <property name="basePackage" value="com.ssm.dao"></property>
       
</bean>

  3.5 事务控制

  

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                  <!-- 控制数据源 -->
             <property name="dataSource" ref="pooledDataSource"></property>
                
</bean> <!-- 开启使用xml配置形式的事务 --> <aop:config> <!-- 切入点表达式 --> <aop:pointcut expression="execution(* com.ssm.service.*.*(..))" id="txPoint"/> <!-- 配置事务增强 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> </aop:config>

  3.6配置事务增强,事务如何切入

  

<tx:advice id="txAdvice"  transaction-manager="transactionManager">
           <tx:attributes>
                    <!-- 所有方法都是事务方法 -->
                 <tx:method name="*"></tx:method>
                    
                        <!--以get开头的所有方法,设置只读-->
                  <tx:method name="get*" read-only="true"></tx:method>
            
           </tx:attributes>
</tx:advice>

 

4. SpringMVC的配置dispath-servlet.xml 

  4.1 扫描包

<context:component-scan base-package="com.ssm" use-default-filters="false">
        
        <!-- 扫描@Controller注解  不扫描用exclude -->
    <context:include-filter type="annotation"  expression="org.springframework.stereotype.Controller" />
        
</context:component-scan>

  4.2 配置视图解析器

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <!-- spring中加入jstl标签库 -->
         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
                  <!-- 前缀 -->
          <property name="prefix" value="/WEB-INF/views/"></property>
                  <!-- 后缀 -->
          <property name="suffix" value=".jsp"></property>
</bean>

  4.3 SpringMVC两个标准配置

 <mvc:default-servlet-handler></mvc:default-servlet-handler>
          
 <mvc:annotation-driven> </mvc:annotation-driven>

5.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>
                  <!-- 开启自动驼峰命名规则(camel case)映射,
                  即从经典数据库列名 A_COLUMN 到经典 Java 属性名 aColumn 的类似映射。 -->
          <settings>
              <setting name="mapUnderscoreToCamelCase" value="true" />
          </settings>
          
          <!-- 类型别名  自定义别名name为类所在的包路径-->
          <typeAliases>
              <package name="com.ssm.bean"></package>
          </typeAliases>
          
          
          <!-- 分页的配置 -->
          <plugins>
      <!-- com.github.pagehelper为PageHelper类所在包名 -->
                    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 调整分页合理化  -->
                      <property name="reasonable" value="true"/>
                </plugin>
    </plugins>
  
  </configuration>

 

posted @ 2018-01-23 13:49  蓉啊  阅读(309)  评论(0编辑  收藏  举报