@Mapper注解:Mybatis避免写xml与实体间字段关系映射

21 篇文章 0 订阅
16 篇文章 0 订阅

支持使用注解来配置映射的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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值