使用注解是开发是少了很多配置文件的麻烦事。但是注解开发也不是全部行的通的,复杂一点的话开始配置文件好一点,注解是对于简单的需求使用。
使用@Mapper就是直接看成了一个配置文件的样子
使用的标签也和配置文件的差不多curd也有相对应的注解
//指定这是一个操作数据库的mapper
@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set departmentName=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
如果你觉得每个类上面配置@Mapper觉的麻烦你可以在程序入口处使用@MapperScan
使用MapperScan批量扫描所有的Mapper接口;
@MapperScan(value = "com.atguigu.springboot.mapper")
@SpringBootApplication
public class SpringBoot06DataMybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot06DataMybatisApplication.class, args);
}
}