How to Use mybatis 3 and mybatis 3

Source: Internet
Author: User

How to Use mybatis 3 and mybatis 3

The preceding section describes how to use mybatis independently. In actual development, mybatis is often used with spring, that is, integration between mybatis and spring. Now let's look at how to integrate it.

Integration of mybatis and spring requires the use of the integration package: mybatis-spring-1.1.1.jar, this package provides mybatis and spring integration support, the package imported to the project lib directory.

Let's first look at the process when mybatis is used separately. mybatis configuration file = read configuration file = operate on the database. For specific usage instructions, see the first two articles.

The following describes how to integrate mybatis with spring,

1. mybatis configuration file

Some configurations in the mybatis configuration file are no longer needed during integration with spring. spring will use its own configuration file. For example, for the data source, see the configuration file mybatis, MybatisConfiguration. xml,

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>          <typeAliases>         <typeAlias alias="Message" type="com.cn.imooc.entity.Message"/>     </typeAliases>     <mappers>         <mapper resource="com/cn/mappers/message.xml"/>     </mappers> </configuration>

The Configuration File above configures the alias and mappers ing files. Compared with the previous configuration file, we can see that there is no information about the data source. Here, you do not need to configure the data source in the configuration file of mybatis, it must be configured in the spring configuration file.

Ii. spring configuration file

Since it is integrated with spring, you must import the spring package. The spring package can be obtained from the previous article. After importing the spring package, you need to configure the spring configuration file, we put the spring configuration file under src, named spring-application.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: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.s Pringframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <bean id =" address "class =" com.cn. test. spring. Address "> </bean> <! -- Introduce the jdbc configuration file --> <! -- <Context: property-placeholder location = "jdbc. properties"/> --> <! -- 1. Create a jdbc data source --> <bean id = "dataSource" class = "org. springframework. jdbc. datasource. driverManagerDataSource "> <property name =" driverClassName "value =" com. mysql. jdbc. driver "/> <property name =" url "value =" jdbc: mysql: // 127.0.0.1: 3306/weixin? UseUnicode = true & amp; characterEncoding = UTF-8 "/> <property name =" username "value =" root "/> <property name =" password "value =" 123456 "/> </bean> <! -- 2. sqlSessionFactoryBean -->
<Bean id = "sqlSessionFactory" class = "org. mybatis. spring. sqlSessionFactoryBean "> <property name =" dataSource "ref =" dataSource "> </property> <property name =" configLocation "value =" classpath: MybatisConfiguration. xml "> </property> <! -- <Property name = "mapperLocations" value = "classpath: com/cn/mappers/message. xml "> </property> --> </bean> <bean id =" messageMapper "class =" org. mybatis. spring. mapper. mapperFactoryBean "> <property name =" mapperInterface "value =" com.cn. inter. IMessageOperation "/> <property name =" sqlSessionFactory "ref =" sqlSessionFactory "/> </bean> </beans>

First, we have configured a data source. If the context namespace is introduced, you can use <context: property-placeholder location = "jdbc. properties "/> to introduce the configuration file under src.

Secondly, sqlSessionFactoryBean is configured. Here, sqlSessionFactoryBean is used to generate sqlSessionFactory (in mybatis, sqlSessionFactory is generated by sqlSessionFactoryBuilder ). SqlSessionFactroyBean has the following attributes to generate sqlSessionFactroy,

DataSourceThat is, the data source just configured, specifying the data source used to generate sqlSessionFactory

ConfigLocationThis attribute specifies the path of the configuration file of mybatis. In this example, we use MybatisConfiguration under src. xml. If the mappers ing file is configured in this file, the third attribute is not required. If the ing file is not configured, the third attribute is required. if no ing file is configured in the xml file or the mapperLocations attribute is not configured, The ing file must be in the same package as the er Class, And the ing file and the er Class must have the same name.

MapperLocationsSpecifies the mappers ing file. This attribute can be configured as a list value.

Finally, use a dynamic proxy to generate the code for accessing the database. MapperFactoryBean, as a factory class, can be used to generate a dynamic proxy for accessing the database. There are two ways to generate a dynamic Proxy, the mapperInterface and sqlSessionFactory attributes are used here. The first attribute specifies the full path of the Mapper class, and the second attribute is the above sqlSessionFactory. The other method is the annotation method.

Now, the spring configuration file is complete and can be tested. The test code is as follows,

Package com.cn. test. spring; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. mybatis. spring. support. sqlSessionDaoSupport; import org. springframework. context. support. classPathXmlApplicationContext; import com.cn. imooc. entity. message; import com.cn. inter. IMessageOperation; public class TestSpringAndMybatis {public static void main (String [] args) {// TODO Auto-generated method stub // get spring configuration ClassPathXmlApplicationContext cpxac = new handler ("spring-application.xml"); // get IMessageOperation Interface Class IMessageOperation imo = (IMessageOperation) cpxac. getBean ("messageMapper"); Message m = imo. selectMessageById ("2"); System. out. println (m );}}

The above completes the integration of mybatis and spring. We will find that if there are multiple mapper classes during proxy generation, you need to configure them multiple times, which is troublesome, the next article uses another method.

Correction if any error exists!

Thank you!

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.