Maven + Spring + SpringMVC + Spring JPA <1>

原创 2013年12月03日 15:25:35

岁数大了,搭个框架用了一天半,跌跌撞撞的,算基本走通,记录下流程和期间用到的问题。

一直懒得用Maven,怕麻烦,这次用是因为Spring官网改版了,必须要用Maven的方式下载,初步用了一下,挺好用,可以把依赖的包都下载下来,但麻烦的是,在eclipse建项目的时候,不是那么流畅,比如web.xml还需要手动创建。配置项目需要用到的包也是,你得找maven是否支持这种包,而且还得写清版本等,查包的网址是:

http://central.maven.org/maven2/oracle/toplink/essentials/
我用的是myeclipse10,没有遇到网上说的还得配置文件夹映射关系之类的问题,直接创建Maven Project项目:

以上两步即可,然后在src/main/webapp/web-inf下创建一个web.xml,然后随便整个jsp,将项目发布到tomcat下,直接就能用了。

-----------------------------------------------------------------------------------------------------------------------------------------

接下来就是整jar包的过程了,用了不少时间,找到项目根目录下的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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.mice</groupId>
	<artifactId>SayGoodbye001</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<build/>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<!-- 与spring-data-jpa冲突
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>
		 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.2.5.RELEASE</version>
		</dependency>
		<dependency>  
        		<groupId>org.springframework</groupId>  
        		<artifactId>spring-webmvc</artifactId>  
        		<version>3.2.5.RELEASE</version>  
    		</dependency>
		<dependency>
        		<groupId>org.springframework</groupId>
        		<artifactId>spring-context</artifactId>
        		<version>3.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.hateoas</groupId>
			<artifactId>spring-hateoas</artifactId>
			<version>0.7.0.RELEASE</version>
		</dependency>
		<dependency>
	       		<groupId>org.springframework.data</groupId>
	        	<artifactId>spring-data-jpa</artifactId>
	        	<version>1.4.2.RELEASE</version>
	    	</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>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>4.1.6.Final</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-entitymanager</artifactId>
			<version>4.1.6.Final</version>
		</dependency>
	</dependencies>
</project>
保存后,Maven便会去网站上去下载相关jar包以及依赖,这点是非常爽的,但是找版本号,颇费尽,熟悉了之后会好很多吧。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------

配置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">
	<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/SpringMVC-servlet.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>
    
    <listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:config/applicationContext.xml</param-value>
	</context-param>
    
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
在src/main/java/config文件下创建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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    
    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/aop   
          http://www.springframework.org/schema/aop/spring-aop-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/cache 
          http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
          http://www.springframework.org/schema/data/jpa
          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">  

       
	<!-- 定义实体管理器工厂 -->
	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
		<property name="persistenceUnitName" value="myJPA"/>
	</bean>
         
	<!-- 配置事务管理器 -->  
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
		<property name="entityManagerFactory" ref="entityManagerFactory" />  
	</bean> 
       
	<!-- 启用 annotation事务--> 
	<tx:annotation-driven transaction-manager="transactionManager"/> 
           
	<!-- 配置Spring Data JPA扫描目录--> 
	<jpa:repositories base-package="org.mice.test"/>
</beans>

再创建一个SpringMVC-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:aop="http://www.springframework.org/schema/aop"
    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-3.2.xsd  
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
   
    <!-- 启用spring mvc 注解 -->    
	<context:annotation-config />  
    <!-- 设置使用注解的类所在的jar包 -->    
	<context:component-scan base-package="org.mice"/>
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><!-- 指定策略解析 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><!-- 指定视图处理 --><property name="prefix" value="/jsp/" /><!--前缀--><property name="suffix" value=".jsp" /><!--后缀--> </bean> </beans>


最后,在src/main/java/META-INF下创建persistence.xml文件,采用的hibernate的jpa实现,学习嘛,能通就行,重在整体,数据库直接用的oracle,由于maven没有oracle的驱动,我也没采用网上的手动添加到本地maven库的方式,直接通过eclipse配置到项目中。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
            http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
            
    <persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>  
        <properties>
            <!--配置Hibernate方言 -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" />
            <!--配置数据库驱动 -->
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
            <!--配置数据库用户名 -->
            <property name="hibernate.connection.username" value="chenph" />
            <!--配置数据库密码 -->
            <property name="hibernate.connection.password" value="chenph" />
            <!--配置数据库url -->
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.101.179:1521:orcl" />
            <!--设置外连接抓取树的最大深度 -->
            <property name="hibernate.max_fetch_depth" value="3" />
            <!--自动输出schema创建DDL语句 -->
            <property name="hibernate.hbm2ddl.auto" value="update" />    
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="javax.persistence.validation.mode" value="none"/>
        </properties>
    </persistence-unit>
            
</persistence>


相关文章推荐

spring data jpa+ spring +maven

用maven创建一个普通maven project. 1. 目录结构如下
  • gaodml
  • gaodml
  • 2017年04月03日 16:02
  • 616

Delphi7高级应用开发随书源码

  • 2003年04月30日 00:00
  • 676KB
  • 下载

Springmvc+Hibernate+JPA Maven环境搭

近期,公司一直在用Springmvc+Hibernate+Jpa开发项目,这里总结一下,近两个月的研究成果。一来,做个记录,方便自己后期查阅。二来,希望可以帮到,正在用同样框架开发,却还在挠头抓耳疯狂...

maven搭建SpringMVC+Spring+hibernate+JPA

maven搭建SpringMVC+Spring+hibernate+JPA问题汇总parent里是dependencies还是dependencyManagement之前看到很多项目把dependen...

基于maven的多框架和多视图融合技术(Struts1、Struts2、Spring、SpringMVC、Hibernate、Ibatis、MyBatis、Spring Data JPA、DWR)

该项目基于maven3.0构建的,项目中融合了Struts1、Struts2、Spring、SpringMVC、Hibernate、Ibatis、MyBatis、Spring Data JPA、Spr...

SpringMVC+JPA+Spring+maven框架搭建

  • 2017年08月24日 11:26
  • 58.53MB
  • 下载

maven+springmvc+spring data jpa

1、 maven创建web项目

SpringMVC+Spring+JPA+Maven框架搭建

  • 2014年09月03日 11:05
  • 405KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Maven + Spring + SpringMVC + Spring JPA <1>
举报原因:
原因补充:

(最多只允许输入30个字)