Spring +springmvc+mybatis Portfolio Summary

Source: Internet
Author: User

Springmvc+spring+mybatis Integration:
1. Copy the required jar
2. Create a spring configuration file (beans.xml)
3. Configure the data source
<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" ></property>
<property name= "url" value= "Jdbc:mysql://127.0.0.1:3306/test" ></property>
<property name= "username" value= "root" ></property>
<property name= "password" value= "" ></property>
</bean>
4. Create an Sqlsessionfactory instance:
<bean id= "Sqlsessionfactorybean" class= "Org.mybatis.spring.SqlSessionFactoryBean" >
<!--specify data source-
<property name= "DataSource" ref= "DataSource"/>
<!--Specify the configuration file for MyBatis--
<property name= "configlocation" value= "Classpath:mybatis-config.xml"/>
</bean>
5. Configure Transactions
<bean class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
6. Configure the propagation characteristics of a transaction
<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" >
<tx:attributes>
<tx:method name= "find*" read-only= "true"/>
<tx:method name= "get*" read-only= "true"/>
<tx:method name= "query*" read-only= "true"/>
<tx:method name= "add*" propagation= "REQUIRED"/>
<tx:method name= "update*" propagation= "REQUIRED"/>
<tx:method name= "del*" propagation= "REQUIRED"/>
</tx:attributes>
</tx:advice>
7. Configuring AOP
<!--configuring AOP--
<aop:config>
<!--tangent-to-
<aop:pointcut expression= "Execution (* com.liu.service. *.*(..))" Id= "Pointcut"/>
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Pointcut"/>
</aop:config>
8.<!--Mapping Interface--
<bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name= "Basepackage" value= "Com.liu.mapper" ></property>
</bean>
9. Configure Dispatcherservlet
<!--Configure Spring Core controller--
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
10. Create the Springmvc-servlet.xml configuration file:
<!--note Scanner--
<context:component-scan base-package= "Com.liu"/>

<!--configuration attempt resolver--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>

<import resource= "Classpath:beans.xml"/>

Spring +springmvc+mybatis Portfolio Summary

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.