回顾上一个案例,如果在真实的项目中,映射器比较多的情况下,我们可以使用MapperScannerConfigure扫描基准包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-3.2.xsd "> 16 17 <!--配置数据源 --> 18 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 19 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 20 <property name="url" 21 value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8"></property> 22 <property name="username" value="root"></property> 23 <property name="password" value="root"></property> 24 25 </bean> 26 <!--配置SqlSessionFactoryBean --> 27 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 28 <!--获取到你的数据源 --> 29 <property name="dataSource" ref="dataSource"></property> 30 <!--获取到mybatis的配置文件 注意这里使用的是value属性 --> 31 <property name="configLocation" value="classpath:mybatis-config.xml"></property> 32 <!--使用下面这种方式获取sql文件 --> 33 <property name="mapperLocations"> 34 <list> 35 <value>classpath:cn/smbms/dao/**/*.xml</value> 36 </list> 37 </property> 38 </bean> 39 40 <!-- 如果在真实的项目中,映射器比较多的情况下,我们可以使用MapperScannerConfigure扫描基准包 --> 41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 42 <property name="basePackage" value="cn.smbms.dao"></property> 43 </bean> 44 45 </beans>
我们可以看下SpringIOC容器为我们创建的Bean的名称:
1 @Test 2 public void testGetUserList(){ 3 4 User userCondition = new User(); 5 userCondition.setUserName("赵"); 6 userCondition.setUserRole(3); 7 //UserMapper userMapper=new UserMapperImpl(); 8 ApplicationContext ctx=new 9 ClassPathXmlApplicationContext("applicationContext.xml"); 10 11 12 //我们可以看下,容器为我们创建的bean的名称 13 for(String name:ctx.getBeanDefinitionNames()){ 14 logger.info("IOC容器创建的Bean:"+name); 15 } 16 UserMapper userMapper=(UserMapper) ctx.getBean("userMapper"); 17 18 19 List<User> userList = userMapper.getUserList(userCondition); 20 21 for(User user: userList){ 22 logger.debug("testGetUserList userCode: " + user.getUserCode() + 23 " and userName: " + user.getUserName() + 24 " and userRole: " + user.getUserRole() + 25 " and userRoleName: " + user.getUserRoleName() + 26 " and age: " + user.getAge() + 27 " and address: " + user.getAddress()); 28 } 29 } 30
运行结果:
1 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:dataSource 2 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:sqlSessionFactory 3 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:org.mybatis.spring.mapper.MapperScannerConfigurer#0 4 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:userMapper 5 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor 6 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor 7 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:org.springframework.context.annotation.internalRequiredAnnotationProcessor 8 [INFO] 2020-01-05 15:17:25,387 cn.smbms.dao.user.UserMapperTest - IOC容器创建的Bean:org.springframework.context.annotation.internalCommonAnnotationProcessor 9 [DEBUG] 2020-01-05 15:17:25,387 org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'userMapper' 10 [DEBUG] 2020-01-05 15:17:25,397 org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 11 [DEBUG] 2020-01-05 15:17:25,397 org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca68fd8] was not registered for synchronization because synchronization is not active 12 [DEBUG] 2020-01-05 15:17:25,417 org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource 13 [DEBUG] 2020-01-05 15:17:25,417 org.mybatis.spring.transaction.SpringManagedTransaction - JDBC Connection [jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver] will not be managed by Spring 14 [DEBUG] 2020-01-05 15:17:25,417 cn.smbms.dao.user.UserMapper.getUserList - ooo Using Connection [jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8, UserName=root@localhost, MySQL-AB JDBC Driver] 15 [DEBUG] 2020-01-05 15:17:25,417 cn.smbms.dao.user.UserMapper.getUserList - ==> Preparing: select u.*,r.roleName from smbms_user u,smbms_role r where u.userName like CONCAT ('%',?,'%') and u.userRole = ? and u.userRole = r.id 16 [DEBUG] 2020-01-05 15:17:25,436 cn.smbms.dao.user.UserMapper.getUserList - ==> Parameters: 赵(String), 3(Integer) 17 [DEBUG] 2020-01-05 15:17:25,446 org.mybatis.spring.SqlSessionUtils - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4ca68fd8] 18 [DEBUG] 2020-01-05 15:17:25,446 org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource 19 [DEBUG] 2020-01-05 15:17:25,446 cn.smbms.dao.user.UserMapperTest - testGetUserList userCode: zhaoyan and userName: 赵燕 and userRole: 3 and userRoleName: 普通员工 and age: 34 and address: 北京市海淀区回龙观小区10号楼