Framework integration--spring and MyBatis framework Integration

Source: Internet
Author: User
Tags metabase

Spring integrates MyBatis1. Integrate Spring

"Consolidation Goal: Configure Sqlsessionfactory in Spring's configuration file and let MyBatis use Spring's declarative transactions"

1). Add spring jar packages and configuration files <1> jar packages required by the Spring framework:

Com.springsource.net.sf.cglib-2.2.0.jar
Com.springsource.org.aopalliance-1.0.0.jar
Com.springsource.org.aspectj.weaver-1.6.8.release.jar
Commons-logging-1.1.1.jar
Spring-aop-4.0.0.release.jar
Spring-aspects-4.0.0.release.jar
Spring-beans-4.0.0.release.jar
Spring-context-4.0.0.release.jar
Spring-core-4.0.0.release.jar
Spring-expression-4.0.0.release.jar
Spring-jdbc-4.0.0.release.jar
Spring-orm-4.0.0.release.jar
Spring-tx-4.0.0.release.jar
Spring-web-4.0.0.release.jar
Spring-webmvc-4.0.0.release.jar

<2>, Spring configuration file Spring.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:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:jdbc= "Http://www.springframework.org/schema/jdbc" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/schema/ Beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPRING-BEANS.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http ://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/context/HTTP/ WWW.SPRINGFRAMEWORK.ORG/SCHEMA/CONTEXT/SPRING-CONTEXT-4.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/JDBC/SPRING-JDBC-4.0.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-4.0.xsd "><!--Scan Package--><context:component-scan base-package = "com.neuedu" &Gt;<context:exclude-filter type= "Annotation" expression= "Org.springframework.stereotype.Controller"/><! --handles global exceptions and can be tagged on the class--><context:exclude-filter type= "annotation" expression= " Org.springframework.web.bind.annotation.ControllerAdvice "/></context:component-scan><!-- Load external properties file (database-driven)--><context:property-placeholder location= "Classpath:jdbc.properties"/><!-- Metabase load content (C3P0)--><bean id= "Combopooleddatasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "user" value= "${jdbc.user}" ></property><property name= "password" value= "${ Jdbc.password} "></property><property name=" Driverclass "value=" ${jdbc.driver} "></property> <property name= "Jdbcurl" value= "${jdbc.url}" ></property></bean> <!--for MyBatis, the transaction manager used is Dat Asourcetransactionmanager--<bean id= "TransactionManager" class= "       Org.springframework.jdbc.datasource.DataSourceTransactionManager ">  <property name= "DataSource" ref= "Combopooleddatasource"/> </bean> <tx:annotation-driven/>&lt ;! --Configure Sqlsessionfactory--><bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= "DataSource" ref= "Combopooleddatasource" ></property> <!--configuration MyBatis configuration file location and name--&gt ; <property name= "configlocation" value= "Classpath:mytabis-config.xml" ></property></bean><!- -Scan interface and SQL configuration file--><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" ><property name= " Basepackage "value=" Com.neuedu.mapper "></property></bean></beans>

  

2). Add the MyBatis jar package and configuration file: The settings is actually the part that needs to be configured. <1>, MyBatis required jar package:

Mybatis-3.2.8.jar

<2>, mybatis configuration file Mytabis-config.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE configurationpublic "-//mybatis.org//dtd Config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" > <!--Master Profile--><configuration><settings> <!--support Database an underscore-named parameter automatically matches a camel-named property in a Project object--><set Ting name= "Mapunderscoretocamelcase" value= "true"/> <!--support lazy loading--<setting name= "Lazyloadi Ngenabled "value=" true "/> </settings> </configuration> </settings></configuration>

3). Join the database driver and database connection pool jar package <1>, join the database driver Jdbc.properties:
Jdbc.user=rootjdbc.password=123456jdbc.url=jdbc:mysql://localhost:3306/mytabisjdbc.driver= Com.mysql.jdbc.Driver
<2>, adding a jar package to the database connection pool:

C3p0-0.9.1.2.jar
Mysql-connector-java-5.1.7-bin.jar

4). Join other required jar packages:<1>, dynamic agent required jar package:

Cglib-2.2.2.jar
Javassist-3.17.1-ga.jar
Asm-3.3.1.jar

<2>, Spring_mybatis framework Integration Jar Package

Mybatis-spring-1.2.2.jar

<3>, Ajax-Required packages

Jackson-all-1.9.11.jar

<4>, log required jar package

Log4j-1.2.17.jar

A total of 24 jar packages required!

5). Configure the data source (Spring.xml configured) in the Spring configuration file.
<!--loading external properties file--><context:property-placeholder location= "classpath:jdbc.properties"/><!-- Metabase load content (C3P0)--><bean id= "Combopooleddatasource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "user" value= "${jdbc.user}" ></property><property name= "password" value= "${ Jdbc.password} "></property><property name=" Driverclass "value=" ${jdbc.driver} "></property> <property name= "Jdbcurl" value= "${jdbc.url}" ></property></bean>

  

6). Configure Sqlsessionfactory in the Spring configuration file (Spring.xml configured)
<!--configuration sqlsessionfactory--><bean id= "sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" ><property name= "DataSource" ref= "Combopooleddatasource" ></property> <!--configuration MyBatis configuration file location and name- -><property name= "configlocation" value= "Classpath:mytabis-config.xml" ></property></bean>

7) Configure the Settings property in the global configuration file of the MyBatis
<configuration><settings> <!--Support Database an underscore-named parameter automatically matches a camel-named property in a Project object--><setting name= "MAPUNDERSC Oretocamelcase "value=" true "/> <!--support lazy loading-<setting name=" lazyloadingenabled "Value=" Tru E "/> </settings> </configuration> </settings></configuration>

  

8). Result of Final integration: You can get Mapper from the IOC container and call the Mapper method directly.

Note: Almost no direct call to Sqlsession is any method.

It is important to note that the transaction for spring is: runtimeexception: While the compile-time exception is not processed, you need to
Set the rollbackfor=exception.class yourself individually
Eg:fileinputstream input = new FileInputStream (New File ("D:\\2323\23.txt"))

Example: A simple operation on a database

Employeemapper.java:

Public interface Employeemapper {public employee Getemployeebyid (int id);}

Employeemapper.xml:

<select id= "Getemployeebyid" parametertype= "Integer" resulttype= "Com.neuedu.Bean.employee" >select id,last_ Name,email,genderfrom tbl_employee where id = #{id} </select>

Test class:

public class Smtest {@Testpublic void Test () {ApplicationContext IOC =new classpathxmlapplicationcontext ("Spring.xml"); Employeemapper bean = Ioc.getbean (employeemapper.class); Employee employee = Bean.getemployeebyid (10); SYSTEM.OUT.PRINTLN (employee);} }

The output is:

Employee [id=10, Lastname=hah, Gender=1, Email=email]

Framework integration--spring and MyBatis framework 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.