IDEA builds Spring + Spring MVC + hibernate

Source: Internet
Author: User

Tools: Idea (JDK 1.8) Spring and Hibernate versions are more than 4.0
It is recommended that you build your project using MAVEN, which is much easier than importing your jar packages manually. 1. Create a new MAVEN Web project.

First build a MAVEN project, build the project here is not much to say,
The correct file structure:
-src
-main
--java (class file)
--resources (configuration file)
--webapp (web file)
-test (test file)
--java
--resources 2, write Pom.xml file, import jar package Import jar package is also a difficult point in the framework integration, often import too many unrelated packages, but because unfamiliar and dare not delete, so jar bag more guide, to the final frame is put up, But the jar bag leads more than 10 20. Note: The recommended approach here is to import the core package when you are unfamiliar with what jar packages you need to import, and then add dependencies based on the error prompts when you are prompted to notfoundclass the project, so that you do not import the extra jar packages. You can go to the website to search for the dependencies you need: https://mvnrepository.com/ 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> <groupid>com.zznode</group id> <artifactId>admin</artifactId> <version>1.0-SNAPSHOT</version> <packaging&gt ;war</packaging> <name>admin Maven webapp</name> <!--fixme change it to the project ' s WEBSI
        Te--> <url>http://www.example.com</url> <properties> <!--spring version number--> <spring.version>4.3.10.RELEASE</spring.version> </properties> <!--dependence from this start <depe Ndencies> is the parent tag of all dependent packages, add a <dependency> tag here by default add a test package, and we need to put all the dependencies we use into it (the dependency information for each package needs its own
  Look, here are the dependencies needed for this demo-->  <!--project Dependencies--> <dependencies> <!--log pack--> <dependency> <groupid& Gt;junit</groupid> <artifactId>junit</artifactId> <version>3.8.1</versi on> <scope>test</scope> </dependency> <dependency> < Groupid>org.slf4j</groupid> <artifactId>slf4j-log4j12</artifactId> <versio n>1.7.21</version> </dependency> <dependency> <groupid>org.projectl Ombok</groupid> <artifactId>lombok</artifactId> <version>1.16.18</versi
            on> <scope>provided</scope> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version >1.5.3</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <arti
        Factid>aspectjtools</artifactid> <version>1.8.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactid>jstl</ar tifactid> <version>1.2</version> <scope>runtime</scope> </de pendency> <dependency> <groupId>javax.annotation</groupId> <artifa 
        Ctid>javax.annotation-api</artifactid> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactid>servlet -api</artifactid> <version>2.5</version> </dependency> <!--mysql Driver
      Package-->  <dependency> <groupId>mysql</groupId> &LT;ARTIFACTID&GT;MYSQL-CONNECTOR-JAVA&L T;/artifactid> <version>5.1.35</version> </dependency> <!--add Hiberna Te-dependent--> <dependency> <groupId>org.hibernate</groupId> <artifactid


        >hibernate-core</artifactId> <version>4.3.8.Final</version> </dependency> <!--&lt;! &ndash;
          Spring-date-jpa&ndash;&gt; <dependency> <groupId>org.springframework.data</groupId> <artifactid>sprin
      G-data-jpa</artifactid> <version>2.0.6.RELEASE</version> </dependency>
            --> <!--https://mvnrepository.com/artifact/javassist/javassist--> <dependency> <groupId>javassist</groupId> <arTifactid>javassist</artifactid> <version>3.12.1.GA</version> </dependency>
            <!--spring--> <dependency> <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId> <version>${spring.version}</version>
            </dependency> <dependency> <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!--Https://mvnrepository.com/artifact/org.springframework/spring-beans--> &L T;dependency> <groupId>org.springframework</groupId> <artifactid>spring-beans </artifactId> <version>${spring.version}</version> </dependency> <d
          Ependency>  <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!--spring MVC + Spring we b--> <dependency> <groupId>org.springframework</groupId> &LT;ARTIFAC Tid>spring-web</artifactid> <version>${spring.version}</version> </dependency&
        Gt <dependency> <groupId>org.springframework</groupId> <artifactid>spring-web mvc</artifactid> <version>${spring.version}</version> </dependency> &L T;dependency> <groupId>org.springframework</groupId> <artifactid>spring-jdbc& lt;/artifactid> <version>${spring.version}</version> </dependency> <! --Other required packages--&GT <dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-lan g3</artifactid> <version>3.4</version> </dependency> <dependency&gt
            ;
            <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!--&lt;! &ndash;
          Https://mvnrepository.com/artifact/com.google.code.gson/gson &ndash;&gt; <dependency> <groupId>com.google.code.gson</groupId> <artifactid>gson& Lt;/artifactid> <version>2.8.0</version> </dependency>--> <!--J
            Ackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-core</artifactid> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-databind</artifacti
            d> <version>2.7.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson-annotations</artifactid
        > <version>2.7.0</version> </dependency> <!--c3p0 start--> 
            <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!--import dbcp jar packages, used in Applicatio Ncontext.xml configuration Database--> <dependency> <groupId>commons-dbcp</groupId> & Lt;artifactid>commons-dbcp</artifactid> <version>1.2.2</version> </dependency> </dependenci
 es> <build> <finalName>admin</finalName> </build> </project>
in which, the dependencies in the annotation block are imported into the framework core jar package, we can import it in advance, others can be changed according to the prompts or requirements. For example, I use MySQL and c3p0 data sources in my project, so I'm relying on MySQL and c3p0, and if you're using other databases and data sources, you need to modify them accordingly. The part of the annotation in the pom file is the dependency on the missing jar package that was prompted at the time of the project run, some of the packages that the spring framework relies on, some of which are servlet or JSP dependent, if you are unfamiliar with the first build, and then introduce dependencies when the error occurs, This also can deepen understanding. 3. configuration file This is the most important part of the framework integration. The configuration file is placed in the Main/resources folder except Web.xml, of course, you can also put it in the other new folder, but the configuration file address to make the corresponding changes, this will be detailed. Configuration file configuration, many framework integration tutorials are not the same, here are my own configuration methods and practices. configuration files are mainly divided into four: Web.xm, Beans.xml, Spring-mvc.xml, Datasource.xml. The configuration steps are divided into two steps: consolidation of SPRING-MVC and spring, integration of Hibernate and spring, and final running of the project. (This is also a higher efficiency of the practice, separate integration, so even if the operation of the project has an error is easier to find, and division integration is not easy to mess) The next step is to start a specific configuration. 4, SPRING-MVC and spring integration Spring-mvc.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
       I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" 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/ MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!--enable some annotation--> for spring <context :annotation-config/> <!--automatically scans Com.test.controller packets to make SPRINGMVC think the class with @controller annotations under the package is the controller--> &LT;CONTEXT:C Omponent-scan base-package= "Com.zznode.admin.controller" > <context:include-filter type= "Annotation" Expressi
   on= "Org.springframework.stereotype.Controller"/>     <!--class--> <context:exclude-filter to exclude @service annotations type= "annotation" expression= "Org.springframework.ster" Eotype. Service "/> </context:component-scan> <!--configuration Note Driver can--> the request parameter with the binding to the controller parameter <mvc:a nnotation-driven/> <!--resolves the model view name by adding a prefix to the Model view name (if the last one still represents a folder, the last slash will not be missed) use the jsp--> <!--default view parser on the top Use (default HTML) for parsing errors---> <bean id= "Defaultviewresolver" class= Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.s Pringframework.web.servlet.view.JstlView "/> <property name= prefix" value= "/web-inf/views/"/><!--set JS The directory location of the P file--> <property name= "suffix" value= ". html"/> </bean> <!--springmvc File upload the node that needs to be configured-- > <bean id= "multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"
      > <property name= "maxuploadsize" value= "20971500"/>  <property name= "defaultencoding" value= "UTF-8"/> <property name= "resolvelazily" value= "true"/> & lt;/bean> <!--use Jackjson to convert the returned object to JSON by default, if this configuration--> <bean id= "is not required to add the Jackson package to the Pom file earlier" Stringconver ter "class=" Org.springframework.http.converter.StringHttpMessageConverter "> <property name=" suppor Tedmediatypes "> <list> <value>text/plain;charset=UTF-8</value>

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.