web中spring与jpa整合

1:新建WEB工程,加入Maven的支持。

 

2: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.0http://maven.apache.org/maven-v4_0_0.xsd">

   <modelVersion>4.0.0modelVersion>

 

   <groupId>com.dfrzgroupId>

   <artifactId>Jpa_03artifactId>

   <packaging>warpackaging>

   <name />

   <version>0.0.1version>

   <description />

   <build>

      <sourceDirectory>${basedir}/srcsourceDirectory>

      <outputDirectory>

         ${basedir}/WebRoot/WEB-INF/classes

      outputDirectory>

      <resources>

         <resource>

            <directory>${basedir}/srcdirectory>

            <excludes>

                <exclude>**/*.javaexclude>

            excludes>

         resource>

      resources>

      <plugins>

         <plugin>

            <artifactId>maven-war-pluginartifactId>

            <configuration>

                <webappDirectory>

                   ${basedir}/WebRoot

                webappDirectory>

                <warSourceDirectory>

                   ${basedir}/WebRoot

                warSourceDirectory>

            configuration>

         plugin>

         <plugin>

            <artifactId>maven-compiler-pluginartifactId>

            <configuration>

                <source>1.5source>

                <target>1.5target>

            configuration>

         plugin>

 

        

         <plugin>

            <groupId>org.mortbay.jettygroupId>

            <artifactId>maven-jetty-pluginartifactId>

            <version>6.1.5version>

            <configuration>

                <webAppSourceDirectory>

                   WebRoot

                webAppSourceDirectory>

                <scanIntervalSeconds>5scanIntervalSeconds>

                <connectors>

                   <connector

                      implementation="org.mortbay.jetty.nio.SelectChannelConnector">

                      <port>8081port>

                   connector>

                connectors>

            configuration>

         plugin>

      plugins>

   build>

 

   <properties>

      <project.build.sourceEncoding>GBKproject.build.sourceEncoding>

      <spring.version>4.2.6.RELEASEspring.version>

   properties>

 

   <dependencies>

      <dependency>

         <groupId>javax.servletgroupId>

         <artifactId>servlet-apiartifactId>

         <version>2.5version>

         <scope>providedscope>

      dependency>

 

      <dependency>

         <groupId>javax.servlet.jspgroupId>

         <artifactId>jsp-apiartifactId>

         <version>2.1version>

         <scope>providedscope>

      dependency>

 

      <dependency>

         <groupId>javax.servletgroupId>

         <artifactId>jstlartifactId>

         <version>1.2version>

      dependency>

 

      <dependency>

         <groupId>com.drivergroupId>

         <artifactId>sqlserverartifactId>

         <version>2005version>

      dependency>

      <dependency>

         <groupId>org.apache.openjpagroupId>

         <artifactId>openjpa-allartifactId>

         <version>2.2.0version>

      dependency>

     

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-contextartifactId>

         <version>${spring.version}version>

      dependency>

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-context-supportartifactId>

         <version>${spring.version}version>

      dependency>

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-txartifactId>

         <version>${spring.version}version>

      dependency>

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-ormartifactId>

         <version>${spring.version}version>

      dependency>

 

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-aspectsartifactId>

         <version>${spring.version}version>

         <exclusions>

            <exclusion>

                <artifactId>aspectjweaverartifactId>

                <groupId>org.aspectjgroupId>

            exclusion>

         exclusions>

      dependency>

 

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-instrumentartifactId>

         <version>${spring.version}version>

      dependency>

 

      <dependency>

         <groupId>org.springframeworkgroupId>

         <artifactId>spring-webartifactId>

         <version>${spring.version}version>

      dependency>

   dependencies>

project>

 

3: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:context="http://www.springframework.org/schema/context"

   xmlns:tx="http://www.springframework.org/schema/tx"

   xmlns:jdbc="http://www.springframework.org/schema/jdbc"

   xmlns:p="http://www.springframework.org/schema/p"

   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/tx

    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

    http://www.springframework.org/schema/jdbc

   http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd

   http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

 

   <context:annotation-config>context:annotation-config>

   <context:component-scan base-package="com.test">context:component-scan>

 

 

  

   <bean name="dataSource"

      class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName"

         value="com.microsoft.sqlserver.jdbc.SQLServerDriver">

      property>

      <property name="url"

         value="jdbc:sqlserver://127.0.0.1:1433;databaseName=Java1503">

      property>

      <property name="username" value="sa">property>

      <property name="password" value="">property>

   bean>

 

 

   <bean name="entityManagerFactory"

      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

      <property name="persistenceProviderClass"

         value="org.apache.openjpa.persistence.PersistenceProviderImpl">

      property>

      <property name="packagesToScan" value="com.jpa.entity">

      property>

      <property name="jpaProperties">

         <props>

            <prop key="javax.persistence.jdbc.driver">

                com.microsoft.sqlserver.jdbc.SQLServerDriver

            prop>

            <prop key="javax.persistence.jdbc.url">

               jdbc:sqlserver://localhost:1433;DatabaseName=java1411;SelectMethod=cursor;

            prop>

            <prop key="javax.persistence.jdbc.user">saprop>

            <prop key="javax.persistence.jdbc.password">prop>

            <prop key="openjpa.jdbc.SynchronizeMappings">

                buildSchema(ForeignKeys=true)

            prop>

            <prop key="openjpa.Log">SQL=TRACEprop>

            <prop key="openjpa.ConnectionFactoryProperties">

                PrettyPrint=true,PrettyPrintLineLength=72

            prop>

           

            <prop key="openjpa.ClassLoadEnhancement">falseprop>

            <prop key="openjpa.DynamicEnhancementAgent">falseprop>

            <prop key="openjpa.RuntimeUnenhancedClasses">

                supported

            prop>

 

         props>

      property>

   bean>

 

   <bean name="transactionManager"

      class="org.springframework.orm.jpa.JpaTransactionManager">

      <property name="entityManagerFactory"

         ref="entityManagerFactory">

      property>

   bean>

   <tx:annotation-driven transaction-manager="transactionManager" />

 

beans>

4:web.xml配置Spring的监听器

 

   <context-param>

      <param-name>contextConfigLocationparam-name>

      <param-value>classpath:applicationContext.xmlparam-value>

   context-param>

 

   <listener>

      <listener-class>

         org.springframework.web.context.ContextLoaderListener

      listener-class>

   listener>

5:在jetty中开发。

 

   jetty开发过程中:内存溢出的解决

  

   在jetty:run的运行对话框的JRE的选项中设置:

  

      -server-XX:PermSize=64M -XX:MaxPermSize=128m

     

     

   jetty中打包

      package

     

   Tomcat中运行测试

     

 

你可能感兴趣的:(javaweb)