java 组织结构 jpa_SpringBoot整合Jpa项目(含Jpa 原生sql语句介绍)

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这三个注解都是需要的

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 bws

* @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 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

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

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

4440fd853798a08cc344c6d87b458566.png

END—

关注作者微信公众号 —《Java架构师联盟》

了解更多java后端架构知识以及最新面试宝典

b88ea680f235d26448cc79dfbf80db41.png

你点的每个好看,我都认真当成了

ce84696040aa34fd8893bbe207b855f0.png

看完本文记得给作者点赞+在看哦~~~大家的支持,是作者远远不断出文的动力。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值