Spring Boot Mybatis注解:@Mapper和@MapperScan


使用@Mapper注解

添加了@Mapper注解之后這個接口在編譯時會生成相應的實現類,讓其他的類進行引用

@Mapper
public interface EmpMapper {
     public List<Emp> queryAll();
     public Emp queryById(Integer empId);
    void update(Emp emp);

    void deleteById(Integer empId);
    void insertSelective(Emp emp);
}

使用@MapperScan注解

通過使用@MapperScan可以指定要掃描的Mapper類的包的路徑,比如:

@SpringBootApplication
@EnableTransactionManagement //開啟事務管理注解模式 最新的版本可以省略
@MapperScan("com.xz.springboot.mapper") //掃描該包下所有的接口並為該接口生成實現類
public class Springboot01Application {

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

}

使用@MapperScan注解多個包

@SpringBootApplication
@MapperScan("com.xz.springboot.mapper.DeptMapper","com.xz.springboot.mapper.EmpMapper") //掃描該包下所有的接口並為該接口生成實現類
public class Springboot01Application {

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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM