SpringMVC注册Mapper.xml文件
<!--在applicationcontext_mapper.xml中注册mapper.xml 文件-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 包名是mapper接口所在的包名。 MapperScannerConfigurer会扫描这个包中
的所有接口,把每个接口都执行一次getMapper()方法,得到每个接口的mapper对象。
创建好的mapper对象放入到spring的容器中的。mapper对象的默认名称是 接口名首字母小写 -->
<property name="basePackage" value="com.chen.mapper"/>
</bean>
SpringBoot的注解式注册
方式一: @Mapper
//@Mapper:放在mapper接口的上面, 每个接口都需要使用这个注解。
//@Mapper:告诉MyBatis这是mapper接口,创建此接口的代理对象
@Mapper
public interface StudentMapper {
Student select_( Integer id);
}
方式 二: @MapperScan
// @MapperScan: 找到mapper接口和mapper文件
// basePackages:mapper接口所在的包名(可指定多个包)
@SpringBootApplication
@MapperScan(basePackages = {"com.chen.mapper"})
public class Application {
}
方式三: mapper文件和mapper接口分开
把mapper文件放在resources目录的自定义目录(mapper)下,并在application.properties中,指定该目录。
#mapper文件的自定义位置
mybatis.mapper-locations=classpath:mapper/*.xml