栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Kotlin & Spring Boot & JPA 开发模版

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Kotlin & Spring Boot & JPA 开发模版

完整源码 https://github.com/tothis/spring-boot-template

全局异常处理(@RestControllerAdvice)JPA 逻辑删除(@Where 和 重写 deleteById 函数)声明式事务

基础类

package com.example.template.service

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.support.TransactionCallback
import org.springframework.transaction.support.TransactionTemplate

abstract class baseService {
    @Autowired
    private lateinit var transactionTemplate: TransactionTemplate

    protected fun  transaction(action: TransactionCallback) = transactionTemplate.execute(action)
}
实现类
package com.example.template.service

@Service
class TaskService(
    private val repository1: Repository1,
    private val repository2: Repository2,
    private val repository3: Repository3
) : baseService() {

    fun deleteByEntity(entity: Task) = with(entity) {
    	transaction {
	        id1?.let { repository1.deleteById(it) }
	        id2?.let { repository2.deleteById(it) }
	        id3?.let { repository3.deleteById(it) }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/750254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号