支持使用注解来配置映射的sql语句,这样可以省掉映射器xml文件。
参考与详细原理
@Mapper注解详解:使用方法、源码
https://mynamelancelot.github.io/mybatis/mapper.html
使用注解配置Mapper
https://blog.csdn.net/soonfly/article/details/67640653
实例
Application配置指定mapper文件扫描位置
@MapperScan
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
添加位置:是在Springboot启动类上面添加
@Slf4j
@EnableSwagger2
@SpringBootApplication(scanBasePackages = {"com.cg.a","com.cg.b.sdk"})
@MapperScan(basePackages = {
"com.cg.a.common.infrastructure.mapper",
"com.cg.b.open.mapper",
})
@EnableAsync
public class Application {
}
配置文件中
mybatis:
mapper-locations:
- classpath*:mapper/*.xml
mybatis:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 其他数据库链接的配置
Mapper接口:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
由Mybatis负责生成其实现类
@Mapper
public interface AMapper {
@Select({"<script> select system_id as systemId,permanent_code as permanentCode, tenant from open_auth where permanent_code=#{code,jdbcType=VARCHAR} "+
"</script>"})
List<PermanentCode> queryByCode(@Param(value = "code") String code);
@Select({"<script> select id from lead where sysetem_id='A' </script>"})
List<Long> select();
@Select({"<script> DELETE from lead_info where lead_id=#{code,jdbcType=BIGINT} </script>"})
void delete(@Param(value = "code") Long code);
}