Mybatis and Spring Integration

Source: Internet
Author: User

Mybatis and Spring Integration

In this article, we'll learn about the integration of MyBatis and spring.
Mapperfactorybean

We know that all operations in MyBatis are based on a sqlsession, and Sqlsession is produced by Sqlsessionfactory, Sqlsessionfactory is also generated by Sqlsessionfactorybuilder. But Mybatis-spring is based on Sqlsessionfactorybean. In the use of mybatis-spring, we also need to sqlsession, and this sqlsession is embedded in the program, generally do not need our direct access. Sqlsession is also produced by Sqlsessionfactory, but Mybatis-spring has encapsulated a sqlsessionfactorybean for us, In this bean, the corresponding sqlsessionfactory is created by Sqlsessionfactorybuilder, and the corresponding sqlsession is obtained. By Sqlsessionfactorybean We can provide some configuration information for MyBatis by assigning some properties to it. So next we need to define a Sqlsessionfactorybean in spring's applicationcontext configuration file.

<bean id= "Sqlsessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" >              <property name= " DataSource "ref=" DataSource "/>              <property name=" mapperlocations "value="                     classpath:/mybatis/mappers/ *mapper.xml "/>              <property name=" typealiasespackage "value=" Net.zaodk.mybatis.model "/>       </bean >



When defining Sqlsessionfactorybean, the DataSource property must be specified, which represents the data source used to connect to the database. Of course, we can also specify some other attributes, which are briefly listed below:
Mapperlocations

It represents the location of our mapper file, and it is not necessary to specify the value of this property when our mapper file is in the same location as the corresponding mapper interface.
Configlocation

The configuration file location used to specify the MyBatis. If this property is specified, the corresponding Sqlsessionfactorybuilder is constructed with the contents of the configuration file as configuration information, but the contents specified in the subsequent properties overwrite the corresponding content specified in the configuration file.
Typealiasespackage

It typically corresponds to the package where our entity classes are located, and this time automatically takes a simple class name that includes the package name in the corresponding package. Multiple package can be separated by commas or semicolons.
Typealiases:

The array type used to specify the alias. When this attribute is specified, MyBatis will use the short name of this type as an alias for this type, provided that the class does not have a callout @alias annotation, otherwise the value corresponding to that annotation will be used as an alias for this type.

<property name= "typealiases" >    <array>        <value>net.zaodk.mybatis.model.blog</value >        <value>net.zaodk.mybatis.model.Comment</value>    </array> </property>

Plugins

The array type used to specify the interceptor of the MyBatis.
Typehandlerspackage:

Used to specify the package that the Typehandler is in, and if the attribute is specified, Sqlsessionfactorybean automatically registers the class below the package as the corresponding Typehandler. Multiple package can be separated by commas or semicolons.
Typehandlers:

An array type that represents Typehandler.

The next step is to define the Mapperfactorybean of the mapper object that we want in spring's ApplicationContext file. By Mapperfactorybean we can get to the Mapper object we want. Mapperfactorybean implements the spring Factorybean interface, so Mapperfactorybean is the GetObject method defined in the Factorybean interface to obtain the corresponding Mapper object. There are two attributes that we need to inject when defining a mapperfactorybean. One is the sqlsessionfactory of the mybatis-spring used to generate the Sqlsessiontemplate object that implements the Sqlsession interface, and the other is the corresponding mapper interface we want to return.

After defining the corresponding Mapperfactorybean for the respective mapper interface, we can inject our corresponding Mapper interface into the Bean object managed by spring, such as the service Bean object. So when we need to use the corresponding mapper interface, Mapperfactorybean will get the corresponding mapper interface from its GetObject method. In GetObject, the Getmapper (Mapper interface) method of the Sqlsession interface is called through the properties we inject to return the corresponding mapper interface. This enables the integration of MyBatis with spring by handing Sqlsessionfactory and the corresponding mapper interface to spring management.

<?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:context="Http://www.springframework.org/schema/context"Xmlns:mvc="Http://www.springframework.org/schema/mvc"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-3.0.xsdhttp//Www.springframework.org/schema/mvchttp//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><context:component-scanBase-package="Net.zaodk.mybatis"/> <context:property-placeholder location="classpath:config/jdbc.properties"/> <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}"/> </bean> <bean id="sqlsessionfactory" class="Org.mybatis.spring.SqlSessionFactoryBean"> <property name="DataSource" ref="DataSource"/> <property name="mapperlocations"Value="Classpath:/mybatis/mapper/*.xml"/> <property name="Typealiasespackage"Value="Net.zaodk.mybatis.model"/> </bean> <bean id="Blogmapper" class="Org.mybatis.spring.mapper.MapperFactoryBean"> <property name="Mapperinterface"value="Net.zaodk.mybatis.mapper.BlogMapper"/> <property name="sqlsessionfactory" ref="sqlsessionfactory"/> </bean> </beans>

Blogmapper.xml file

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < Mapper namespace= "Net.zaodk.mybatis.mapper.BlogMapper" > <!--new record-<insert id= "Insertblog" Parameterty Pe= "Blog" usegeneratedkeys= "true" keyproperty= "id" > INSERT into T_blog (Title,content,owner) VALUES (#{title},#{co Ntent},#{owner}) </insert> <!--query single record--<select id= "Selectblog" parametertype= "int" resultmap= "B Logresult "> select * from t_blog where id = #{id} </select> <!--modify Record-<update id=" UPDA Teblog "parametertype=" Blog > Update t_blog Set title = #{title},content = #{content},owner = #{owner} where ID  = #{id} </update> <!--query all records--<select id= "SelectAll" resulttype= "Blog" > select * FROM T_blog </select> <!--delete records-<delete id= "Deleteblog" parametertype= "int" >       Delete from T_blog where id = #{id} </delete> </mapper> 

Blogmapper.java:

Publicinterface blogmapper {public    Blog selectblog (int id);    publicvoid insertblog (blog blog);    publicvoid updateblog (blog blog);    Publicvoid deleteblog (int id);    Public list<blog> SelectAll ();} Blogserviceimpl.java@service Publicclass Blogserviceimpl implements Blogservice {    private blogmapper BlogMapper;    Publicvoid deleteblog (int id) {       blogmapper.deleteblog (ID);    }    Public Blog find (int id) {       returnblogmapper.selectblog (ID);    }    Public list<blog> Find () {       returnblogmapper.selectall ();    }    publicvoid insertblog (Blog blog) {       blogmapper.insertblog (blog);    }    publicvoid updateblog (Blog blog) {       blogmapper.updateblog (blog);    }    Public Blogmapper Getblogmapper () {       returnblogmapper;    }    @Resource    publicvoid setblogmapper (blogmapper blogmapper) {       this.blogmapper = Blogmapper;    }}

Manuscripts: Diligent Learning qkxue.net

Extended reading:

Mybatis and Spring Integrate a

Http://qkxue.net/info/25402/Mybatis-nbsp-spring

Mybatis and Spring Integration II

Http://qkxue.net/info/25968/Mybatis-nbsp-spring

Mybatis and Spring 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.