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);
    }

}

猜你喜欢

转载自www.cnblogs.com/sitian2050/p/11872161.html