SSM组件

作者: 康明 | 来源:发表于2017-02-11 14:04 被阅读0次

    1.Spring Framework特性 IOC
    依赖注入。
    面向切面编程,包括spring的声明性事务管理。
    Spring mvc web应用程序。
    RESTful web service框架
    JDBC, JPA, JMS的基础支持
    1.1
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.3.RELEASE</version>
    </dependency>
    1.2 注解所需要 在 applicationContext.xml 中配置如下(Spring-IOC有关-->注解)

    <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自动扫描的包,包含所有子包 -->
    <context:component-scan base-package="com.captain.demo.springdemo"/>
    … …
    

    2.Spring AOP
    --2.1 .
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>4.3.3.RELEASE</version>
    </dependency>
    2.2
    添加spring扫描
    xmlns:aop="http://www.springframework.org/schema/aop"
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd

    <context:component-scan base-package="com.captain.demo.springdemo"/>


    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <bean id="logAspect" class="com.captain.demo.aopdemo.LogAspect"></bean>
    
    <!-- aop配置 -->
    <aop:config>
        <!-- 切点 -->
        <aop:pointcut id="aop" expression=
            "execution(public void com.captain.demo.aopdemo.AopService.*(..))"/>
        <!-- 切面 : ref 的值是 切面类的id-->
        <aop:aspect id="aspect" ref="logAspect">
            <!-- 前置方法 : pointcut-ref 的值是 切点的id -->
            <aop:before method="afterMethod" pointcut-ref="aop"/>
        </aop:aspect>
    </aop:config>
    

    二、Spring-MVC
    1.1 maven中引用Spring-MVC

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.3.RELEASE</version>
    </dependency>

    1.2 创建springmvc配置文件
    ------创建:applicationServlet.xml
    ------默认配置文件:根目录dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <context:component-scan base-package="com.captain.demo.web" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
    </bean>

    1.3 web.xml添加支持 (application.xml和selvelt.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"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    classpath:spring/applicationContext.xml
    </param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <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:spring/applicationServlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>
    ... ...
    </web-app>

    1.4在maven中引用SpringTest

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.3.RELEASE</version>
    </dependency>

    1.41 .测试用例添加注解 1
    @RunWith(SpringJUnit4ClassRunner.class)
    使用junit4进行测试
    @ContextConfiguration
    ({"/spring/applicationContext.xml",
    "/spring/applicationServlet.xml"})
    加载配置文件,多个文件逗号分隔
    @TransactionConfiguration(defaultRollback = false)
    配置默认回滚

    Spring-Web测试 1

    模拟request
    MockHttpServletRequest request
    模拟response
    MockHttpServletResponse response
    执行前初始化request和response
    request = new MockHttpServletRequest();
    request.setCharacterEncoding("UTF-8");
    response = new MockHttpServletResponse();
    设置传入值
    request.setParameter("username", "张三");
    request.setParameter("password", "123456");

    三.Mybatis

    3.1
    <dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.2.5</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.40</version>
    </dependency>

    3.2 mybatis的配置文件(mybatis-config.xml随便命名),包含了对MyBatis系统的核心设置。

    <?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>
    <environments default="development">
    <environment id="development">
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/book?characterEncoding=UTF-8"/>
    <property name="username" value="root"/>
    <property name="password" value="root"/>
    </dataSource>
    </environment>
    </environments>

    <!– 引入sql文件,引入employee.xml 有sql的配置文件-->
    <mappers>
        <mapper resource=“employeeMapper.xml"/>
    </mappers>
    

    </configuration>
    3.3
    通过mybatis配置文件(mybatis-config.xml),构建SqlSessionFactory对象
    String resource = "mybatis-config.xml";
    InputStream inputStream =
    Resources.getResourceAsStream(resource);
    sessionFactory =
    new SqlSessionFactoryBuilder().build(inputStream);

    3.4 employeeMapper.xml 中 sql Mapper

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
    "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
    <mapper namespace="com.captain.demo.mybatis.dao.UserDao">
    <insert id="insertUser" parameterType="map">
    insert into user(id,name,nname,password,createtime,updatetime,
    cost,money,usertypeid,phone)
    values(#{id},#{name},#{nname},#{password},#{createtime},
    #{updatetime},#{cost},#{money},#{usertypeid},#{phone})
    </insert>
    ... ...
    </mapper>

    相关文章

      网友评论

          本文标题:SSM组件

          本文链接:https://www.haomeiwen.com/subject/ewtoittx.html