参考:Spring Boot的MyBatis注解:@MapperScan和@Mapper
1、@Mapper单独加在Mapper上
package com.tj.demo.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tj.demo.system.domain.Users;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UsersMapper extends BaseMapper<Users> {
//继承BaseMapper常用的增删改查
}
2、@MapperScan加在main主启动程序上,路径下的mapper就不需要在加@Mapper
package com.tj.demo;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@Slf4j //log日志
@SpringBootApplication
@ServletComponentScan //扫描WebFilter的注解
@MapperScan({"com.tj.demo.system.mapper"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.info("项目启动成功....");
}
}
多个mapper可以这样写,如果mapper没有scan就会报错
@Slf4j //log日志
@SpringBootApplication
@ServletComponentScan //扫描WebFilter的注解
@MapperScan({"com.tj.system.mapper","com.tj.base.mapper"}) //这个里加了,mapper下就不需要再加@Mapper了
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.info("项目启动成功....");
}
}