Struts 2.3.7 + spring3.2.0 + mybatis3.1 Integration

Source: Internet
Author: User

I'm bored recently. I just want to integrate SSM ~ Search for information online, but most of them are not entry-level. In particular, the jar files are not specified.

So I will write an article, which is counted as an entry-level tutorial. Do not shoot bricks ~

I. Preparations

First go to struts. http://struts.apache.org/download the latest struts 2.3.7)

And then find spring. http://www.springsource.org/spring-framework download spring-framework-3.2.0.RELEASE-dist

The last is the http://code.google.com/p/mybatis/downloads/list of mybatis3.0? Can = 3 & Q = product % 3 dmybatis download mybatis-3.1.1-bundle

2. Extract the required jar

Here, I will directly integrate SSM. Do not break down. So if you don't understand it, you can do it first. After you go down, you can find the decomposition and integration.

Jar required by struts: jar required by spring: jar required by mybatis other required jar

Struts2-core-2.3.7.jar spring-aop.jar mybatis-3.1.1.jar commons-dbcp-1.4.jar
Xwork-core-2.3.7.jar spring-beans.jar mybatis-spring-1.0.0.jar commons-pool.jar
Commons-fileupload-1.2.2.jar spring-context.jar dom4j. Jar
Commons-lang3-3.1.jar spring-core.jar log4j. Jar
Commons-logging-1.1.1.jar spring-jdbc.jar cglib-2.2.jar
Freemarker-2.3.19.jar spring-orm.jar classes12.jar
Ognl-3.0.5.jar spring-tx.jar
Javassist-3.12.0.GA.jar spring-web.jar
Commons-collections-3.1.jar spring-expression.jar

Struts2-spring-plugin-2.3.7.jar

OK. We have extracted the basic jar required for integration. I am using an Oracle database, so classes12.jar is introduced. You can change it as needed.

Put these jar files under the Lib of our project. I think everyone will.

So far, we have imported all the required jar files. But there is no association. Now let's start to configure the association.

Applicationcontext. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" 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-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.sp Ringframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">! -- Configure the datasource data source --> <Bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" locations "> <value> classpath: JDBC. properties </value> </property> </bean> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "value =" $ {dB. driver} "/> <property name =" url "value =" $ {dB. URL} "/> <property name =" us Ername "value =" $ {dB. username} "/> <property name =" password "value =" $ {dB. password} "/> <property name =" initialsize "value =" $ {dB. initialsize} "/> <property name =" maxactive "value =" $ {dB. maxactive} "/> <property name =" validationquery "value =" $ {dB. validationquery} "/> <! -- <Property name = "defaultautocommit" value = "$ {dB. defaultautocommit}"> </property> --> </bean> <! -- Configure the Transaction Manager. Note that the datasource here must be consistent with the datasource of sqlsessionfactorybean, otherwise the transaction will not work --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. datasourcetransactionmanager "> <property name =" datasource "ref =" datasource "/> </bean> <! -- Create sqlsessionfactory and specify the data source --> <bean id = "sqlsessionfactory" class = "org. mybatis. spring. sqlsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <! -- <Property name = "configlocation" value = "classpath: mybatis-config.xml"> </property> --> </bean> <! -- Configure the propagation feature of the transaction --> <bean id = "basetransactionproxy" class = "org. springframework. transaction. interceptor. transactionproxyfactorybean "abstract =" true "> <property name =" transactionmanager "ref =" transactionmanager "/> <property name =" transactionattributes "> <props> <prop key =" add * "> propagation_required </prop> <prop key =" Edit * "> propagation_required </prop> <prop key =" Remove * "> propagation_required </prop> <prop Key = "insert *"> propagation_required </prop> <prop key = "Update *"> propagation_required </prop> <prop key = "del *"> propagation_required </prop> <prop key = "cancel *"> propagation_required </prop> <prop key = "*"> readonly </prop> </props> </property> </bean> <! -- Configure a Mapper separately. In this mode, you must configure a bean for each mapper interface --> <! -- <Bean id = "accountmapper" class = "org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. hoo. mapper. accountmapper "/> <property name =" sqlsessionfactory "ref =" sqlsessionfactory "/> </bean> <bean id =" companymapper "class =" org. mybatis. spring. mapper. mapperfactorybean "> <property name =" mapperinterface "value =" com. hoo. mapper. companymapper "/> <property name =" sqlsession Factory "ref =" sqlsessionfactory "/> </bean> --> <! -- In the scan mode, the scan directory is under the COM/hoo/mapper directory, and all mapper inherits the interface of the sqlmapper interface, so that a bean can be used.
<Bean class = "org. mybatis. spring. mapper. mapperscannerconfigurer "> <property name =" basepackage "value =" com. mobile. mapper "/> <property name =" markerinterface "value =" com. mobile. mapper. sqlmapper "/> </bean> --> </beans>

JDBC. Properties

 1 db.driver=oracle.jdbc.driver.OracleDriver 2 db.url=jdbc:oracle:thin:@localhost:1521:orcl 3 db.username= 4 db.password= 5 db.alias=OraclePool 6 db.minIdle=5 7 db.maxIdle=10 8 db.maxWait=5 9 db.maxActive=2010 db.initialSize=1011 db.logWriter=12 db.validationQuery=SELECT 1 FROM DUAL

Mybatis. xml

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype configuration public "-// mybatis.org//dtd config 3.0 //" http://mybatis.org/dtd/mybatis-3-config.dtd "> <configuration> <! -- Alias --> <typealiases> </configuration>

Struts. xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>      <package name="my-default" abstract="true" extends="struts-default">            </package>    </struts>

So far, we have integrated the framework.

Deploy to Tomcat to run.

2012-12-29 11:38:13 Org. apache. catalina. core. aprlifecyclelistener init information: the APR based Apache Tomcat native library which allows optimal performance in production environments was not found on the java. library. path: C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin ;.; c: \ windows \ sun \ Java \ bin; c: \ windows \ system32; C: \ WINDOWS; C:/program files/Java/jre6/bin/client; C: /program files/Java/jre6/bin; C:/program files/Java/jre6/lib/i386; F: \ oracle \ product \ 11.1.0 \ db_1 \ bin; C: \ Program Files \ Broadcom 802.11 Network Adapter; c: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; C: \ windows \ system32 \ windowspowershell \ V1.0 \; C: \ Program Files \ widcomm \ Bluetooth Software \; C: \ Program Files \ mysql server 5.5 \ bin; C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ bin; C: \ Program Files \ Java \ jdk1.6.0 _ 25 \ JRE \ bin; F: \ Program Files \ apache-maven-3.0.4 \ bin; F: \ Program Files \ eclipse; 2012-12-29 11:38:13 Org. apache. tomcat. util. digester. setpropertiesrule begin warning: [setpropertiesrule] {server/service/engine/host/context} setting property 'source' to 'org. eclipse. JST. jee. server: ssm'did not find a matching property.2012-12-29 11:38:13 Org. apache. coyote. http11.http11protocol init information: initializing coyote HTTP/1.1 on http-80802012-12-29 11:38:13 Org. apache. catalina. startup. catalina load information: initialization processed in 482 ms2012-12-29 11:38:13 Org. apache. catalina. core. standardservice start information: starting service Catalina2012-12-29 11:38:13 Org. apache. catalina. core. standardengine start information: Starting Servlet Engine: Apache Tomcat/6.0.322012-12-29 11:38:14 Org. apache. catalina. core. applicationcontext log information: initializing spring root WebApplicationContext2012-12-29 11:38:14, 936 [Org. apache. ibatis. logging. logfactory]-[debug] logging initialized using 'org. apache. ibatis. logging. commons. jakartacommonsloggingimpl 'adapter.2012-12-29 11:38:14, 949 [Org. mybatis. spring. sqlsessionfactorybean]-[debug] property 'configlocation' not specified, using default mybatis Configuration2012-12-29 11:38:16, 155 [Org. mybatis. spring. sqlsessionfactorybean]-[debug] property 'mapperlocation' was not specified or no matching resources found2012-12-29 11:38:17 Org. apache. coyote. http11.http11protocol start information: Starting coyote HTTP/1.1 on http-80802012-12-29 11:38:17 Org. apache. JK. common. channelsocket init information: JK: ajp13 listening on/0.0.0.0: 80092012-12-29 11:38:17 Org. apache. JK. server. jkmain start information: JK running id = 0 time = 0/22 Config = null2012-12-29 11:38:17 Org. apache. catalina. startup. catalina start information: server startup in 3410 MS

Error message ~~ Do not be nervous. Because we didn't fully integrate mybatis.

The reference is commented out in the configuration file. These and other project architecture plans can be opened for use.

I am also the first time to integrate SSM.

I would like to use this document to give students who want to learn SSM integration.

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.