Spring-boot配置JPA与MyBatisPlus

一、在pom.xml中配置依赖

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>

二、在application.yml文件中添加配置

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/test?serverTimezone=GMT%2B8&characterEncoding=utf-8
    username: root
    password: root
  profiles:
    active: dev
  jpa:
    show-sql: false
    #  自动建表
    hibernate:
      ddl-auto: update

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  configuration:
#    输出sql语句
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

这里需要注意jpa配置是在spring配置下面,我犯了这个错误浪费了两小时。这里结合使用jpa和mybatis-plus目的是结合使用jpa的自动建表和mybatis-plus的数据库查询。

三、为Model类添加注解

JPA也可以添加联合主键,需要定义一个实现Serializable接口的联合主键类。

package com.example.jiakao.pojo.entity.idClass;

import java.io.Serializable;

public class UserRoleId implements Serializable {
    private Integer userId;
    private Short roleId;
}

然后在entity中增加@IdClass和@Id注解。

package com.example.jiakao.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.example.jiakao.pojo.entity.idClass.UserRoleId;
import lombok.Data;

import javax.persistence.*;

@Entity
@TableName("user_role")
@Table(name="user_role",uniqueConstraints = {@UniqueConstraint(columnNames = {"userId","roleId"})})
@IdClass(UserRoleId.class)
@Data
public class UserRolePo {
    @Id
    @Column( columnDefinition = "int4 not null" )
    private Integer userId;
    @Id
    @Column( columnDefinition = "smallint not null" )
    private Short roleId;
}

注意:

@Entityh和@Id的是从javax.persistence导入。到这里已经可以利用jpa实现自动建表。

@Table的uniqueConstraints属性给表增加唯一约束。

@Column的columnDefinition定义字段数据格式,还可以增加默认值,在后面加上default value即可。unique属性可以将字段设置为唯一键。

四、添加Mapper和Service类

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xia.xiatest.entity.Files;

public interface FilesMapper extends BaseMapper<Files> {
}
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xia.xiatest.entity.Files;
import com.xia.xiatest.mapper.FilesMapper;
import org.springframework.stereotype.Service;

@Service
public class UserService extends ServiceImpl<FilesMapper,Files> {
}

现在就可以用mybatis-plus实现简单的数据库操作啦。

五、Mybatis-plus更新策略

  1. IGNORE:忽略判断。不管是否设置属性,所有字段都会设置到insert语句中,如果没有设置值,则更新为null。

  1. NOT_NULL:默认策略,非null判断。忽略null字段,不忽略空字符串。

  1. NOT_EMPTY:非空判断。如果设置值为null或者空字符串,不会插入数据库。

如果我们需要对FieldStrategy策略进行调整,有以下方式

  1. 调整全局策略

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  type-handlers-package: com.example.jiakao.typeHandle
  global-config:
    db-config:
      #  主键自增
      id-type: auto
      insert-strategy: ignored
      update-strategy: ignored
  1. 调整字段验证策略

@TableField(insertStrategy = FieldStrategy.IGNORED, updateStrategy = FieldStrategy.IGNORED)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值