SpringBoot統合Jpaプロジェクト(Jpa原生sql文の紹介を含む)

5892 ワード

1、挿入文
@Transactional 
@Query(value = "insert into number_rule values(?1,?2)", nativeQuery = true)
@Modifying 
int insertRule(int nums,int rule);

2、文の更新
@Transactional
@Query(value = "update number_count set count = ?1", nativeQuery = true)
@Modifying
public void updateCount(int count);

3、クエリ文
@Transactional
@Query(value = "select count from number_count")
@Modifying
Count selectCount();

4、削除文
@Transactional
@Modifying
@Query(value = "delete from number_count  where count =?1",nativeQuery = true)
int deleteCount(int count);

注意:@Transactionalを加えないとjavax.persistence.TransactionRequiredException:が異常になる可能性があります.私は異常を投げた後に追加しました.@Transactional、 @Query、@Modifyingこの3つの注釈はすべて必要ですnativeQuery = trueは、原生sqlの使用を表す
完全なコードを貼り付けます.
package com.example.demo.mapper;

import com.example.demo.entity.Count;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 *          
 *
 * @author zhaohualuo
 * @date 2019/8/30
 **/
@Component
public interface CountMapper extends JpaRepository {
    /*
     *           JpaRepository
     *               
     *    JPA     
     *
     * */
    @Transactional
    @Query(value = "update number_count set count = ?1", nativeQuery = true)
    @Modifying
    public void updateCount(int count);
}
package com.example.demo.controller;

import com.example.demo.entity.Count;
import com.example.demo.mapper.CountMapper;
import com.example.demo.service.DisorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

/**
 *        
 *
 * @author zhaohualuo
 * @date 2019/8/30
 **/
@RestController
public class DisorderController {

    @Autowired CountMapper countMapper;

    @GetMapping("/list")
    public int findAll() {
        return countMapper.updateCount(1);
    }
}
package com.example.demo.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *       
 *
 * @author zhaohualuo
 * @date 2019/8/30
 **/
@Data
@Entity
@Table(name = "number_count")
public class Count {

    @Id
    private int count;
}

application.yml
spring:
  devtools:
    restart:
      enabled: false
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/xxx
    username: xxx
    password: xxx
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    properties:
      hibernate:
        temp:
          use_jdbc_metadata_defaults: false
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.7.RELEASE
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot

    
        1.8
    

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

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.postgresql
            postgresql
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



プロジェクトを開始した後、アクセス先:
http://localhost:8080/list