温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatisplus与JPA混合使用的方法是什么

发布时间:2023-03-30 15:35:11 来源:亿速云 阅读:175 作者:iii 栏目:开发技术

本文小编为大家详细介绍“mybatisplus与JPA混合使用的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“mybatisplus与JPA混合使用的方法是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

实践过程

一、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、启动

发现表已经自动建立好。

mybatisplus与JPA混合使用的方法是什么

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);
	}
	
}

mybatisplus与JPA混合使用的方法是什么

mybatisplus与JPA混合使用的方法是什么

读到这里,这篇“mybatisplus与JPA混合使用的方法是什么”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI