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 ):
<? 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.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
Default-autowire = "byName" default-lazy-init = "false">
<! -- In this example, the DBCP connection pool is used. Copy the jar package of DBCP to the lib directory of the project in advance.
The connection pool configuration is as follows -->
<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 attribute specifies the connection pool to be used -->
<Propertyname = "dataSource" ref = "dataSource"/>
<! -- ConfigLocation attribute specifies the core configuration file of mybatis -->
<Propertyname = "configLocation" value = "resources/configuration. xml"/>
</Bean>
<! -- MapperScannerConfigurer configuration -->
<Beanclass = "org. mybatis. spring. mapper. MapperScannerConfigurer">
<! -- BasePackage: Specifies the package to be scanned. All the mappers under this package will be
Search. Multiple packages can be specified. separate packages with commas (,) or semicolons (;). -->
<Propertyname = "basePackage" value = "com. abc. mapper"/>
</Bean>
</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:
TeacherMapper mapper
= (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:
package com.abc.mapper;
import com.abc.domain.Teacher;
import org.springframework.stereotype.Component;
@Component("myTeacherMapper")
publicinterface TeacherMapper {
public Teacher getById(int id);
}
Correspondingly, the code for accessing this ER in the program should be changed:
TeacherMapper mapper
= (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