java WEB 框架开发之SSM(springmvc、spring、mybatis)

时间:2021-10-26 21:56:45

  在使用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>