java WEB 框架開發之SSM(springmvc、spring、mybatis)


  在使用SSM開發項目之前,需要單獨學習springmvc、spring、mybatis。

    springmvc:當一個請求到來,springmvc執行流程:

        1.前端控制器DispatcherServlet:攔截請求

        2.處理器映射器(HandlerMapping):找到對應的Handler

        3.處理器適配器(HandlerAdaptor):執行處理器

        4.處理器(Handler):處理請求(開發者編寫,類似於Servlet),獲取參數,查詢數據庫等操作

        5.視圖解析器(viewResolver):解析視圖並響應回瀏覽器

    常用的注解:@RequestMapping("映射路徑"):在方法中添加時表示此方法是一個處理器、@ResponseBody:返回json數據(ajax)、@RequestParam("key"):參數綁定等

    

    spring:兩個核心,IOC、AOP

        IOC:依賴注入,管理bean,工廠模式,屬性注入(set方法),構造器注入(構造函數)、靜態工廠的方法注入、實例工廠的方法注入

        AOP:面向切面編程,可以將方法插入或環繞在某個方法里,前置:在某個方法執行之前執行;后置:在執行之后執行;環繞:在執行之前和執行之后。aspect

        切面方法:必須有參數JoinPoint類型或ProceedingJoinPoint類型     

/**
*環繞
**/
public Object around(ProceedingJoinPoint  point){
      System.out.println("執行之前要執行的代碼");
      Object returnMsg = point.proceed();   //放行,執行原方法
      System.out.println("執行之后要執行的代碼");
      return returnMsg;   
}

public void beforeOrAfter(JoinPoint point){
     System.out.println("執行之前或之后要執行的代碼");
}

  

  mybatis:根據配置文件自動實現dao接口(創建實現類),半自動orm(Object Relation Mapping)框架,只需配置入參、出參、sql即可。

  常用的配置標簽:

    sql:<insert><update><select><delete><sql><include>

    映射標簽:<resultMap><parameterMap><id><result><parameter><collection>:一對多、多對多;<association>:一對一。

    動態sql:<where><set><choose><when><otherwise><if> <foreach>:批量

 

 

------------------------------------SSM整合開發----------------------------------

一、springmvc准備

  1.配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <!-- 組件掃描 ,掃描組件(注解:@Controller、@Service、@Repository、@Componment)創建bean-->
    <context:component-scan base-package="com.hgx.note.*.*"></context:component-scan>
    
    <!-- 處理器映射器、處理器適配器,注解驅動 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    
    <!-- 視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前綴 -->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!-- 后綴 -->
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- json支持,映射json為http消息轉換器 -->
    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>
    <!-- 開啟默認servlet:處理靜態資源,以便靜態資源不被dispatcherServlet當成Handler(處理器)攔截報404 ,此servlet的name為default-->
    <mvc:default-servlet-handler/>
    </beans>

2.在web.xml配置DispatcherServlet和攔截路徑

<!-- spring-mvc核心servlet -->
    <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:config/spring-mvc.xml</param-value>
        </init-param>
        <!-- 服務器啟動時創建 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- 攔截路徑需要配置為/而不是/*,/*攔截所有包括.jsp;而/攔截沒有后綴的請求如:/findAll -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

到這里springmvc准備完畢

 

二、mybatis准備:mybatis.xml配置,開發時在配置mapper.xml

mybatis.xml一般只配置typeAilases和plugins,其他數據源等都與spring整合

<?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 type="java.util.Map" alias="Map"/>
    </typeAliases>
    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="oracle"/>
        </plugin>
    </plugins>
</configuration>

 

三、spring整合springmvc和mybatis:關於spring的配置

  1.springmvc不需要我們手動整合,springmvc默認整合了spring,使用spring創建的controller-bean

  2.spring整合mybatis:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <!-- 加載jdbc.properties -->
    <context:property-placeholder location="classpath:config/jdbc.properties"/>
    
    <!-- 組件掃描,掃描注解創建bean -->
    <context:component-scan base-package="com.hgx.note.*.*"/>
    
    <!-- spring-mybatis -->
    <!-- dataSources -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName">
            <value>${driver_class}</value>
        </property>
        <property name="url">
            <value>${jdbc_url}</value>
        </property>
        <property name="username">
            <value>${jdbc_username}</value>
        </property>
        <property name="password">
            <value>${jdbc_password}</value>
        </property>
    </bean>
    
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:config/mybatis.xml"/>
        <property name="mapperLocations" value="classpath:mappers/*.xml"/>
    </bean>
    
    <!-- 事務管理transactionManager -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 注解事物 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- mapper(dao)接口掃描器,掃描接口並獲取實現類對象:mybatis的 session.getMapper();-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hgx.note.*.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    </beans>

 

四、最后也是最關鍵的一步,配置web.xml,加載spring

<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">

  <display-name>Archetype Created Web Application</display-name>
    
    <!-- 獲取spring.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring.xml</param-value>
    </context-param>
    
    <!-- 加載spring的監聽 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- spring-mvc核心servlet -->
    <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:config/spring-mvc.xml</param-value>
        </init-param>
        <!-- 服務器啟動時創建 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- 攔截路徑需要配置為/而不是/*,/*攔截所有包括.jsp;而/攔截沒有后綴的請求如:/findAll -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <servlet>
        <servlet-name>imgServlet</servlet-name>
        <servlet-class>note.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>imgServlet</servlet-name>
        <url-pattern>/imageServlet</url-pattern>
    </servlet-mapping>
    
    <!-- 編碼過濾器 -->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 加載log4j -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:config/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <!-- 靜態資源處理servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.css</url-pattern>
        <url-pattern>/assets/*"</url-pattern>
        <url-pattern>/images/*</url-pattern>
        <url-pattern>/static/*</url-pattern>
    </servlet-mapping>
    
    <!-- 歡迎頁 -->
    <welcome-file-list>
        <welcome-file>WEB-INF/index.jsp</welcome-file>
    </welcome-file-list>
    
    <!-- 錯誤頁 -->
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/errorpage/500.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/errorpage/404.jsp</location>
    </error-page>
    
    <!-- 項目路徑唯一標識,把項目路徑存至變量hgx.root中,在log4j中可以使用${hgx.root}獲取項目路徑 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>hgx.root</param-value>
    </context-param>
</web-app>

 

最后的最后,附上maven的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hgx</groupId>
    <artifactId>note</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>note Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- spring版本號 -->
        <spring.version>3.2.4.RELEASE</spring.version>
        <!-- mybatis版本號 -->
        <mybatis.version>3.2.4</mybatis.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.9</log4j.version>
        <!-- <hessian.version>4.0.38</hessian.version> -->
    </properties>
 

    <dependencies>
        <!-- spring核心包 -->
        <!-- springframe start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- springframe end -->

        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis/spring包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <!-- mybatis分頁插件 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.4</version>
        </dependency>
        <!-- oracle驅動包 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
        <!-- junit測試包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!-- 阿里巴巴數據源 包 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.2</version>
        </dependency>
        <!-- json數據 -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!-- json2java -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
        </dependency>
        <!-- <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> 
            <version>1.0.6</version> </dependency> -->

        <!-- 日志文件管理包 -->
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <!-- <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> 
            <version>${hessian.version}</version> </dependency> -->
        <!-- log end -->


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    <!-- 標簽庫 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <!-- jsp -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <!-- mysql驅動包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
    </dependencies>


    <build>
        <finalName>note</finalName>
    </build>
</project>

 

 

 

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM