验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

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

阅读:297 来源:乙速云 作者:代码code

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

实践过程

一、pom配置


		
			com.baomidou
			mybatis-plus-boot-starter
			3.1.2
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
	

二、配置

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





package com.naruto.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.naruto.entity.PlatformTableModel;

public interface PlatformTableMapper extends BaseMapper{

}
package com.naruto.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.naruto.entity.PlatformTableModel;

public interface IPlatformTableService extends IService{

}
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 implements IPlatformTableService{

}

测试

1、启动

发现表已经自动建立好。

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

2、 测试插入 与 查询, 没有问题。

@RestController
@RequestMapping("table")
public class PlatformTableAction {
	
	@Autowired
	private IPlatformTableService platformTableService;
	
	@GetMapping("get")
	public List get() {
		LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(PlatformTableModel::getId, "1461159441186361345");
		List 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混合使用的方法是什么

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>