mybatis-plus与JPA混合的使用方式
作者:K歌、之王 时间:2022-06-24 02:10:25
前言
感觉Jpa的动态构建查询不好使用,然后mybatis-plus没有动态构建表的功能,有没有可能使两者混合使用,利用Jpa自动建表的优势 与 mybatis-plus lambda查询的优势 结合一下呢?
实践过程
一、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:
# 关闭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、启动
发现表已经自动建立好。
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);
}
}
结论
可以结合使用以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
来源:https://kimguo.blog.csdn.net/article/details/121395598
标签:mybatis-plus,JPA,混合
0
投稿
猜你喜欢
Android系统永不锁屏永不休眠的方法
2023-03-11 00:06:21
Java枚举类使用Lombok方式
2022-06-28 22:07:02
android图像绘制(一)多种方法做图像镜像
2023-08-23 01:34:18
为何找不到Java 7中的警告
2022-09-08 23:04:18
Java并发编程之ReentrantLock可重入锁的实例代码
2021-12-10 06:05:50
解决@PathVariable对于特殊字符截断的问题
2021-10-10 08:19:40
解决unity rotate旋转物体 限制物体旋转角度的大坑
2022-01-14 01:41:49
Sharding-Proxy基本功能用法介绍
2021-10-17 06:41:03
C++ boost::asio编程-异步TCP详解及实例代码
2021-12-28 03:36:52
Android Jetpack架构组件 ViewModel详解
2021-09-08 00:29:20
java批量修改文件后缀名方法总结
2022-03-15 15:46:04
java多线程和并发包入门示例
2022-05-10 12:29:34
详解Maven私 服Nexus的安装与使用
2023-11-24 12:34:02
android编程之ip2id程序实例
2023-07-20 16:08:09
C#中var关键字用法分析
2022-02-11 15:01:43
Android系统制作自定义签名的例子
2023-02-02 05:10:04
WebService教程详解(一)
2022-02-26 09:59:50
ListView实现下拉动态渲染数据
2022-10-31 11:19:12
Java浅析枚举类的使用
2023-11-10 14:12:58
Java实现控制台输出两点间距离
2023-05-18 11:49:11