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