【分布式】-- 微服務抽獎系統后台整合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依賴

 <!--mybatis-plus插件-->
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.3.2</version>
 </dependency>

 

2.2.對應的application.yml配置

#整合mybatis-plus配置
mybatis-plus:
  #配置Mapper映射文件
  mapper-locations: classpath:/mybatis/mapper/*.xml
  # 配置Mybatis數據返回類型別名(默認別名為類名)
  type-aliases-package: com.fengye.springcloud.entities
  configuration:
    #自動駝峰命名
    map-underscore-to-camel-case: false

#配置控制台打印日志Debug
logging:
  level:
    com.jd.mapper: debug

 

2.3.修改Mapper與Service實現

基於MyBatis-Plus實現Mapper層直接繼承BaseMapper,即可實現基本的增刪改查操作,無需再編寫基本的sql語句,十分清爽。而在ServiceImpl中調用也非常方便,直接調用簡單的內置方法即可。

Mapper:

@Repository
public interface ProductMapper extends BaseMapper<Product> {
}

ServiceImpl:

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product getProductById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<Product> getProductList() {
        return productMapper.selectList(null);
    }

    @Override
    public Integer insertProduct(Product product) {
        return productMapper.insert(product);
    }
}

 

2.4.啟動類上添加@MapperScan掃描

使用這個注解之后,在Mapper層上就可以不用添加@Mapper注解了:

@SpringBootApplication
@EnableDiscoveryClient
//主啟動類上標注,在XxxMapper中可以省略@Mapper注解
@MapperScan("com.fengye.springcloud.mapper")
public class ProviderProductMain6700 {
    public static void main(String[] args) {
        SpringApplication.run(ProviderProductMain6700.class, args);
    }
}

 

2.5.關於數據庫表與PoJo類映射

在實際過程中,為了實現Mapper API調用的自動化CURD,在配置數據庫和PoJo實體類的關系上還是有必要進行一些細節的說明,這些是坑點、也是重點!

數據庫實體類POJO:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用戶實體類")
@TableName("mk_product")  //用於指定表名,表名與實體名稱不一致時必須使用
public class Product
{
    @ApiModelProperty("主鍵id")
    //注意:表主鍵使用@TableId
    //value表示數據庫中實際列名,若實體類屬性名與表主鍵列名一致可省略value
    @TableId(value = "productId", type = IdType.AUTO)  //type指定自增策略
    private Integer id;   //主鍵id
    @ApiModelProperty("商品名稱")
    //指定實體類中屬性與表中列名的對應關系
    @TableField(value = "pname")  
    private String productName;  //商品名稱
    @ApiModelProperty("中獎率")
    //@TableField("winrate")
    // 如果開啟了map-underscore-to-camel-case: true 自動駝峰命名,
    // 那么這里會將winRate修改為win_rate進行sql封裝查詢,有兩種方式處理不對應問題:
    //1.設置@TableField("winrate");2.修改map-underscore-to-camel-case: false(不使用駝峰命名方式)
    //@TableField(value = "winrate", exist = true) //exist表明數據庫表中有沒有對應列存在,默認true表示存在
    private float winRate;  //中獎率 -- 請用戶輸入小數點后兩位
}

數據庫表:

 

對應mybatis-plus配置自動駝峰命名:

#整合mybatis-plus配置
mybatis-plus:
  #配置Mapper映射文件
  mapper-locations: classpath:/mybatis/mapper/*.xml
  # 配置Mybatis數據返回類型別名(默認別名為類名)
  type-aliases-package: com.fengye.springcloud.entities
  configuration:
    #自動駝峰命名
    map-underscore-to-camel-case: false

 

上述為整合MP的主要步驟,整合完成之后即可在項目中實際使用上MyBatis-Plus啦:

 

參考博文:

Mybatis-Plus的使用 ------ 入門

 

博客示例及相關代碼已上傳至GitHub:

LotteryDraw


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM