mybatis-plus與JPA混合的使用方式
更新時間:2023年03月30日 14:29:37 作者:K歌、之王
這篇文章主要介紹了mybatis-plus與JPA混合的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
前言
感覺Jpa的動態(tài)構(gòu)建查詢不好使用,然后mybatis-plus沒有動態(tài)構(gòu)建表的功能,有沒有可能使兩者混合使用,利用Jpa自動建表的優(yōu)勢 與 mybatis-plus lambda查詢的優(yōu)勢 結(jié)合一下呢?
實踐過程
一、pom配置
<dependencies> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies>
二、配置
package com.naruto.configuration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
@Configuration
@MapperScan({"com.naruto.**.mapper*"})
public class MybatiesPlusConfig {
/**
* 開啟mybatis-plus分頁功能
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}application.yml配置
spring:
jpa:
database-platform: org.hibernate.dialect.MySQL5Dialect
show-sql: true
properties:
hibernate:
hbm2ddl:
auto: update
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false&nullCatalogMeansCurrent=true
username: root
password: 123456
mybatis-plus:
mapper-locations: classpath*:com/naruto/**/xml/*Mapper.xml
global-config:
# 關(guān)閉MP3.0自帶的banner
banner: false
三、實體類
此處
Table TableName @Id @GeneratedValue(strategy=GenerationType.AUTO) @TableId(type = IdType.ID_WORKER_STR) 不可忽略
@Table(name="platform_table")
@TableName("platform_table")
@Entity
public class PlatformTableModel implements Serializable{
/**
*
*/
private static final long serialVersionUID = 4977394314428963032L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@TableId(type = IdType.ID_WORKER_STR)
private String id;
private String tableName;
private String tableVersion;
private String tableDescrition;
private String createBy;
private String createTime;
private String updateBy;
private String updateTime;
....
}
四、配置好mapper和Service
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.naruto.mapper.PlatformTableMapper"> </mapper>
package com.naruto.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.naruto.entity.PlatformTableModel;
public interface PlatformTableMapper extends BaseMapper<PlatformTableModel>{
}package com.naruto.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.naruto.entity.PlatformTableModel;
public interface IPlatformTableService extends IService<PlatformTableModel>{
}package com.naruto.service.impl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naruto.entity.PlatformTableModel;
import com.naruto.mapper.PlatformTableMapper;
import com.naruto.service.IPlatformTableService;
@Service
public class PlatformTableServiceImpl extends ServiceImpl<PlatformTableMapper, PlatformTableModel> implements IPlatformTableService{
}測試
1、啟動
發(fā)現(xiàn)表已經(jīng)自動建立好。

2、 測試插入 與 查詢, 沒有問題。
@RestController
@RequestMapping("table")
public class PlatformTableAction {
@Autowired
private IPlatformTableService platformTableService;
@GetMapping("get")
public List<PlatformTableModel> get() {
LambdaQueryWrapper<PlatformTableModel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(PlatformTableModel::getId, "1461159441186361345");
List<PlatformTableModel> platformTableModels = platformTableService.list(lambdaQueryWrapper);
return platformTableModels;
}
@PostMapping("save")
public Result save(@RequestBody PlatformTableModel platformTableModel) {
platformTableService.save(platformTableModel);
return new Result(platformTableModel);
}
}


結(jié)論
可以結(jié)合使用以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Idea快速搭建SpringMVC項目的詳細(xì)步驟記錄
這篇文章主要給大家介紹了關(guān)于使用Idea快速搭建SpringMVC項目的詳細(xì)步驟,Spring?MVC是一種基于MVC模式的框架,它是Spring框架的一部分,它提供了一種更簡單和更有效的方式來構(gòu)建Web應(yīng)用程序,需要的朋友可以參考下2024-05-05

