SpringBoot整合Jpa項目(含Jpa 原生sql語句介紹)

一、插入語句java

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

二、更新語句web

@Transactional
@Query(value = "update number_count set count = ?1", nativeQuery = true)
@Modifying
public void updateCount(int count);

三、查詢語句面試

@Transactional
@Query(value = "select count from number_count")
@Modifying
Count selectCount();

四、刪除語句spring

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

注意:不加@Transactional有可能會拋javax.persistence.TransactionRequiredException:異常,我是在拋異常以後才加上的。sql

@Transactional、 @Query、@Modifying這三個註解都是須要的數據庫

nativeQuery = true表明使用原生sqlapache


下面貼一下完整代碼:後端

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 bws
* @date 2019/8/30
**/
@Component
public interface CountMapper extends JpaRepository<Count, Integer> {
  /*
    * 咱們在這裏直接繼承 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 bws
* @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 bws
* @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架構

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.7.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>

  <properties>
      <java.version>1.8</java.version>
  </properties>

  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
      </dependency>
      <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <scope>runtime</scope>
      </dependency>
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>

  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

</project>

啓動項目以後,訪問地址:

http://localhost:8080/list

END


關注做者微信公衆號 —《Java架構師聯盟》


瞭解更多java後端架構知識以及最新面試寶典



你點的每一個好看,我都認真當成了


看完本文記得給做者點贊+在看哦~~~你們的支持,是做者遠遠不斷出文的動力。


本文分享自微信公衆號 - Java架構師聯盟(msbxq2019)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索