MAVEN Project Environment Setup (maven + Spring + IBatis) steps

Source: Internet
Author: User

Preparation steps

? 1. Install maven and download unzip.

? 2. Modify the <localRepository>D:/MavenRepo</localRepository> in Maven_home/conf/settings.xml to specify the local warehouse location.

? 3. Modify the settings.xml ? <mirrors></mirrors> tags, add a common MAVEN remote warehouse address. These warehouse addresses are used when downloading jar packages. For example:

<mirror>       <id>oschina</id>      <mirrorOf>central</mirrorOf>       <url>http://maven.oschina.net/content/groups/public/</url>   </mirror> <mirror>      <id>repo2</id>       <mirrorOf>central</mirrorOf>      < url>http://repo2.maven.org/maven2/</url>  </mirror>  <mirror>       <id>net-cn</id>      <mirrorOf> central</mirrorof>      <url>http://maven.net.cn/content/groups/ Public/</url>   </mirror> 

If you use the MVN command line to create, build, and run a MAVEN project, you need to configure the environment variable, which points to maven_home/bin. Once configured, you can view the MVN command:

Because of the cumbersome use of commands, I use the Maven plugin from Eclipse to create and run MAVEN projects directly.

4. Integrate Maven in Eclipse.

    • Install Eclipse's Maven plugin first

    • In Eclipse, specify the installed Maven through the Windowàpreferencesàmaven menu:

and specify your own configuration file settings.xml:

Create a MAVEN project

5. Newàmaven Projectànext, select the project structure of the WebApp type:


6. Enter the group ID, Artifact ID, version and package, Finish.

7. Once created, junit3.8 is imported into the project by default:

8. Replace the default JRE environment with the JRE environment currently in use in Eclipse.

9. Each MAVEN project has a Pom.xml file that describes the dependencies of the project, as well as some of its own properties, including the definition of properties, the version of the MAVEN modules, the parent module, and the name of the submodule. The file also includes a definition of what the project does during the build process. Now open the Pom.xml file, and first add the attribute definition used for the item above the <dependencies> tag:

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> < Spring.version>4.0.0.release</spring.version></properties>

and add the following dependencies to the <dependencies></dependencies> tag, and the rest of the content does not need to be modified:

<dependencies>    <!-- mybatis Related  -->    < dependency>        <groupid>org.mybatis</groupid>         <artifactId>mybatis</artifactId>         <version>3.2.0</version>    </dependency>     <dependency>        <groupid>org.mybatis </groupid>        <artifactid>mybatis-spring</ artifactid>        <version>1.2.0</version>     </dependency>        <!-- mysql Related  -- >    <dependency>        <groupId> mysql</groupid>        <artifactid>mysql-connector-java</artifactid>         <version>5.1.36</version>    </ dependency>    <dependency>        < groupid>c3p0</groupid>        <artifactid>c3p0</ artifactid>        <version>0.9.1.2</version>     </dependency>     <!-- spring Related  -->     <dependency>        <groupId> Org.springframework</groupid>        <artifactid> spring-webmvc</artifactid>        <version>${ Spring.version}</version>    </depenDency>    <dependency>        <groupid >org.springframework</groupId>        <artifactId> Spring-web</artifactid>        <version>${spring.version} </version>    </dependency>    <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-test</artifactId>         <version>${spring.version}</version>    </dependency>     <dependency>        <groupId> Org.springframework</groupid>        <artifactid> Spring-ibatis</artifactid>        <version>2.0.8</version>    </ dependency>    <dependency>        < Groupid>org.springframework</groupid>        <artifactid >spring-jdbc</artifactid>        <version>${ spring.version}</version>    </dependency>     <!- -  Test related  -->    <dependency>         <groupId>junit</groupId>        <artifactId> Junit</artifactid>        <version>4.10</version>         <scope>test</scope>    </ Dependency>     <!-- servlet Related  -->    <dependency>         <groupId>tomcat</groupId>         <artifactid>servlet-api</artifactid>        <version >5.5.23</version>    </dependency>     <!--  log related  -->    <dependency>         <groupId>log4j</groupId>        <artifactId> Log4j</artifactid>        <version>1.2.17</version>     </dependency></dependencies>

10. In Maven's world, each jar package can be uniquely positioned by the group ID, Artifact ID, version three fields (typically abbreviated as GAV), so if you need to use which jar package, you only need to provide these three fields.

If you do not know the version number or GroupID, you can go to the public Maven repository to search for keywords. For example, search: log4j, can appear in the warehouse already included in the jar package about log4j:


, I searched for log4j in the MAVEN library provided by Oschina, and there are a list of available jar packages, select one, and the Gav property of the package at the bottom right. Copy this section directly into the MAVEN project Pom.xml file.

Once the jar package is ready, the project interface definition is started. Modified structure

Web. XML only defines the basic dispatchservlet, which is used to forward requests:

<servlet> <servlet-name>spring</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>con Textconfiglocation</param-name> <param-value>classpath:spring.xml</param-value> </init-para M> <load-on-startup>1</load-on-startup></servlet><servlet-mapping> <servlet-name >spring</servlet-name> <url-pattern>/m/*</url-pattern></servlet-mapping>

Spring.xml.

<context:component-scan base-package= "com.xxx"  /> <bean id= "PropertiesConfigurer"     class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >     <property name= "Locations" >          <list>              < value>classpath:log4j.properties</value>               <value>classpath:jdbc.properties</value>          </list>    </property></bean> <bean  id= "DataSource"      class= "Com.mchange.v2.c3p0.ComboPooledDataSource"   Destroy-method= "Close" >    <property name= "Driverclass"  value= "${ Jdbc.driverclassname} "&NBSP;/&GT;&NBSP;&NBsp;  <property name= "Jdbcurl"  value= "${jdbc.url}"  />     <property name= "User"  value= "${jdbc.username}"  />    <property  name= "Password"  value= "${jdbc.password}"  /></bean> <!--  Sqlsessionfactory --><bean id= "Sqlsessionfactory"  class= " Org.mybatis.spring.SqlSessionFactoryBean ">    <property name=" DataSource "  ref= "DataSource"  />    <!-- <property name= "Mapperlocations"              value= "Classpath*:com/xxx/dao/*.xml"  / > -->    <property name= "Configlocation"  value= "classpath: Mybatis-config.xml " /></bean> <!-- Mybatis sql session -->< Bean id= "sqlsession"  class= "Org.mybatis.spring.SqlSessionTemplate">    <constructor-arg index= "0"  ref= "sqlsessionfactory"  /></ bean> <!-- mybatis mapper scanner, scans for java mapper -- ><!-with this class, the defined DAO interface does not need to implement the--><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >    <property name= "Basepackage"  value= "Com.xxx.dao"  />     <property name= "Sqlsessiontemplatebeanname"  value= "SqlSession"  /></ Bean>

Log4j.properties and Jdbc.properties, slightly.

The. mybatis-config.xml file, which specifies which XML files can be used as mapping files for the DAO Interface:

<configuration> <mappers> <mapper resource= "Com/xxx/entity/usermap.xml"/> </mapper S></configuration>

The. usermap.xml file defines the SQL statement for the operation of the user object:

<mapper namespace= "Com.xxx.dao.TestDao" >    <resultmap id= " Userresultmap " type=" Com.xxx.entity.User ">        <id  column= "id"  jdbctype= "INTEGER"  property= "id"  />         <result column= "UserName"  jdbctype= "VARCHAR"  property= "name"  />         <result column= "Userage"  jdbctype= "INTEGER"  property= " Age " />        <result column=" userAddress "  Jdbctype= "VARCHAR"  property= "Address"  />    </resultMap>      <select id= "Testquery"  resultmap= "Userresultmap" >         select * from user    </select></mapper >

Statement of Controller, service and DAO: slightly.

Build and run

18. After writing, right-click on the project Àrun Asàmaven Build ... Prepare to build this MAVEN project.


19. Specify the project you want to build in BaseDirectory and specify the target of the build in the Goals box. And you can select some additional properties (in the Green box),

20. If the build succeeds, an output similar to the following will appear:

21. Once the build is successful, you can run the project just like normal Web project. This adds it to Tomcat and starts it.

22. First look at the contents of the database:


23. Access the specified interface in the browser to view the results:


MAVEN Project Environment Setup (maven + Spring + IBatis) steps

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.