springboot整合jpa

在配置springboot和jpa整合,遇到很多错误,这里说明指出

1.数据源配置(根据个人情况配置)

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/teams
    username: postgres
    password: 123456
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

2.依赖的引用(本文采用gradle形式)

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.apache.poi:poi-ooxml:3.17'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    implementation group: 'org.projectlombok', name: 'lombok', version: '1.18.8'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '2.2.0.RELEASE'
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
}

3.配置类

本文设置一个复合主键的设计。PS:注意构造函数的引用

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;

/**
 * @author : Jenson.Liu
 * @date : 2019/11/29  11:26 上午
 */
@Embeddable
public class ContentUserKey implements Serializable {

    /**
     * 组合主键
     */
    String userId;

    String bbName;

    @Column(nullable = false)
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Column(nullable = false)
    public String getBbName() {
        return bbName;
    }

    public void setBbName(String bbName) {
        this.bbName = bbName;
    }

    public ContentUserKey(String userId, String bbName) {
        this.userId = userId;
        this.bbName = bbName;
    }

    public ContentUserKey() {
    }
}
import javax.persistence.*;
import java.io.Serializable;

/**
 * @author : Jenson.Liu
 * @date : 2019/11/29  10:07 上午
 */
@Entity
public class ContentUser implements Serializable {

    ContentUserKey id;

    String component;

    @EmbeddedId
    public ContentUserKey getId() {
        return id;
    }

    public void setId(ContentUserKey contentUserKey) {
        this.id = contentUserKey;
    }

    @Column(nullable = true)
    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public ContentUser(String userId, String bbName, String component) {
        this.id = new ContentUserKey(userId,bbName);
        this.component = component;
    }

    public ContentUser(ContentUserKey id) {
        this.id = id;
    }

    public ContentUser() {
    }
}

配置jpa类

import com.sap.verifys4andx4.pojo.ContentUser;
import com.sap.verifys4andx4.pojo.ContentUserKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

/**
 * @author : Jenson.Liu
 * @date : 2019/11/29  1:14 下午
 */
@Service
public interface ContentUserRepository extends JpaRepository<ContentUser, ContentUserKey>{

    /**
     * 通过userId查询
     * @param userId
     * @return
     */
    List<ContentUser>  getAllById_UserIdIs(String userId);

    /**
     * 通过bbName查询
     * @param bbName
     * @return
     */
    ContentUser getContentUsersByIdBbName(String bbName);

    /**
     * 通过ContentType查询
     * @param contentType
     * @return
     */
    List<ContentUser> getAllByContentType(String contentType);

    /**
     * 查询contentTeam目前在用的SI
     * @return
     */
    @Query(value = "from ContentUser c where c.contentType='SI' and c.id.bbName not in (select c1.id.bbName from ContentUser c1 where c1.contentType = 'SI' and c1.emc='-' and c1.op = '-')")
    List<ContentUser> getContentUsersBySI();
}

最后就可以愉快的使用jpa接口提供的命令类

       ContentUser contentUser = new ContentUser("i501695","1zq","test");
        System.out.println(contentUser.toString());
        contentUserRepository.save(contentUser);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值