前言
我业余项目使用SpringBoot+Kotlin+Mybatis-Plus+Sqlite搭建的,Sqlite也支持JSON字段,下面就配置试下。
- Sqlite自3.9之后的版本就支持Json字段了
项目代码地址: https://github.com/blanexie/vxpt
数据对象RoleDO
- 需要在对象上加上 @TableField(typeHandler = FastjsonTypeHandler::class) 注解
- json的转换使用的fastjson自带的转换类,好用
- 之后就可以正常使用了
- 本来我还准备使用 @TableField(fill = FieldFill.INSERT_UPDATE) 注解来设置 status ,createTime和updateTime字段的默认值,但是Kotlin的非空变量定义加默认值,就已经可以实现这个功能了,没有必要。 kotlin yyds
数据对象代码:
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);
全局异常拦截
拦截处理类
- 此处我把未自定义的异常的message直接返回客户端了,这不好
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 ?: "")
}
}
自定义异常类
- 还是摆脱不能java的影响,继承了java的RuntimeException类
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,"服务器错误")
}
总结
- 整个用起来和java类型,
- 但是kotlin的代码用起来真的YYDS