SSM練習--CURD之jar包導入及配置文件

 

1.新建maven項目,在pom.xml下添加須要導入的包:來源:http://mvnrepository.com/html

   1.1 spring和springmvc:前端

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

    2.1 mybatis:mysql

       mybatis,mybatis-generator(逆向工程)git

 3.1 mybatis整合springgithub

       myybatis-springweb

 4.1 數據庫鏈接池和驅動ajax

     c3p0,mchange-commons-java,mysql spring

 5.1 其餘包sql

    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>
相關文章
相關標籤/搜索