ssm框架整合 理解及搭建

如何开发一个java-web的开发模式 。三大块 前端 后端 存储。分层 ,首先用户的请求 到 view ,view 调后端controller,controller业务逻辑处理存储,数据模型层 model。按照这种模式开发。用框架实现mvc 。目前用springmvc,最早期的 controller层用的是struts1,servlet,再往后是struts+hibernate,现在开发用的是spring+ibatis (是m层)。

整个工程的目录结构   。mvc是设计模式,开发中也有三层结构 controller service dao,controller是一个控制转发的,service负责业务逻辑实现,dao与数据交互。中间传输对象一般是bean。两个方向。一个是设计思想,一个是开发的规范。controller 是由两个 springmvc+前端 组成。持久层主要内容是dao,model作为bean存在于整个环节,只要需要。现在controller层主要是框架,strut2,springmvc。但他们的组成部分中都在servlet。不过也还会有一些其它组件。

 

 

在web.xml 文件中配置两个大项,一个是ContextLoaderlListener (内容加载监听),contextConfigLocation,。DispatcherServlet(调度程序容器),主要配置名称、路径<servlet>  </servlet>, <servlet-mapping>  </servlet-mapping>。   在<servlet> 中 配置出spring-mvc 路径,进而配置spring-mvc.xml, 在spring-mvc中配置一个是<context:component-scan,代表自动扫描 controller, 另一个配置 视图解析工具

两个坑 一个是 在配置web.xml时候 <servlet-mapping> 配置成*.do。  一个是在spring-mvc.xml中,视图解析工具名称是VelocityViewResolver,而不是VelocityLayoutViewResolver。

 

首先配置web.xml ,对servlet做映射,映射到spring上去,

 

 

 

在配置web.xml的时候加入依赖,导入spring-mvc 、spring-web的jar包,配置完web.xml后,配置applicationContext.xml、datasource.xml、spring-mvc.xml。

web.xml内容

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

    <!-- 配置web.xml,使其具有springmvc特性,主要配置两处,一个是ContextLoaderListener,一个是DispatcherServlet -->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext.xml</param-value>
    </context-param>
    <!-- 配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml
  作为spring容器的配置文件,该文件里可以初始化一些bean -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 字符集过滤器 -->
    <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>/*</url-pattern>
    </filter-mapping>

    <!-- spring mvc 配置 -->
    <!-- 配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件,
        XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;
        ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。
        此处使用指定的配置文件spring-mvc.xml -->
    <servlet>
        <servlet-name>contacts</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value>-->
            <param-value>classpath*:/spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>contacts</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

 

 

applicationContext.xml,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" 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/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <import resource="datasource.xml"></import>
</beans>

 

 

 

 

 

 

 

datasource.xml,配置数据源,数据源都有哪些,为什么要用数据源,本身是一个连接池,不同类型的连接池,德鲁伊druid, c3p0,自己写的jdbc 不用数据源,没有抽象出一个池子,没有池,即用即销毁,如果对db连接频繁,会创建很多对象,创建很多链接对象,在old区,会有很多对象,会导致频繁full gc,这样tps上不去,这是用数据源的其中一个原因,用一个池子管理,初始化那么多对象,用过后,扔到池子里,不用再创建新的连接对象,避免jvm产生大量fullgc。bean 依赖注入概念,spring特性注入叫ioc,自动往代码里new 对象,new的对象是单例,对象不可能是一个,ref 指引用的是哪个bean。

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" 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/aop     http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置 C3P0 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="empty"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目 -->
        <property name="acquireIncrement" value="5"></property>
        <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
        <property name="initialPoolSize" value="10"></property>
        <property name="maxPoolSize" value="20"></property>
        <property name="minPoolSize" value="5"></property>
        <!-- jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属于单个Connection而不是整个连接 -->
        <property name="maxStatements" value="20"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--<bean id="serverDao" class="com.testing91.dao.OrderDao">-->
    <!--<property name="jdbcT">-->
    <!--<ref bean="jdbcTemplate"/>-->
    <!--</property>-->
    <!--</bean>-->
</beans>

 

 

 

 

spring-mvc.xml,前端框架用的velocity,是mvc显示层的一个框架,最早期的是servlet ,后来用jsp映射,现在用velocity,需要配置一个引擎、一个解析工具,用pom.xml文件引入velocity的依赖,velocity 在这个引擎下面,解析velocity的文件,前端文件,前端文件就是加载前端的资源,会配置一个路径,加载哪个路径下的资源,就是在配置的路径,加载完了后,解析加载的文件。组建扫描,前端发请求找到后端的controller,自动扫描controller,工程机起动会加载出所有的controller文件,包名必须一致。

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <mvc:default-servlet-handler/>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="features">
                    <array>
                        <!-- List字段如果为null,输出为[],而非nul -->
                        <value>WriteNullListAsEmpty</value>
                        <!-- 字符类型字段如果为null,输出为"",而非null -->
                        <value>WriteNullStringAsEmpty</value>
                        <!-- 进制循环引用 -->
                        <value>DisableCircularReferenceDetect</value>
                        <!--Boolean字段如果为null,输出为false,而非null -->
                        <value>WriteNullBooleanAsFalse</value>
                        <!--时间 yyyy.MM.dd hh:mm:ss -->
                        <value type="com.alibaba.fastjson.serializer.SerializerFeature">WriteDateUseDateFormat</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--<mvc:resources mapping="/assets/**" location="/assets/"/>-->
    <!--velocity引擎-->
    <bean id="velocityConfigurer"
          class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/WEB-INF/view"/>
        <!-- 設置模板防止位置-->
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>

    <!-- 设置视图解析工具 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="suffix" value=".vm"/>
        <property name="layoutUrl" value="/layout/layout.vm"/>

        <!-- 避免乱码 -->
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="dateToolAttribute" value="dateTool"/>
        <property name="numberToolAttribute" value="numberTool"/>
        <property name="exposeRequestAttributes" value="false"/>
        <property name="exposeSessionAttributes" value="true"/>
    </bean>
  <!-- 组建扫描,自动扫描controller,包名必须一致 -->
    <context:component-scan base-package="com.testing91.model"/>

</beans>

 

 

 

 

 

db.properties 配置

 

dbuser=root
dbpassword=
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test

 

 

 

 

pom.xml 配置内容

 

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <groupId>com.91testing</groupId>
    <artifactId>Demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <spring.version>3.2.13.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-tools</artifactId>
            <version>2.0</version>
        </dependency>
        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!-- junit测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.0</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>Demo</finalName>
    </build>
</project>

 

 

 

 

 

 

 

 
 

 
 
 
 
 
 
 
 
 
 
 
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeilNiu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值