The SSM integration configuration step under Maven

Source: Internet
Author: User
Tags aop xmlns ssh tomcat log4j
The SSM integration configuration step under Maven

Prior to this, most of the projects are using the SSH framework, the previous years, the existence of struts fatal bug,hibernate package depth, less than mybatis inefficiency, now SSH seems to be in the gradual exit from the historical stage, and instead of nature is now used by the most SSM framework. The next part is the Gospel of little white, let me take you together to build a project of SSM architecture.

The first step: Create a MAVEN project, this project is packaged in the form of a war, specifically to create a MAVEN project I will not write, directly on the configuration of the Pom.xml file:

<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.millery</groupId> <artifactId> Millery-ssm</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging > <dependencies> <!--unit Testing-<dependency> <groupId>junit</groupId> <arti factid>junit</artifactid> <version>4.10</version> <scope>test</scope> </depen Dency> <!--Spring Dependent--<dependency> <groupId>org.springframework</groupId> <a Rtifactid>spring-context</artifactid> <version>4.1.3.RELEASE</version> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <Artifactid>spring-beans</artifactid> <version>4.1.3.RELEASE</version> </dependency> & Lt;dependency> <groupId>org.springframework</groupId> <artifactid>spring-webmvc</ artifactid> <version>4.1.3.RELEASE</version> </dependency> <dependency> &LT;GROUPID&G T;org.springframework</groupid> <artifactId>spring-jdbc</artifactId> <version>4.1.3.
			release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.3.RELEASE</version> </dependency > <!--mybatis Dependency--<dependency> <groupId>org.mybatis</groupId> <artifactid>m
		Ybatis</artifactid> <version>3.2.8</version> </dependency> <!--mybatis Integration Spring-- <dependency> <groupid>org.mybatis</groupid> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </d ependency> <!--mysql dependency--<dependency> <groupId>mysql</groupId> <artifactId> Mysql-connector-java</artifactid> <version>5.1.32</version> </dependency> <!--slf4j Log Lai-<dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-log4j12</artifacti D> <version>1.6.4</version> </dependency> <!--connection Pool--<dependency> <grou Pid>com.jolbox</groupid> <artifactId>bonecp-spring</artifactId> <version>0.8.0. Release</version> </dependency> <!--jsp related to <dependency> <groupid>jstl</grou pid> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> &LT;DEP Endency> <groupid>javax.servlet</groupid> <artifactId>servlet-api</artifactId> <version>2.5</version> < scope>provided</scope> </dependency> <dependency> <groupid>javax.servlet</groupid&
			Gt <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope > </dependency> </dependencies> <build> <plugins> <!--configuring Tomcat plug-ins-<p Lugin> <groupId>org.apache.tomcat.maven</groupId> <artifactid>tomcat7-maven-plugin</ artifactid> <configuration> <port>8080</port> <path>/</path> </confi guration> </plugin> </plugins> </build> </project>


Step two: After the MAVEN project is established there is no Web-inf directory, There is no web. xml file, this needs to be created by ourselves, under the SRC under main under the WebApp directory to create a web-inf, and then in the Web-inf directory to create a Web. XML configuration file, it is recommended to copy from other projects, and then change it, the following is posted on the Web. XML configuration Text Contents of the piece

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" MyWebApp "version=" 2.5 "> <display-name>millery-ssm</ display-name> <!--loading spring's own configuration file--<context-param> <param-name>contextconfiglocation</ Param-name> <param-value>classpath:spring/applicationContext*.xml</param-value> </ context-param> <!--spring ApplicationContext loading: Spring's listener--<listener> <listener-class>org. Springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- Load SPRINGMVC Profiles-<servlet> <servlet-name>millery-ssm</servlet-name> <servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-namE>contextconfiglocation</param-name> <param-value>classpath:spring/millery-ssm-servlet.xml</ param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet -mapping> <servlet-name>millery-ssm</servlet-name> <url-pattern>/</url-pattern> </
 Servlet-mapping> </web-app>

The third step: depends on, the Web. xml file also has, Next is SPRINGMVC's core configuration file Millery-ssm-servlet.xml, according to the configuration in Web. XML to create the file, the contents of the file is pasted below:

<?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:p= "http://www.springframework.org/schema/p"
	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/MVC/http Www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        Http://www.springframework.org/schema/context/http Www.springframework.org/schema/context/spring-context.xsd ">
		
	<!--SPRINGMVC Driver Scan Package--
	< Mvc:annotation-driven/>

	<!--define a controller scan package, the user scans the controller and creates a proxy object--
	<context: Component-scan base-package= "Com.millery.controller"/>
	
</beans>

Fourth step: At this time to configure the spring core configuration file Applicationcontext.xml, at this time also need to set the parameters of the data connection needs to be configured in, not much nonsense, configuration has been posted:

Database data configuration file Jdbc.properties:

Jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/theuser?useunicode=true& zerodatetimebehavior=converttonull&characterencoding=utf8&autoreconnect=true&allowmultiqueries= True
jdbc.username=root
jdbc.password=123456

Spring Core configuration file 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:p= "http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--Configure the service package scan, automatically inject service--> <context:component-scan base-package=" Com.millery "/> <!--use spring's own placeholder replacement function--<bean class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <!--allow JVM parameters to be overwritten--<property Name= "Systempropertiesmodename" ValUe= "System_properties_mode_override"/> <!--Ignore resource file not found--<property name= "Ignoreresourcenotfound" Valu E= "true"/> <!--configuration resource file--<property name= "Locations" > <list> &LT;VALUE&GT;CLASSPATH:JDBC  .properties</value> </list> </property> </bean> <!--database connection pool:d atasource--> <bean Id= "DataSource" class= "Com.jolbox.bonecp.BoneCPDataSource" destroy-method= "Close" > <!--database-driven <prop Erty name= "Driverclass" value= "${jdbc.driver}"/> <!--corresponding driver Jdbcurl--<property name= "Jdbcurl" value= "${ Jdbc.url} "/> <!--database user name--<property name=" username "value=" ${jdbc.username} "/> <!--password for database- <property name= "Password" value= "${jdbc.password}"/> <!--check the interval of idle connections in the database connection pool, in units of minutes, default: 240, set to 0 if you want to cancel --<property name= "idleconnectiontestperiod" value= "/> <!--the maximum surviving time of unused links in the connection pool, in units of minutes, default: 60, If you want to live forever set to 0--<property name= "Idlemaxage" value= "/>" <!--maximum connections per partition-<property name= "maxconnectionsperpartition" value= "150"/&
		Gt <!--the minimum number of connections per partition--<property name= "Minconnectionsperpartition" value= "5"/> </bean> </beans>

The fifth step: the spring and SPRINGMVC in the SSM are already configured, and it's time to start configuring the MyBatis configuration file. MyBatis configuration file official recommended naming rules Mybatis-config.xml, we'll press the official, see below, for this is the contents of the MyBatis core configuration file:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE configuration Public
  "-//mybatis.org//dtd Config 3.0//en"
  "http://mybatis.org/dtd/ Mybatis-3-config.dtd ">
<configuration>

	<settings>
		<!--open Hump auto-map--
		< Setting name= "Mapunderscoretocamelcase" value= "true"/>
	</settings>

</configuration>

The sixth step: the basic profile of SSM has been configured to complete, the next is to configure some other content, such as transaction management, spring and MyBatis integration configuration, not urgent, we first look at the configuration of transaction management, using the AOP aspect-oriented programming, The configuration file Applicationcontext-transaction.xml content is as follows:

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:p=" http://www.springframework.org/schema/p "xmlns:aop=" http://www.springframework.org/ Schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans-4.0.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-4.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-4.0.xsd Http://www.springframework.org/schema/util/HTTP Www.springframework.org/schema/util/spring-util-4.0.xsd "> <!--definition transaction manager-<bean id=" TransactionManager "Class=" Org.springframework.jdbc.datasouRce. Datasourcetransactionmanager "> <property name=" dataSource "ref=" DataSource "/> </bean> <!--define transaction policy --<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > <tx:attributes> <!--all que The methods that begin with RY are read-only--<tx:method name= "query*" read-only= "true"/> <!--other methods use the default transaction policy--<tx:method N Ame= "*"/> </tx:attributes> </tx:advice> <aop:config> <!--pointcut element defines a pointcut, the first star in execution Number to match the return type of the method, where the asterisk indicates that all return types are matched. Com.abc.dao.*.* (..) Shows all methods that match all classes under the Cn.millery.service package--<aop:pointcut id= "mypointcut" expression= "Execution (* Com.millery.servi Ce.*.* (..)) " /> <!--apply a defined transaction-handling policy to the above pointcut--<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Mypointcut"/> </a
 Op:config> </beans>

Seventh step: This step together with spring and MyBatis, the Applicationcontext-mybatis.xml configuration file reads as follows:

<?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:p= "http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "> <!--definition mybatis sqlsessionfactory--<bean id=" Sqlsessionfactory "class=" Org.mybatis.spring.SqlSessionFactoryBean > <!--defining data sources-<property name= "DataSource" ref= "DataSource"/&
		Gt <!--Specify MyBatis Global profile--<property name= "configlocation" value= "Classpath:mybatis/mybaTis-config.xml "/> <!--scan all XML files in Mappers directory and subdirectories--<property name=" mapperlocations "value=" classpath:m Ybatis/mappers/**/*.xml "/> <!--alias Scan Package--<property name=" Typealiasespackage "value=" Com.millery.pojo "/> </bean> <!--definition Mapper interface Scanner--<bean class=" Org.mybatis.spring.mapper.MapperScannerConfigurer "
 > <property name= "basepackage" value= "Com.millery.mapper"/> </bean> </beans>

The eighth step: The configuration is complete, at this time we should try to hackers, the last step is to run the project. Right-click on the project-->run As-->maven build ...---> enter the Tomcat7:run command in the Goals column to witness the moment of the miracle, click the bottom Run button to run the project.


Small episode:

1, the POM configuration file I used coordinates, the use of this coordinate needs to be built in you have to install MAVEN and import the local warehouse and configure to eclipse, how to configure it, the following blog will solve your question.

MAVEN installation and MAVEN configuration in eclipse

2. As with the 1th, you also need to integrate TOMCAT7 in eclipse, without integration, in previous blogs.

Integrated Tomcat in Eclipse

3, in addition to the above configuration, there is a log configuration file log4j.properties:

LOG4J.ROOTLOGGER=DEBUG,A1
log4j.logger.org.mybatis = DEBUG
log4j.appender.a1= Org.apache.log4j.ConsoleAppender
log4j.appender.a1.layout=org.apache.log4j.patternlayout
Log4j.appender.a1.layout.conversionpattern=%-d{yyyy-mm-dd hh:mm:ss,sss} [%t] [%c]-[%p]%m%n

Finally, a picture of the whole project is attached:

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.