MyBatis mapperscannerconfigurer Configuration ――mybatis Learning notes eight

Source: Internet
Author: User

MyBatis mapperscannerconfigurer Configuration--mybatis Learning notes eight2012-09-02 20:01:42Tags: Spring MyBatis mapperscannerconfigurer Beans By default named original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://legend2011.blog.51cto.com/3018495/980150

In the example of the previous blog post, we configured Studentmapper and Teachermapper in Beans.xml for use when we needed it. However, if more mapper is needed, it is inefficient to use this configuration method. To solve this problem, we can use Mapperscannerconfigurer to scan specific packages and automatically help us create the mapper in batches. In this way, the workload of the configuration can be greatly reduced. As shown below (click here to access this sample source program download page):

12345678910111213141516171819202122232425262728293031323334 <?xmlversion="1.0" encoding="utf8"?><beansxmlns="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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"default-autowire="byName"default-lazy-init="false"><!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。连接池配置如下--><beanid="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost/courseman"/><propertyname="username"value="courseman"/><propertyname="password"value="abc123"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--dataSource属性指定要用到的连接池--><propertyname="dataSource"ref="dataSource"/><!--configLocation属性指定mybatis的核心配置文件--><propertyname="configLocation" value="resources/configuration.xml"/></bean><!--MapperScannerConfigurer配置--><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。可指定多个包,包与包之间用逗号或分号分隔--><propertyname="basePackage"value="com.abc.mapper"/></bean></beans>

Here are three points to note:

First, you do not need to specify a reference sqlsessionfactory, because Mapperscannerconfigurer is referenced by automatic assembly when creating the mapper.

Second, create a naming problem for the mapper. As we can see from the Beans.xml file, we have no way to specify the ID or name attribute for these mappers created by Mapperscannerconfigurer, which seems to us to be invisible. The solution to this problem is the use of spring's default naming strategy for automatically detected components, that is, the first letter of the class/interface name is lowercase, the other unchanged, as the name of the mapper. For example, the Mapper interface Teachermapper is scanned and created after the mapper bean is named Teachermapper. Therefore, we can use this code as before to get the Teachermapper instance:

1 TeacherMapper mapper  =  (TeacherMapper)ctx.getBean("teacherMapper");

Thirdly, you can use the @component annotation to specify a name for the mapper (this is how the source program for this example is used). Here, for example, if you want to specify a generated mapper bean name called "Myteachermapper", proceed as follows: Teachermapper

1, add the following declaration in the Teachermapper interface: "Import org.springframework.stereotype.Component;" ;

2. Add @component ("Myteachermapper") annotations before the interface declaration, that is, specify that the generated mapper name is Myteachermapper.

Source code (Teachermapper.java) is as follows:

1234567 package  com.abc.mapper; import  com.abc.domain.teacher; import   org.springframework.stereotype.Component; @Component ( "Myteachermapper" public  interface   teachermapper { public   teacher getById ( int   id);

Accordingly, the code that accesses this mapper in the program should read:

1 TeacherMapper mapper = (TeacherMapper)ctx.getBean("myTeacherMapper");

The results of the operation are as follows:

And a little bit, incidentally, If the Mapper interface (such as the Teachermapper interface) has the same name as the corresponding mapping configuration file (such as Teachermapper.xml) and is in the same directory, you do not need to use the mappers element in the core configuration file Configuration.xml to specify the mapping configuration file. The reader can experiment on its own.

Resources:

1, Http://www.mybatis.org/spring/zh/mappers.html#MapperScannerConfigurer (Chinese)

2. Http://www.mybatis.org/spring/mappers.html#MapperScannerConfigurer (English)

poke here to learn mybatis fully systematically 3

MyBatis Technology Group: 188972810, or scan QR code:

Preparation for the "MyBatis Study Notes" series: Ant Download and installation

Preparation of the "MyBatis study Notes" Series II: Ant Getting Started example

One of the "MyBatis Study Notes" series: MyBatis Getting Started sample

"MyBatis Study Notes" series bis: MyBatis additions and deletions example

"MyBatis Study Notes" Series III: MyBatis's Association example

"MyBatis Study Notes" series Four: two forms of MyBatis Association

"MyBatis Study notes" series of Five: MyBatis and spring integration examples

"MyBatis Study Notes" Series VI: MyBatis and Spring integration examples continued

"MyBatis Study Notes" series VII: MyBatis one-to-many bidirectional association

"MyBatis Study notes" series of Eight: MyBatis mapperscannerconfigurer configuration

"MyBatis Study Notes" Series IX: Two forms of MyBatis collection

"MyBatis Study Notes" series of ten: MyBatis log log4j Example

Xi. "MyBatis Study Notes" series: an example of annotated mode of MyBatis multi-parameter transfer

"MyBatis Study Notes" series 12: mybatis example of the default naming method for multi-parameter delivery

"MyBatis Study Notes" series 13: Example of map mode of MyBatis multi-parameter transfer

"MyBatis Study Notes" series 14: n+1 Problems in MyBatis

The "MyBatis Study Notes" series: a hybrid approach to mybatis multi-parameter transfer

"MyBatis Study Notes" series 16: Spring Declarative Transaction Management Example

"MyBatis Study Notes" series 17: MyBatis Many-to-many save example

"MyBatis Study Notes" series 18: MyBatis Many-to-many association query example

"MyBatis Study Notes" series 19: How to use LOG4J2 in MyBatis-3.2.7 RC2

How to write DAO in MyBatis by inheriting Sqlsessiondaosupport (a)

How to write DAO in MyBatis by inheriting Sqlsessiondaosupport (ii)

This article is from the "Xiaofan column" blog, be sure to keep this source http://legend2011.blog.51cto.com/3018495/980150

MyBatis mapperscannerconfigurer Configuration ――mybatis Learning notes eight

Related Article

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.