Maven+spring+spring Mvc+mybatis+mysql, building the SSM framework Environment

Source: Internet
Author: User
Tags log4j

Structure after completion of project construction:

The table structure of the database is as follows:

Environment: Build MAVEN environment, Tomcat Environment, need MySQL database support, use programming tools eclipse (these are the preparation);

Start creating a project:

1. Create a MAVEN project:

Select WebApp

Fill it out:

The project will error when the creation is complete:

What's next?

Select Item Right click, select "Properties" to enter the following interface:

Select Project Facets, and then change the Java version to your JDK version, or 1.6 or later.

Then choose Java Build Path

To modify the Java version inside Lib:

Select the JRE System Library above and select Edit

Save and then look at the project. The main operation above is to make the directory structure appear normal. (The above does not necessarily solve the problem, the project will be an error)

Add dependencies in the Pom.xml file:

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>

This is immediate;

Then add the dependency package to the Pom file, and the final Pom.xml file is as follows:

<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>G1</groupId> <artifactId>C1</artifactId> < packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>c1 Maven webapp</ Name> <url>http://maven.apache.org</url> <properties> <!--Spring Version number---& Lt;spring.version>4.0.2.release</spring.version> <!--mybatis Version number--<mybatis.version& gt;3.2.6</mybatis.version> <!--log4j log file Management Pack version--<slf4j.version>1.7.7</slf4j.vers ion> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> & Lt;dependency> <groupId>junit</groupId>      <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scop e> </dependency> <dependency> <groupId>javax</groupId> &L                    T;artifactid>javaee-api</artifactid> <version>7.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactid>jun              It</artifactid> <version>4.11</version> <!--is introduced at the time of development and will not load this package when it is released-- <scope>test</scope> </dependency> <!--Spring Core pack--<de Pendency> <groupId>org.springframework</groupId> <artifactid>spring-core&lt ;/artifactid> <version>${spring.version}</version> </dependency> &lt         ;d ependency>     <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId>              <version>${spring.version}</version> </dependency> <dependency>              <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId>              <version>${spring.version}</version> </dependency> <dependency>              <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId>              <version>${spring.version}</version> </dependency> <dependency>              <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId>              <version>${spring.version}</version> </dependency> <dependency> <groupId>Org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <versi on>${spring.version}</version> </dependency> <dependency> <groupid& Gt;org.springframework</groupid> <artifactId>spring-aop</artifactId> <versi on>${spring.version}</version> </dependency> <dependency> <groupi              D>org.springframework</groupid> <artifactId>spring-context-support</artifactId>              <version>${spring.version}</version> </dependency> <dependency>              <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId>          <version>${spring.version}</version> </dependency> <!--mybatis Core Pack-- <dependency&gT <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <vers Ion>${mybatis.version}</version> </dependency> <!--mybatis/spring Package--&L T;dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring< /artifactid> <version>1.2.2</version> </dependency> <!--import Java EE Jar Package--<dependency> <groupId>javax</groupId> <artifactId> javaee-api</artifactid> <version>7.0</version> </dependency> <!- -Import MySQL Database link jar pack-<dependency> <groupId>mysql</groupId> <a Rtifactid>mysql-connector-java</artifactid> <version>5.1.30</version> </depe Ndency&gT <!--importing DBCP jar packages to configure the database in Applicationcontext.xml-<dependency> <groupid>commons -dbcp</groupid> <artifactId>commons-dbcp</artifactId> <version>1.2.2< /version> </dependency> <!--jstl Label class--<dependency> <gro Upid>jstl</groupid> <artifactId>jstl</artifactId> <version>1.2</ve Rsion> </dependency> <!--log file Management Pack--<!--log Start-to-<depe              Ndency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- Format objects for easy output logs-<dependency> <groupId>com.alibaba</groupId> <a Rtifactid>fastjson</artifactid> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</arti factid> <version>${slf4j.version}</version> </dependency> <depend Ency> <groupId>org.slf4j</groupId> <artifactid>slf4j-log4j12</artifactid > <version>${slf4j.version}</version> </dependency> <!--log End-- > <!--map JSON--<dependency> <groupid>org.codehaus.jackson</group Id> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version > </dependency> <!--upload Kit--<dependency> <groupid>co Mmons-fileupload</groUpid> <artifactId>commons-fileupload</artifactId> <version>1.3.1</versio              n> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> &LT;/DEP endency> <dependency> <groupId>commons-codec</groupId> &LT;ARTIFAC            Tid>commons-codec</artifactid> <version>1.9</version> </dependency> </dependencies> <build> <finalName>C1</finalName> </build></project>

Then see if Lib has imported the jar package:

Next you need to configure the following four configuration files in the resource directory structure as follows:

Jdbc.properties, Log4j.properties, Spring-mvc.xml, spring-mybatis.xml These files may all need to be created on their own.

You need to create a database table Testu, including four field IDs username password age

The jdbc.properties configuration is as follows:

jdbc.driver=com.mysql.jdbc.driverjdbc.url=jdbc:mysql://localhost:3306/testu?useunicode=true& Characterencoding=utf-8jdbc.username=rootjdbc.password=root#\u5b9a\u4e49\u521d\u59cb\u8fde\u63a5\ u6570jdbc.initialsize=0#\u5b9a\u4e49\u6700\u5927\u8fde\u63a5\u6570  jdbc.maxactive=20#\u5b9a\u4e49\u6700\ U5927\U7A7A\U95F2  jdbc.maxidle=20#\u5b9a\u4e49\u6700\u5c0f\u7a7a\u95f2jdbc.minidle=1#\u5b9a\u4e49\u6700\ u957f\u7b49\u5f85\u65f6\u95f4jdbc.maxwait=60000

The log4j.properties configuration is as follows:

#定义LOG输出级别log4j. rootlogger=info,console,file# definition Log Output destination to console log4j.appender.console= The org.apache.log4j.consoleappenderlog4j.appender.console.target=system.out# provides the flexibility to specify the log output format, The following line specifies the specific format log4j.appender.Console.layout = ORG.APACHE.LOG4J.PATTERNLAYOUTLOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=[%C]-%m%n# When the file size reaches the specified size, a new file is generated log4j.appender.File = org.apache.log4j.rollingfileappender# Specifies the output directory log4j.appender.File.File = logs/ssm.log# definition file Maximum size log4j.appender.File.MaxFileSize = 10mb# output so log, If switching to debug indicates output debug above level log Log4j.appender.File.Threshold = ALLlog4j.appender.File.layout = Org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-mm-dd hh\:mm\:ss}][%c]% m%n

The Spring-mvc.xml configuration is 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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "><!--automatically scan the package to make Springmvc that the class with the @controller annotation under the package is the controller--><context:component-scan base-package= "Com.cn.uuu.controller"/> <!--avoid IE when performing Ajax, return json appears download file--><bean id= "MappingjacksonhttpmessageConverter "class=" Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "><property Name= "Supportedmediatypes" ><list><value>text/html;charset=utf-8</value></list></ property></bean><!--start the SPRINGMVC annotation function to complete the mapping of request and annotation Pojo--><beanclass= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingjacksonhttpmessageconverter "/><!--JSON Converter-- </list></property></bean><!--define the prefix of the file to jump, view mode configuration--><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><!-- The configuration I understand here is to automatically add a prefix and suffix to the return string of the method that follows the action, becoming an available URL address--><property name= "prefix" value= "/web-inf/jsp/"/ ><property name= "suffix" value= ". jsp"/></bean><!--configuration file Upload, if you do not use file upload can not be configured, of course, if not worthy, Then there is no need to introduce the upload component package--><bean id= "Multipartresolver" class= "Org.springframework.web.mult" in the configuration fileIpart.commons.CommonsMultipartResolver "> <!--Default codec--<property name=" defaultencoding "value=          "Utf-8"/> <!--file size max--<property name= "maxuploadsize" value= "10485760000"/> <!--maximum value in memory--<property name= "maxinmemorysize" value= "40960"/> </bean> </beans>

Spring-mybatis.xml configuration file:

<?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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "><!--Auto-Scan--& Gt;<context:component-scan base-package= "com.cn.uuu"/><!--introduce configuration file--><bean id= "Propertyconfigurer" Class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfiGurer "><property name=" Location "value=" Classpath:jdbc.properties "/></bean><bean id=" DataSource "Class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "><property name=" Driverclassname " Value= "${jdbc.driver}"/><property name= "url" value= "${jdbc.url}"/><property name= "username" value= "${ Jdbc.username} "/><property name=" password "value=" ${jdbc.password "/><!--initialize connection size--><property Name= "InitialSize" value= "${jdbc.initialsize}" ></property><!--maximum number of connection pools--><property Name= " Maxactive "value=" ${jdbc.maxactive} "></property><!--connection Pool max idle--><property name=" Maxidle "value=" $ {Jdbc.maxidle} "></property><!--connection pool min idle--><property name=" Minidle "value=" ${jdbc.minidle} "> </property><!--Get connection Maximum wait time--><property name= "maxwait" value= "${jdbc.maxwait}" ></property> </bean><!--Spring and MyBatis are perfectly integrated without the need for MyBatis configuration mapping files--><bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" ><property name= "DataSource" ref= "DataSource"/><!-- Auto Scan mapping.xml file--><property name= "mapperlocations" value= "Classpath:com/cn/uuu/mapping/*.xml" ></ property></bean>< the package name!--the DAO interface, Spring automatically finds the class under it--><bean class= " Org.mybatis.spring.mapper.MapperScannerConfigurer "><property name=" Basepackage "value=" Com.cn.uuu.IDao "/ ><property name= "Sqlsessionfactorybeanname" value= "Sqlsessionfactory" ></property></bean> <!--(transaction management) transaction manager, use Jtatransactionmanager for global TX--><bean id= "TransactionManager" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name= "datasource" ref= " DataSource "/></bean></beans>

Here the configuration file basic configuration is complete

Then how to create a simple instance to run:

You can refer to this article http://blog.csdn.net/cool_easy/article/details/42778239 using code generation. Generated

With these three packages, there are several files that need to be created yourself.

The code structure is as follows

The final code see: http://download.csdn.net/detail/naploen8/8381755

Original link: http://blog.csdn.net/cool_easy/article/details/42963573

Maven+spring+spring Mvc+mybatis+mysql, building the SSM framework Environment

Related Article

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.