SSSP框架整合之路

来源:互联网 发布:json null 编辑:程序博客网 时间:2024/06/03 20:59

SSSP是:SpringMvc,Spring,SpringData,JPA(hibernate)的整合

第一步:创建web项目工程文件,导入所需jar包

第二步:配置web.xml的文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <!--配置SpringMVC-->    <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-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!--配置Spring-->    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener></web-app>
第二步:配置数据库db.peoperties

db.driver=com.mysql.jdbc.Driverdb.url=jdbc:mysql://localhost:3306/japdb.username=rootdb.password=root

第三步:配置SpringMvc的配置文件spring-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:mvc="http://www.springframework.org/schema/mvc"       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">    <mvc:annotation-driven/>    <!--配置注解-->    <context:component-scan base-package="org.peter" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    </context:component-scan>    <!--配置视图解析器-->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"/>        <property name="suffix" value=".jsp"/>    </bean></beans>

第四步:配置SpringMvc的配置文件applicationContext.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:jpa="http://www.springframework.org/schema/data/jpa"       xmlns:tx="http://www.springframework.org/schema/tx" 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">        <!--配置扫描的service-->    <context:component-scan base-package="org.peter" use-default-filters="false">        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>    </context:component-scan>    <!--配置数据源-->    <context:property-placeholder location="classpath:db.properties"/>    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">        <property name="driverClassName" value="${db.driver}"/>        <property name="url" value="${db.url}"/>        <property name="username" value="${db.username}"/>        <property name="password" value="${db.password}"/>    </bean>    <!--配置JPA,,替代之前JPA中的persistence.xml文件-->    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">        <property name="dataSource" ref="dataSource"/>        <!--配置JPA的实现类的适配器-->        <property name="jpaVendorAdapter">            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>        </property>        <!--配置所有Model的位置-->        <property name="packagesToScan" value="org.peter.model"></property>        <property name="jpaProperties">            <props>                <prop key="hibernate.show_sql">true</prop>                <prop key="hibernate.format_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>    </bean>    <!--配置Spring Data,base-package表示要扫描的包-->    <jpa:repositories base-package="org.peter.dao" entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">        <property name="entityManagerFactory" ref="entityManagerFactory"/>    </bean>    <!--配置事物-->    <tx:annotation-driven transaction-manager="transactionManager"/></beans>


第五步:测试配置的环是否可用

只需通过这五步就可以完成SSSP的整合

空白的SSSP框架的代码:http://download.csdn.net/detail/strive_peter/9916238

原创粉丝点击