springboot配置扫描DAO层

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28379809/article/details/82800740

可以使用三种注解来引入DAO层的接口到spring容器中。
1.@Mapper,写在每一个DAO层接口上,如下:

//@Mapper
public interface UserDAO {
  public User find(@Param("name") String name, @Param("password") String password);
}

2.@MapperScan@ComponentScan两者之一。前者的意义是将指定包中的所有接口都标注为DAO层接口,相当于在每一个接口上写@Mapper。后者则是代替所有@Component,包括@Service、@Repository、@Controller等。如下:

@SpringBootApplication
/*@ComponentScan(basePackages = "com.example.demo.dao")*/
@MapperScan(basePackages = "com.example.demo.dao")
public class UserManagerApplication {

  public static void main(String[] args) {
    SpringApplication.run(UserManagerApplication.class, args);
  }
}

猜你喜欢

转载自blog.csdn.net/qq_28379809/article/details/82800740