Maven + spring + SPRINGMVC + Spring JPA <1>

Source: Internet
Author: User
Tags aop metabase

Old, a frame with a day and a half, stumbled, calculate the basic pass, record the process and the problems used during.

have been too lazy to use maven, afraid of trouble, this time use is because Spring official website revision, must use Maven way to download, preliminary use, quite good, can rely on the package to download down, but the trouble is, in Eclipse build project time, not so smooth, For example, Web.xml also need to be created manually. Configure the project to use the package is also, you need to find Maven whether to support the package, but also have to write a version of, and so on, check the package URL is:

http://central.maven.org/maven2/oracle/toplink/essentials/
I'm using Myeclipse10, and I don't have to talk to you. Configure the folder mapping relationship and so on, directly create the MAVEN project project:

The above two steps can be, and then create a web.xml under the Src/main/webapp/web-inf, and then casually the entire JSP, the project published to Tomcat, directly can be used.

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

The next is the whole jar package process, used a lot of time to find the project root directory of the Pom.xml file, add the package, after a few articles may increase or decrease, will be explained in the post

<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> <!--and SPRING-DATA-JPA conflict <dependency> <groupid>javax.persistence</ groupid> <artifactId>persistence-api</artifactId> <version>1.0</version> </dependen
cy>--> <dependency> <groupId>org.springframework</groupId>			<artifactId>spring-web</artifactId> <version>3.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-w ebmvc</artifactid> <version>3.2.5.RELEASE</version> </dependency> <depend Ency> <groupId>org.springframework</groupId> <artifactid>spring-context</artif actid> <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&gt 
			;  
			<version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <d ependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> &L t;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>
After the save, MAVEN will go to the Web site to download the relevant jar bag and dependencies, this is very cool, but find version number, quite laborious, familiar with after a lot better.
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------------

Configure 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>classpa Th:config/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name&gt  
        ; <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-cla ss>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!--Specifies the directory where the spring bean's configuration file resides. Default configuration--> <context-param> <param-name>contextConfigLocation</param-name> <par in Web-inf directory Am-value>classpath:config/applicationcontext.xml</param-value> </context-param> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app
 >
Create a Applicationcontext.xml file under Src/main/java/config file

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x Si= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" x mlns: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:j Pa= "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.springframewor K.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.springframewo Rk.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd Http://www.sprin GFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/spring-jpa.xsd "> &L t;! --Define Entity manager factory--> <bean id= "entitymanagerfactory" class= " Org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean "> <property name=" Persistenceunitname " Value= "MYJPA"/> </bean> <!--configuration transaction manager--> <bean id= "TransactionManager" class= "ORG.SPRINGF" Ramework.orm.jpa.JpaTransactionManager "> <property name=" entitymanagerfactory "ref=" Entitymanagerfactory " > </bean> <!--enable annotation transaction--> <tx:annotation-driven transaction-manager= "transactionm Anager "/> <!--Configure the spring Data JPA Scan directory--> <jpa:repositories base-Package= "Org.mice.test"/> </beans> 

Then create a 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 ">
   
    <!--enable spring MVC annotations-->    
	<context:annotation-config/>  
    <!--setting the jar package where the annotations are used-->    
	< Context:component-scan base-package= "Org.mice"/>
<bean class= "Org.springframework.web.servlet.view.UrlBasedViewResolver" ><!--Specify policy resolution--><property Name= "Viewclass" value= "Org.springframework.web.servlet.view.JstlView"/><!--Specify view handling--><property name= "prefix" value= "/jsp/"/><!--prefix--><property name= "suffix" value= ". jsp"/><!--suffix--> </bean > </beans>

Finally, in src/main/java/ Meta-inf under the creation of persistence.xml files, using the hibernate of the JPA implementation, learning, can pass on the line, the whole, database direct use of Oracle, because MAVEN does not have the Oracle driver, I did not use the online manual to add to the local Maven library way, straight is then configured in the project through 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://ja
            
    Va.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> <!--configuration Hibernate dialect--&G
            T 
            <property name= "Hibernate.dialect" value= "Org.hibernate.dialect.Oracle9Dialect"/> <!--configuration database driver-->
            <property name= "Hibernate.connection.driver_class" value= "Oracle.jdbc.driver.OracleDriver"/> <!--metabase user name--> <property name= "Hibernate.connection.username" value= "Chenph"/> & lt;! --Configure Database password--> <property name= "Hibernate.connection.password" value= "Chenph"/> <!--metabase url--> <property "hi
            Bernate.connection.url "value=" Jdbc:oracle:thin:@192.168.101.179:1521:orcl/> <!--sets the maximum depth of the outer-join grab tree-->
            <property name= "Hibernate.max_fetch_depth" value= "3"/> <!--automatic Output schema create DDL statements--> <property name= "Hibernate.hbm2ddl.auto" value= "Update"/> <property name= "Hibernate.show_" SQL "value=" true "/> <property name=" Hibernate.format_sql "value=" true "/> <property n
            
Ame= "Javax.persistence.validation.mode" value= "None"/> </properties> </persistence-unit> </persistence>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.