Spring Data JPA配置使用联合主键demo

我们在Spring Boot下使用Spring Data JPA,在项目的Maven依赖里添加spring-boot-stater-data-jpa,然后只需定义DataSource、实体类和数据访问层,并在需要使用数据访问的地方注入数据访问层的Bean即可,无须任何额外配置。
pom.xml引入依赖:

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            mysql
            mysql-connector-java
        

application.properties配置:

# 配置连接池,默认使用的是tomcat的连接池,但实际很少用tomcat的连接池
spring.datasource.url=jdbc:mysql://localhost:3306/NewData?useUnicode=true&characterEncoding=UTF8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置方言 否则提示:Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
# 自动更新数据库表结构,也可以是 validate | update | create | create-drop
spring.jpa.hibernate.hbm2ddl.auto=update
# 显示sql语句
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jackson.serialization.indent-output=true

定义实体映射类:

package com.example.demo.domain;

import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;

@Entity
@Data
@Table(name = "data") //映射实体表名
@IdClass(dataMultiKeysClass.class) //定义的联合主键
public class dataBean implements Serializable {
    private String id;

    @Id
    private String sbj;
    @Id
    private String prd;
    @Id
    private String obj;

    private Integer is_delete;
    private String file_id;
    private String pos;

    public dataBean() {
        super();
    }

    public dataBean(String sbj, String prd, String obj) {
        super();
        this.sbj = sbj;
        this.prd = prd;
        this.obj = obj;
    }
}

定义l联合主键类:

package com.example.demo.domain;

import lombok.Data;
import java.io.Serializable;

@Data
public class dataMultiKeysClass implements Serializable {
    private String sbj;
    private String prd;
    private String obj;

    public dataMultiKeysClass() {}

    public dataMultiKeysClass(String sbj, String prd, String obj) {
        this.sbj = sbj;
        this.prd = prd;
        this.obj = obj;
    }

}

定义数据访问接口:

package com.example.demo.dao;

import com.example.demo.domain.Mysqldata;
import com.example.demo.domain.dataMultiKeysClass;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;

public interface MysqlRepository extends JpaRepository {
    @Modifying
    @Transactional
    @Query("update Mysqldata d set d.is_delete=1 where d.sbj=?1 and d.prd=?2 and d.obj=?3")
    void delete(String sbj, String prd, String obj);

    @Modifying
    @Transactional
    @Query("update Mysqldata d set d.is_delete=1 where d.sbj=?1 or d.obj=?1")
    void updateIs_deleteByKey(String uri);

    @Modifying
    @Transactional
    @Query("update Mysqldata d set d.sbj=?4,d.prd=?5,d.obj=?6 where d.sbj=?1 and d.prd=?2 and d.obj=?3")
    void update_relation(String sbj, String prd, String obj, String Sbj, String Prd, String Obj);
}

数据dao层:

package com.example.demo.dao;

public interface DataDAO {
    void add(String sbj, String prd, String obj);

    void remove(String sbj, String prd, String obj);

    void delete(String sbj, String prd, String obj);

    void updateIs_deleteByKey(String uri);

    void update_relation(String sbj, String prd, String obj, String Sbj, String Prd, String Obj);
}

实现操作接口:

package com.example.demo.dao;

import com.example.demo.domain.Mysqldata;
import com.example.demo.domain.dataMultiKeysClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MysqlDataDAO implements DataDAO {
    @Autowired
    MysqlRepository mysqlRepository;
    @Override
    public void delete(String sbj, String prd, String obj) {
        mysqlRepository.delete(sbj, prd, obj);
    }

    @Override
    public void updateIs_deleteByKey(String uri) {
        mysqlRepository.updateIs_deleteByKey(uri);
    }

    @Override
    public void remove(String sbj, String prd, String obj) {
        mysqlRepository.deleteById(new dataMultiKeysClass(sbj, prd, obj));
    }

    @Override
    public void add(String sbj, String prd, String obj) {
        mysqlRepository.save(new Mysqldata(sbj, prd, obj));
    }

    @Override
    public void update_relation(String sbj, String prd, String obj, String Sbj, String Prd, String Obj) {
        mysqlRepository.update_relation(sbj, prd, obj, Sbj, Prd, Obj);
    }
}

运行service层:

package com.example.demo.service;

import com.example.demo.POJO.Jsondata;
import com.example.demo.POJO.UriJson;

public interface DataService {
    //增加一条数据
    void add(Jsondata jsondata);

    //删除一条数据,is_delete设置为1
    void delete(Jsondata jsondata);

    //修改关系,删除old,增加novel
    void update_relation(Jsondata oldData, Jsondata newData);

    //删除节点,将sbj和obj等于uri的删除
    void remove_node(UriJson uriJson);

    //删除一条数据,从库里删除
    void remove(Jsondata jsondata);
}

service接口实现:

package com.example.demo.service.impl;

import com.example.demo.POJO.UriJson;
import com.example.demo.dao.DataDAO;
import com.example.demo.service.DataService;
import com.example.demo.POJO.Jsondata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataServiceImpl implements DataService {
    @Autowired
    private DataDAO dataDAO;

    @Override
    public void update_relation(Jsondata oldData, Jsondata newData) {
        dataDAO.update_relation(oldData.getSbj(), oldData.getPrd(), oldData.getObj(), newData.getSbj(), newData.getPrd(), newData.getObj());
    }

    @Override
    public void remove_node(UriJson uriJson) {
        dataDAO.updateIs_deleteByKey(uriJson.getUri());
    }

    @Override
    public void add(Jsondata jsondata) {
        dataDAO.add(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
    }

    @Override
    public void delete(Jsondata jsondata) {
        dataDAO.delete(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
    }

    @Override
    public void remove(Jsondata jsondata) {
        dataDAO.remove(jsondata.getSbj(), jsondata.getPrd(), jsondata.getObj());
    }
}

你可能感兴趣的:(mysql学习,Java)