• 「分布式」——微服务抽奖系统后台整合MyBatis-Plus


     

    1.整合MyBatis-Plus背景

    【分布式】-- 基于Nacos、OpenFeign搭建的微服务抽奖系统后台小案例

    本篇是基于微服务抽奖系统后台对持久层MyBatis进行更换,并整合MyBatis-Plus替换掉原来的MyBatis框架为目的来进行整合说明的。

    1.1.为什么要使用MP

    基于MyBatis-Plus本身jar包底层就包含了MyBatis的基本jar,是在MyBatis基础上的进一步扩展,而且就如同使用JPA一样,不需要我们再去编写基础的xml与注解sql。

    可以使用注解方式完成基本的表与PoJo之间的映射关系。

    2.基于抽奖系统整合MyBatis-Plus

    基于provider-product6700服务方进行添加配置。

    2.1.配置pom依赖

     

    1. <!--mybatis-plus插件-->
    2. <dependency>
    3. <groupId>com.baomidou</groupId>
    4. <artifactId>mybatis-plus-boot-starter</artifactId>
    5. <version>3.3.2</version>
    6. </dependency>

    2.2.对应的application.yml配置

    1. #整合mybatis-plus配置
    2. mybatis-plus:
    3. #配置Mapper映射文件
    4. mapper-locations: classpath:/mybatis/mapper/*.xml
    5. # 配置Mybatis数据返回类型别名(默认别名为类名)
    6. type-aliases-package: com.fengye.springcloud.entities
    7. configuration:
    8. #自动驼峰命名
    9. map-underscore-to-camel-case: false
    10. #配置控制台打印日志Debug
    11. logging:
    12. level:
    13. com.jd.mapper: debug

    2.3.修改Mapper与Service实现

    基于MyBatis-Plus实现Mapper层直接继承BaseMapper,即可实现基本的增删改查操作,无需再编写基本的sql语句,十分清爽。而在ServiceImpl中调用也非常方便,直接调用简单的内置方法即可。

    Mapper:

    1. @Repository
    2. public interface ProductMapper extends BaseMapper<Product> {
    3. }

    ServiceImpl:

    1. @Service
    2. public class ProductServiceImpl implements ProductService {
    3. @Autowired
    4. private ProductMapper productMapper;
    5. @Override
    6. public Product getProductById(Integer id) {
    7. return productMapper.selectById(id);
    8. }
    9. @Override
    10. public List<Product> getProductList() {
    11. return productMapper.selectList(null);
    12. }
    13. @Override
    14. public Integer insertProduct(Product product) {
    15. return productMapper.insert(product);
    16. }
    17. }

    2.4.启动类上添加@MapperScan扫描

    使用这个注解之后,在Mapper层上就可以不用添加@Mapper注解了:

    1. @SpringBootApplication
    2. @EnableDiscoveryClient
    3. //主启动类上标注,在XxxMapper中可以省略@Mapper注解
    4. @MapperScan("com.fengye.springcloud.mapper")
    5. public class ProviderProductMain6700 {
    6. public static void main(String[] args) {
    7. SpringApplication.run(ProviderProductMain6700.class, args);
    8. }
    9. }

    2.5.关于数据库表与PoJo类映射

    在实际过程中,为了实现Mapper API调用的自动化CURD,在配置数据库和PoJo实体类的关系上还是有必要进行一些细节的说明,这些是坑点、也是重点!

    数据库实体类POJO:

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. @ApiModel("用户实体类")
    5. @TableName("mk_product") //用于指定表名,表名与实体名称不一致时必须使用
    6. public class Product
    7. {
    8. @ApiModelProperty("主键id")
    9. //注意:表主键使用@TableId
    10. //value表示数据库中实际列名,若实体类属性名与表主键列名一致可省略value
    11. @TableId(value = "productId", type = IdType.AUTO) //type指定自增策略
    12. private Integer id; //主键id
    13. @ApiModelProperty("商品名称")
    14. //指定实体类中属性与表中列名的对应关系
    15. @TableField(value = "pname")
    16. private String productName; //商品名称
    17. @ApiModelProperty("中奖率")
    18. //@TableField("winrate")
    19. // 如果开启了map-underscore-to-camel-case: true 自动驼峰命名,
    20. // 那么这里会将winRate修改为win_rate进行sql封装查询,有两种方式处理不对应问题:
    21. //1.设置@TableField("winrate");2.修改map-underscore-to-camel-case: false(不使用驼峰命名方式)
    22. //@TableField(value = "winrate", exist = true) //exist表明数据库表中有没有对应列存在,默认true表示存在
    23. private float winRate; //中奖率 -- 请用户输入小数点后两位
    24. }

    数据库表:

     

    对应mybatis-plus配置自动驼峰命名:

    1. #整合mybatis-plus配置
    2. mybatis-plus:
    3. #配置Mapper映射文件
    4. mapper-locations: classpath:/mybatis/mapper/*.xml
    5. # 配置Mybatis数据返回类型别名(默认别名为类名)
    6. type-aliases-package: com.fengye.springcloud.entities
    7. configuration:
    8. #自动驼峰命名
    9. map-underscore-to-camel-case: false

    上述为整合MP的主要步骤,整合完成之后即可在项目中实际使用上MyBatis-Plus啦:

     

     

     

  • 相关阅读:
    【Serverless】Unity快速集成认证服务实现邮件登录
    Rust函数进阶
    antd+ Umi使用中出现的问题集合(一)
    【算法刷题】【反转链表】给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
    机器学习(四十六):Streamlit 构建机器学习 Web
    keepalived配置高可用web服务器
    快速使用vscode写python
    2022年下半年软件设计师下午真题及答案解析
    EtherCAT主站SOEM -- 5 -- SOEM之ethercatdc.h/c文件解析
    运维学习CentOS 7进行Nightingale二进制部署
  • 原文地址:https://blog.csdn.net/m0_67698950/article/details/125558632