This paper mainly introduces the integration of the three frameworks, and the construction of the environment and the creation of the project can refer to other materials. This integration uses two configuration files: spring-mybatis.xm, a configuration file containing spring and MyBatis, and a configuration file for Spring-mvc.xml, plus two resource files: Jdbc.propertis and log4j.properties. The complete directory structure is as follows:
All the jar packages used in this framework are in the source code.
The SQL Server database is used in this test project, MyEclipse 8.6 and apache-tomcat-7.0.41
To describe the configuration file individually:
1 , Spring-mybatis.xml
This file is used to complete the integration of spring and MyBatis. There are not many lines in the configuration, the main is automatic scanning, automatic injection, configuration database, comments are also very detailed
<?xmlversion= "1.0"encoding="UTF-8"?>
<beansxmlns="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 scanned--
<context:component-scanbase-package="Com.myProcess.study"/>
<!--introduce configuration files--
<beanid="Propertyconfigurer"
class="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="Location"value="classpath:jdbc.properties"/>
</bean>
<beanid= "dataSource" class="Org.apache.commons.dbcp.BasicDataSource "
destroy-method="Close">
<propertyname="Driverclassname"value="${driver}"/>
<propertyname="url"value="${url}"/>
<propertyname="username"value="${username}"/>
<propertyname="password"value="${password}"/>
<!--Initialize connection size--
<propertyname= "initialsize"value="${initialsize}"></property>
<!--maximum number of connection pools--
<propertyname= "maxactive"value="${maxactive}"></property>
<!--connection Pool Max free--
<propertyname= "maxidle"value="${maxidle}"></property>
<!--connection Pool min free--
<propertyname= "minidle"value="${minidle}"></property>
<!--get connection Maximum wait time--
<propertyname= "maxwait"value="${maxwait}"></property>
</bean>
<!--spring and MyBatis are perfectly integrated without the need for mybatis Configuration mapping Files--
<beanid= "sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean" >
<propertyname= "dataSource"ref="DataSource"/>
<!--automatically scan mapping.xml files--
<propertyname= "mapperlocations" value="Classpath:com/myprocess/study/mapping/*.xml" > </property>
</bean>
<!--the DAO interface with the package name, Spring automatically finds the class under it--
<beanclass="Org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname= "basepackage"value="Com.cn.hnust.dao"/>
<propertyname= "sqlsessionfactorybeanname" value="Sqlsessionfactory" ></property>
</bean>
<!--(transaction management) transaction manager, use Jtatransactionmanager for globalTX---
<beanid="TransactionManager"
class="Org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname= "dataSource"ref="DataSource"/>
</bean>
</beans>
2, Log4j.properties
In order to facilitate debugging, the general will use the log to output information, log4j is an Apache open source project, through the use of log4j, we can control the log information delivery destination is the console, files, GUI components, even a socket server, NT Event recorder, UNIX Syslog Daemon, etc. we can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. The log4j configuration is simple and versatile, and there's no need to do a lot of tweaking in other projects.
Log4j.rootlogger=info,console,file
#定义日志输出目的地为控制台
Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.target=system.out
#可以灵活地指定日志输出格式, the following line specifies the specific format
log4j.appender.console.layout= Org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionpattern=[%c]-%m%n
#文件大小到达指定尺寸的时候产生一个新的文件
Log4j.appender.File =org.apache.log4j.rollingfileappender
#指定输出目录
Log4j.appender.file.file= Logs/ssm.log
#定义文件最大大小
Log4j.appender.file.maxfilesize= 10MB
# output so log, if switching to debug indicates output debug above level log
Log4j.appender.file.threshold= All
log4j.appender.file.layout= Org.apache.log4j.PatternLayout
log4j.appender.file.layout.conversionpattern=[%p][%d{yyyy-mm-ddhh\:mm\:ss}][%c]%m%n
3 , Spring-mvc.xml
is mainly Automatic Scan controller, view mode, annotation start-up these three a
<?xmlversion= "1.0"encoding="UTF-8"?>
<beansxmlns="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 scans the package so springmvc that the class with @controller annotation under the package is a controller--
<context:component-scanbase-package="Com.myProcess.study.web"/>
<!--avoid IE when performing Ajax, return JSON appears download file--
<beanid="Mappingjacksonhttpmessageconverter"
class="Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<propertyname="Supportedmediatypes">
<list>
<value>text/HTML; CharSet=utf-8</value>
</list>
</property>
</bean>
<!--start the SPRINGMVC annotation function, complete the request and annotation pojo mappings--
<bean
class="Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="Messageconverters">
<list>
<refbean="Mappingjacksonhttpmessageconverter"/><!--JSON Converter--
</list>
</property>
</bean>
<!--define the prefix of the file to jump, view mode configuration--
<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--Here the configuration I understand is to automatically add a prefix and suffix to the string of the method return of the back action, which becomes an available URL address--
<propertyname= "prefix"value="/web-inf/jsp/"/>
<propertyname="suffix"value= ". JSP"/>
</bean>
<!--configuration file upload, if not using file upload can not be configured, of course, if not, then the configuration file does not have to introduce the upload component package--
<beanid="Multipartresolver"
class="Org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--default encoding--
<propertyname= "defaultencoding"value="Utf-8"/>
<!--file Size max--
<propertyname= "maxuploadsize"value="10485760000"/>
<!--maximum value in memory--
<propertyname= "maxinmemorysize"value="40960"/>
</bean>
</beans>
4 , Web. XML
This is the case with the introduction of Spring-mybatis.xml and the configuration of the Spring-mvc servlet in order to complete SSM integration, the previous 2 framework consolidation does not need to be configured here.
<?xmlversion= "1.0"encoding="UTF-8"?>
<web-appversion="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 ">
<display-name>archetype Created Web application</display-name>
<!--spring and mybatis Profiles--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--coded Filter--
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--Spring Listener--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--prevent spring memory overflow listener--
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--Spring MVCservlet---
<servlet>
<servlet-name>myTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>myTest</servlet-name>
<!--here can be configured to/-
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5, jdbc.properties database configuration (URL according to their own IP settings, database name according to their own needs to be named)
Driver=com.microsoft.sqlserver.jdbc.sqlserverdriver
Url=jdbc:sqlserver://192.168.1.202:1433;databasename=spgbid
Username=spgbid
Password=simple
#定义初始连接数
Initialsize=0
#定义最大连接数
Maxactive=20
#定义最大空闲
Maxidle=20
#定义最小空闲
Minidle=1
#最大等待
maxwait=60000
After the profile has been written, the integration of the SSM three frameworks has been completed, followed by testing
Create a JSP page first
Note When you create a JSP page, you must agree with the configuration file. You can create JSP pages directly below the JSP file, or you can create them in a catalog
Create Usercontroller.java
Deploying Projects for access
SPRINGMVC and MyBatis Integration