使用@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); } }