MyBatis MapperScannerConfigurer configuration -- eight of MyBatis Study Notes

Source: Internet
Author: User

In the example in the previous blog, studentMapper and teacherMapper are configured in beans. xml for use as needed. However, if many mappers are needed, this configuration method is inefficient. To solve this problem, we can use MapperScannerConfigurer to scan specific packages and automatically create mappers in batches. In this way, the configuration workload can be greatly reduced. Click here to go to the download page of the source program in this example ):

 
 
  1. <? Xmlversion = "1.0" encoding = "utf8"?>

  2. <Beansxmlns = "http://www.springframework.org/schema/beans"

  3. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

  4. Xmlns: aop = "http://www.springframework.org/schema/aop"

  5. Xmlns: tx = "http://www.springframework.org/schema/tx"

  6. Xmlns: context = "http://www.springframework.org/schema/context"

  7. Xsi: schemaLocation ="

  8. Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  9. Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

  10. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

  11. Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

  12. Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

  13. Default-autowire = "byName" default-lazy-init = "false">

  14. <! -- In this example, the DBCP connection pool is used. Copy the jar package of DBCP to the lib directory of the project in advance.

  15. The connection pool configuration is as follows -->

  16. <Beanid = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource">

  17. <Propertyname = "driverClassName" value = "com. mysql. jdbc. Driver"/>

  18. <Propertyname = "url"

  19. Value = "jdbc: mysql: // localhost/courseman"/>

  20. <Propertyname = "username" value = "courseman"/>

  21. <Propertyname = "password" value = "abc123"/>

  22. </Bean>

  23. <Beanid = "sqlSessionFactory" class = "org. mybatis. spring. SqlSessionFactoryBean">

  24. <! -- DataSource attribute specifies the connection pool to be used -->

  25. <Propertyname = "dataSource" ref = "dataSource"/>

  26. <! -- ConfigLocation attribute specifies the core configuration file of mybatis -->

  27. <Propertyname = "configLocation" value = "resources/configuration. xml"/>

  28. </Bean>

  29. <! -- MapperScannerConfigurer configuration -->

  30. <Beanclass = "org. mybatis. spring. mapper. MapperScannerConfigurer">

  31. <! -- BasePackage: Specifies the package to be scanned. All the mappers under this package will be

  32. Search. Multiple packages can be specified. separate packages with commas (,) or semicolons (;). -->

  33. <Propertyname = "basePackage" value = "com. abc. mapper"/>

  34. </Bean>

  35. </Beans>

Note the following three points: first, you do not need to specify the reference SqlSessionFactory, because MapperScannerConfigurer will be referenced by automatic assembly when creating the mapper. Second, the name of the created mapper. From the beans. xml file, we can see that there is no way to specify the id or name attributes for the MapperScannerConfigurer. They seem invisible to us. The solution to this problem is that Spring uses the default naming policy for automatically detected components, that is, the first letter of the class/interface name is lowercase, And the other words remain unchanged, which serves as the name of the er. For example, the er bean created after the ER interface TeacherMapper is scanned is named teacherMapper. Therefore, we can use such code as before to get the TeacherMapper instance:
 
 
  1. TeacherMapper mapper  

  2.         =  (TeacherMapper)ctx.getBean("teacherMapper"); 

Third, you can use the @ Component annotation to specify the name of the er. This method is used for the source program in this example ). Take TeacherMapper as an example. to specify the name of the generated er bean as "myTeacherMapper", follow these steps: 1. Add the following statement to the TeacherMapper interface: "import org. springframework. stereotype. component; "; 2. Add the @ Component (" myTeacherMapper ") annotation before the interface declaration, that is, specify the name of the generated er as myTeacherMapper. Source code TeacherMapper. java) is as follows:
 
 
  1. package com.abc.mapper;  

  2. import com.abc.domain.Teacher;  

  3. import org.springframework.stereotype.Component;  

  4. @Component("myTeacherMapper")  

  5. publicinterface TeacherMapper {  

  6. public Teacher getById(int id);  

  7. }  

Correspondingly, the code for accessing this ER in the program should be changed:
 
 
  1. TeacherMapper mapper  

  2.         = (TeacherMapper)ctx.getBean("myTeacherMapper"); 

The running result is as follows:

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201209/194913431.png "border =" 0 "/>

By the way, if the ER interface such as TeacherMapper Interface) maps to the corresponding configuration file such as TeacherMapper. xml. the mappers element is used in xml to specify the ing configuration file. You can experiment on your own.

References:

1. http://www.mybatis.org/spring/zh/mappers.html#mapperscannerconfigurerchinese)

2. http://www.mybatis.org/spring/mappers.html?mapperscanner=er)

MyBatis Study Notes series I: download and install ant

Prepare for MyBatis Study Notes Series II: ant getting started

MyBatis learning notes: MyBatis getting started

MyBatis Study Notes Series II: Example of adding, deleting, and modifying MyBatis

MyBatis Study Notes Series 3: association examples of MyBatis

MyBatis Study Notes Series 4: two forms of MyBatis association

MyBatis Study Notes Series 5: examples of integration between MyBatis and Spring

MyBatis Study Notes Series 6: examples of integration between MyBatis and Spring

MyBatis study notes 7: MyBatis one-to-multiple bidirectional Association

MyBatis Study Notes: MyBatis MapperScannerConfigurer Configuration

MyBatis Study Notes: two forms of MyBatis collection

MyBatis Study Notes Series 10: Log4j example of MyBatis logs

MyBatis Study Notes: example of how to annotate MyBatis with multiple parameters

MyBatis learning notes: Example of the default naming method for MyBatis multi-parameter transfer

MyBatis Study Notes: Example of Map mode for MyBatis multi-parameter transfer

MyBatis Study Notes: N + 1 in MyBatis

MyBatis Study Notes: a hybrid transfer of multiple parameters in MyBatis

MyBatis Study Notes:Spring declarative Transaction Management example

MyBatis Study Notes: Example of MyBatis multiple-to-multiple storage

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

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.