Integration of spring and mybaits

Source: Internet
Author: User

This article mainly introduces spring and mybatis three common integration methods, the need of the integrated rack package is mybatis-spring.jar, through the link

Http://code.google.com/p/mybatis/download.

1. mapperfactorybean provides corresponding SQL statements and input parameters without writing the mybatis ing file.
(1) Spring configuration file:

<! -- Introduce the JDBC configuration file -->
<Context: Property-placeholder location = "JDBC. properties"/>

<! -- Create a JDBC data source -->
<Bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource" Destroy-method = "close">
<Property name = "driverclassname" value = "$ {driver}"/>
<Property name = "url" value = "$ {URL}"/>
<Property name = "username" value = "$ {username}"/>
<Property name = "password" value = "$ {password}"/>
<Property name = "initialsize" value = "$ {initialsize}"/>
<Property name = "maxactive" value = "$ {maxactive}"/>
<Property name = "maxidle" value = "$ {maxidle}"/>
<Property name = "minidle" value = "$ {minidle}"/>
</Bean>

<! -- Create sqlsessionfactory and specify the data source -->
<Bean id = "sqlsessionfactory" class = "org. mybatis. Spring. sqlsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
</Bean>

<! -- Create a data er. The data er must be an interface -->
<Bean id = "usermapper" class = "org. mybatis. Spring. Mapper. mapperfactorybean">
<Property name = "mapperinterface" value = "com. xxt. ibatis. DBCP. Dao. usermapper"/>
<Property name = "sqlsessionfactory" ref = "sqlsessionfactory"/>
</Bean>

<Bean id = "userdaoimpl2" class = "com. xxt. ibatis. DBCP. Dao. impl. userdaoimpl2">
<Property name = "usermapper" ref = "usermapper"/>
</Bean>

(2) Data er usermapper, the Code is as follows: public interface usermapper {
@ Select ("select * from user where id = # {userid }")
User getuser (@ param ("userid") Long ID );
} (3) Dao Interface Class userdao, the Code is as follows: public interface userdao {
Public user getuserbyid (User user );
} (4) DAO implementation class userdaoimpl2, the Code is as follows:

Public class userdaoimpl2 implements userdao {
Private usermapper;

Public void setusermapper (usermapper ){
This. usermapper = usermapper;
}

Public user getuserbyid (User user ){
Return usermapper. getuser (user. GETID ());
}
}

2. org. Apache. ibatis. session. sqlsession implementation class org. mybatis. Spring. sqlsessiontemplate is used.
In mybatis, sessionfactory can be created by sqlsessionfactorybuilder. In mybatis-spring, sqlsessionfactorybean is used instead. Sqlsessionfactorybean has a required attribute datasource and a common attribute configlocation (used to specify the xml configuration file path of mybatis ).
(1) Spring configuration file: <! -- Create sqlsessionfactory and specify the data source -->
<Bean id = "sqlsessionfactory" class = "org. mybatis. Spring. sqlsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
<! -- Specify the sqlmapconfig configuration file. The customized environment does not take effect in the spring container. -->
<Property name = "configlocation" value = "classpath: sqlmapconfig. xml"/>
<! -- Specifies the object class ing file. You can specify all configuration files under a package and sub-package at the same time. Either mapperlocations or configlocation can be specified. When you need to specify an alias for the object class, you can specify the configlocation attribute, and then use Mapper to introduce the object class ing file in the mybatis configuration file -->
<! --<Property name = "mapperlocations" value = "classpath *: COM/xxt/ibatis/DBCP/**/*. xml"/> -->
</Bean> (2) mybatis configuration file sqlmapconfig. xml: <configuration>
<Typealiases>
<Typealias type = "com. xxt. ibatis. DBCP. domain. User" alias = "user"/>
</Typealiases>
<Mappers>
<Mapper resource = "com/xxt/ibatis/DBCP/domain/user. Map. xml"/>
</Mappers>
</Configuration> (3) object class ing file user. Map. xml: <mapper namespace = "com. xxt. ibatis. DBCP. domain. User">
<Resultmap type = "user" id = "usermap">
<ID property = "ID" column = "ID"/>
<Result property = "name" column = "name"/>
<Result property = "password" column = "password"/>
<Result property = "createtime" column = "createtime"/>
</Resultmap>
<Select id = "getuser" parametertype = "user" resultmap = "usermap">
Select * from user where ID =#{ ID}
</SELECT>
<Mapper/> (4) Dao layer interface implementation class userdaoimpl: public class userdaoimpl implements userdao {
Public sqlsessiontemplate sqlsession;
Public user getuserbyid (User user ){
Return (User) sqlsession. selectone ("com. xxt. ibatis. DBCP. domain. User. getuser", user );
}
Public void setsqlsession (sqlsessiontemplate sqlsession ){
This. sqlsession = sqlsession;
}
} 3. Use the abstract class org. mybatis. Spring. Support. sqlsessiondaosupport to provide sqlsession.
(1) Spring configuration file:

<Bean id = "sqlsessionfactory" class = "org. mybatis. Spring. sqlsessionfactorybean">
<Property name = "datasource" ref = "datasource"/>
<Property name = "configlocation" value = "classpath: sqlmapconfig. xml"/>
<! -- <Property name = "mapperlocations" value = "classpath *: COM/xxt/ibatis/DBCP/domain/user. Map. xml"/> -->
</Bean>

<Bean id = "sqlsession" class = "org. mybatis. Spring. sqlsessiontemplate">
<Constructor-Arg Index = "0" ref = "sqlsessionfactory"/>
</Bean>

<Bean id = "userdaoimpl3" class = "com. xxt. ibatis. DBCP. Dao. impl. userdaoimpl3">
<! -- Inject sqlsessiontemplate instance -->
<Property name = "sqlsessiontemplate" ref = "sqlsession"/>
<! -- You can also directly inject the sqlsessionfactory instance. Both of them indicate that the sqlsessionfactory is invalid. -->
<! -- <Property name = "sqlsessionfactory" ref = "sqlsessionfactory"/>
-->
</Bean>

(2) The Dao layer interface implementation class userdaoimpl3:
Public class userdaoimpl3 extends sqlsessiondaosupport implements userdao {
Public user getuserbyid (User user ){
Return (User) getsqlsession (). selectone ("com. xxt. ibatis. DBCP. domain. User. getuser", user );
}
} Address: http://blog.csdn.net/bluesky5219/article/details/7066174

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.