我业余项目使用SpringBoot+Kotlin+Mybatis-Plus+Sqlite搭建的,Sqlite也支持JSON字段,下面就配置试下。
项目代码地址: https://github.com/blanexie/vxpt
数据对象代码:
package com.github.blanexie.vxpt.bbs.user.meta.entity
import com.baomidou.mybatisplus.annotation.*
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler
import java.time.LocalDateTime
@TableName("role")
class RoleDO(
@TableId(type = IdType.AUTO)
var id: Long?,
var code: String,
var name: String,
@TableField(typeHandler = FastjsonTypeHandler::class)
var permissions: List<String>,
@TableField(typeHandler = FastjsonTypeHandler::class)
var roles: List<String>,
var status: Int = 0,
var createTime: LocalDateTime = LocalDateTime.now(),
var updateTime: LocalDateTime = LocalDateTime.now(),
)
表定义:
create table role
(
id INTEGER not null
constraint role_pk
primary key autoincrement,
name TEXT not null,
code TEXT not null,
permissions json not null,
roles json not null,
status INTEGER default 0 not null,
create_time numeric not null,
update_time numeric not null
);
create unique index role_code_uindex
on role (code);
package com.github.blanexie.vxpt.bbs.util.spring
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.ResponseBody
import javax.servlet.http.HttpServletRequest
@ControllerAdvice
@ResponseBody
class GlobalExceptionHandler {
val logger: Logger = LoggerFactory.getLogger(GlobalExceptionHandler::class.java)
@ExceptionHandler(value = [Exception::class])
fun exceptionHandler(request: HttpServletRequest, e: Exception): WebResp {
if (e is VxptException) {
logger.error("全局异常处理器拦截了", e)
return WebResp.fail(e.respCode.code, e.respCode.message)
}
logger.error("全局异常处理器拦截了", e)
return WebResp.fail(RespCode.SERVER_ERROR.code, e.message ?: "")
}
}
package com.github.blanexie.vxpt.bbs.util.spring
import java.lang.RuntimeException
class VxptException(val respCode: RespCode) : RuntimeException(respCode.message) {
}
package com.github.blanexie.vxpt.bbs.util.spring
class WebResp(val code: Int, val message: String, val data: Any?) {
companion object {
fun success(data: Any): WebResp {
return WebResp(200, "", data)
}
fun success(): WebResp {
return WebResp(200, "", null)
}
fun fail(code: Int, message: String): WebResp {
return WebResp(code, message, null)
}
}
}
package com.github.blanexie.vxpt.bbs.util.spring
enum class RespCode(val code: Int, var message: String) {
Not_Found(404, "NotFound"),
SERVER_ERROR(500,"服务器错误")
}