tio-boot-admin 快速入门指南
tio-boot-admin
是基于 tio-boot
的管理系统中间层框架,旨在帮助开发者快速搭建项目后台管理系统。本文将通过一个入门示例,详细介绍如何集成和配置 tio-boot-admin
,从而快速启动一个简单的后台管理系统。
目录
初始化数据
见下面的章节
添加依赖
首先,在你的项目的 pom.xml
文件中添加 tio-boot-admin
的依赖:
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>tio-boot-admin</artifactId>
<version>1.0.0</version>
</dependency>
这将引入 tio-boot-admin
的核心库,确保你能够使用其提供的功能和接口。
配置文件
应用配置 app.properties
在项目的 resources
目录下创建 app.properties
文件,进行基本的应用配置:
app.env=dev
server.port=8100
server.context-path=/enote-admin
http.response.showExceptionDetails=true
tio.http.request.printUrl=true
http.multipart.max-request-size=73741824
http.multipart.max-file-size=73741824
sa.admin.token=123456
配置说明:
app.env
: 设置应用的运行环境,此处为开发环境 (dev
)。server.port
: 指定服务器启动的端口号,此处为8100
。server.context-path
: 设置应用的上下文路径,此处为/enote-admin
。http.response.showExceptionDetails
: 是否在响应中显示异常详情,方便调试。tio.http.request.printUrl
: 是否打印 HTTP 请求的 URL,便于监控请求路径。http.multipart.max-request-size
和http.multipart.max-file-size
: 配置文件上传的最大请求和文件大小限制。
环境特定配置 app-dev.properties
针对开发环境,创建 app-dev.properties
文件,配置数据库和缓存相关信息:
jdbc.url=jdbc:mysql://192.168.3.9/enote?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
jdbc.user=root
jdbc.pswd=robot_123456#
jdbc.MaximumPoolSize=2
redis.host=192.168.3.9
redis.port=6379
redis.password=123456
redis.cacheName=main
mongodb.host=192.168.3.9
mongodb.port=27017
mongodb.authSource=admin
mongodb.username=admin
mongodb.password=Litong@123
mongodb.database=penhub
配置说明:
MySQL 配置 (
jdbc.*
):jdbc.url
: MySQL 数据库连接地址。jdbc.user
和jdbc.pswd
: 数据库用户名和密码。jdbc.MaximumPoolSize
: 数据库连接池的最大连接数。
Redis 配置 (
redis.*
):redis.host
和redis.port
: Redis 服务器的地址和端口。redis.password
: Redis 服务器密码。redis.cacheName
: Redis 缓存名称。
MongoDB 配置 (
mongodb.*
):mongodb.host
和mongodb.port
: MongoDB 服务器的地址和端口。mongodb.authSource
: 认证数据库。mongodb.username
和mongodb.password
: MongoDB 用户名和密码。mongodb.database
: 默认数据库名称。
配置类
创建一个配置类 EnoteAdminAppConfig
,用于初始化 tio-boot-admin
的各项配置:
package com.enote.admin.config;
import com.litongjava.annotation.AConfiguration;
import com.litongjava.annotation.Initialization;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.tio.boot.admin.config.*;
import com.litongjava.tio.boot.admin.handler.SystemFileTencentCosHandler;
import com.litongjava.tio.boot.server.TioBootServer;
import com.litongjava.tio.http.server.router.HttpRequestRouter;
@AConfiguration
public class EnoteAdminAppConfig {
@Initialization
public void config() {
// 配置数据库相关
new TioAdminDbConfiguration().config();
new TioAdminRedisDbConfiguration().config();
new TioAdminMongoDbConfiguration().config();
new TioAdminSaTokenConfiguration().config();
new TioAdminInterceptorConfiguration().config();
new TioAdminHandlerConfiguration().config();
// 获取 HTTP 请求路由器
HttpRequestRouter r = TioBootServer.me().getRequestRouter();
if (r != null) {
// 获取文件处理器,并添加文件上传和获取 URL 的接口
SystemFileTencentCosHandler systemUploadHandler = Aop.get(SystemFileTencentCosHandler.class);
r.add("/api/system/file/upload", systemUploadHandler::upload);
r.add("/api/system/file/url", systemUploadHandler::getUrl);
}
// 配置控制器
new TioAdminControllerConfiguration().config();
}
}
代码解析:
注解说明:
@AConfiguration
: 标识这是一个配置类。@Initialization
: 标识该方法用于初始化配置。
配置步骤:
数据库配置:
TioAdminDbConfiguration
: 配置数据库连接。TioAdminRedisDbConfiguration
: 配置 Redis 缓存。TioAdminMongoDbConfiguration
: 配置 MongoDB。
安全与拦截器配置:
TioAdminSaTokenConfiguration
: 配置安全认证。TioAdminInterceptorConfiguration
: 配置请求拦截器。
处理器配置:
TioAdminHandlerConfiguration
: 配置请求处理器。
HTTP 请求路由配置:
- 获取
HttpRequestRouter
实例。 - 通过依赖注入获取
SystemFileTencentCosHandler
实例。 - 添加文件上传和获取 URL 的接口路由。
- 获取
控制器配置:
TioAdminControllerConfiguration
: 配置控制器路由。
启动类
创建主启动类 EnoteAdminApp
,用于启动应用:
package com.enote.admin;
import com.litongjava.annotation.AComponentScan;
import com.litongjava.tio.boot.TioApplication;
@AComponentScan
public class EnoteAdminApp {
public static void main(String[] args) {
long start = System.currentTimeMillis();
TioApplication.run(EnoteAdminApp.class, args);
long end = System.currentTimeMillis();
System.out.println((end - start) + "(ms)");
}
}
代码解析:
注解说明:
@AComponentScan
: 自动扫描组件,确保所有配置类和控制器被正确加载。
主方法:
- 记录启动时间,调用
TioApplication.run
启动应用。 - 打印启动耗时,帮助开发者了解启动性能。
- 记录启动时间,调用
启动日志
启动应用后,控制台会输出一系列日志,帮助开发者了解应用的初始化过程。以下是示例启动日志及其解析:
2024-10-30 10:12:55.415 [main] INFO c.l.t.u.e.EnvUtils.load:171 - app.env:dev
2024-10-30 10:12:55.429 [main] INFO c.l.j.a.s.ComponentScanner.findClasses:72 - resource:file:/E:/code/project/maliangbi/enote-admin/target/classes/com/enote/admin
2024-10-30 10:12:55.431 [main] INFO c.l.t.b.c.TioApplicationContext.run:98 - scanned classes size:2
2024-10-30 10:12:55.434 [main] INFO c.l.t.b.c.TioApplicationContext.configHttp:434 - server session enable:false
2024-10-30 10:12:55.437 [main] INFO c.l.t.b.c.TioApplicationContext.run:143 - used cache :class com.litongjava.tio.utils.cache.mapcache.ConcurrentMapCacheFactory
2024-10-30 10:12:55.459 [main] INFO c.l.t.b.c.TioApplicationContext.run:189 - sever heartbeat timeout:0
2024-10-30 10:12:55.502 [main] INFO c.l.t.b.a.c.TioAdminDbConfiguration.config:26 - jdbcUrl:jdbc:mysql://192.168.3.9/enote?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
2024-10-30 10:12:55.506 [main] INFO c.z.h.HikariDataSource.<init>:80 - HikariPool-1 - Starting...
2024-10-30 10:12:56.160 [main] INFO c.z.h.HikariDataSource.<init>:82 - HikariPool-1 - Start completed.
2024-10-30 10:12:56.191 [main] INFO c.l.t.b.a.c.TioAdminRedisDbConfiguration.config:19 - host:192.168.3.9
2024-10-30 10:12:56.267 [main] INFO c.l.t.b.a.c.TioAdminMongoDbConfiguration.config:31 - mongo:192.168.3.9:27017,penhub
2024-10-30 10:12:56.661 [main] INFO o.m.d.cluster.info:71 - Cluster created with settings {hosts=[192.168.3.9:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2024-10-30 10:12:56.661 [main] INFO o.m.d.cluster.info:71 - Adding discovered server 192.168.3.9:27017 to client view of cluster
2024-10-30 10:12:56.707 [main] INFO c.l.t.u.Threads.getTioExecutor:93 - new worker thead pool:com.litongjava.tio.utils.thread.pool.SynThreadPoolExecutor@28dcca0c[Running, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]
2024-10-30 10:12:56.717 [main] INFO c.l.t.b.c.TioApplicationContext.run:313 - http mapping
{
"/api/system/file/firebase/getUrl": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$27/1904783235@1807e3f6",
"/api/system/file/s3/md5": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$24/1574877131@480d3575",
"/api/accountSettingCurrentUser": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$20/1040776996@f1da57d",
"/api/currentUser": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$19/725680028@194fad1",
"/api/event/add": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$21/1293680734@26abb146",
"/api/geographic/province": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$29/2083999882@72c8e7b",
"/api/login/validateLogin": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$18/1158676965@65f8f5ae",
"/api/system/file/url": "com.enote.admin.config.EnoteAdminAppConfig$$Lambda$32/1984513847@431cd9b2",
"/api/sd/generateSd3": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$30/611520720@38467116",
"/api/fake_analysis_chart_data": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$22/138817329@5b7a7f33",
"/api/login/account": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$16/2107443224@790da477",
"/api/system/file/s3/url": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$25/1894601438@5c7933ad",
"/api/system/changeUserPassword": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$28/1810458830@57bc27f5",
"/api/login/outLogin": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$17/572145572@5fb759d6",
"/api/system/file/s3/upload": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$23/609656250@4b8d604b",
"/api/system/file/uploadToGoogle": "com.litongjava.tio.boot.admin.config.TioAdminHandlerConfiguration$$Lambda$26/1497377679@5e7cd6cc",
"/api/system/file/upload": "com.enote.admin.config.EnoteAdminAppConfig$$Lambda$31/809300666@68c9d179"
}
2024-10-30 10:12:56.742 [cluster-ClusterId{value='6722934815daa02914c1dbff', description='null'}-192.168.3.9:27017] INFO o.m.d.connection.info:71 - Opened connection [connectionId{localValue:1, serverValue:1}] to 192.168.3.9:27017
2024-10-30 10:12:56.745 [cluster-ClusterId{value='6722934815daa02914c1dbff', description='null'}-192.168.3.9:27017] INFO o.m.d.cluster.info:71 - Monitor thread successfully connected to server with description ServerDescription{address=192.168.3.9:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 4, 29]}, minWireVersion=0, maxWireVersion=9, electionId=null, maxDocumentSize=16777216, roundTripTimeNanos=1380800}
2024-10-30 10:12:56.746 [cluster-ClusterId{value='6722934815daa02914c1dbff', description='null'}-192.168.3.9:27017] INFO o.m.d.cluster.info:71 - Discovered cluster type of STANDALONE
2024-10-30 10:12:56.751 [main] INFO c.l.t.b.h.r.TioBootHttpControllerRouter.printMapping:446 - method mapping
{
"/api/table/export-all-table-excel": "com.litongjava.tio.boot.admin.controller.ApiTableController.exportAllTableExcel(request)",
"/api/table/index": "com.litongjava.tio.boot.admin.controller.ApiTableController.index()",
"/api/table/names": "com.litongjava.tio.boot.admin.controller.ApiTableController.tableNames()",
"/api/table/{f}/batchUpdate": "com.litongjava.tio.boot.admin.controller.ApiTableController.batchUpdate(f,request)",
"/api/table/{f}/columns": "com.litongjava.tio.boot.admin.controller.ApiTableController.proTableColumns(f)",
"/api/table/{f}/config": "com.litongjava.tio.boot.admin.controller.ApiTableController.fConfig(f,lang)",
"/api/table/{f}/create": "com.litongjava.tio.boot.admin.controller.ApiTableController.create(f,request)",
"/api/table/{f}/delete/{id}": "com.litongjava.tio.boot.admin.controller.ApiTableController.delete(f,id)",
"/api/table/{f}/export-excel": "com.litongjava.tio.boot.admin.controller.ApiTableController.exportExcel(f,request)",
"/api/table/{f}/export-table-excel": "com.litongjava.tio.boot.admin.controller.ApiTableController.exportAllExcel(f,request)",
"/api/table/{f}/get": "com.litongjava.tio.boot.admin.controller.ApiTableController.get(f,request)",
"/api/table/{f}/list": "com.litongjava.tio.boot.admin.controller.ApiTableController.list(f,request)",
"/api/table/{f}/listAll": "com.litongjava.tio.boot.admin.controller.ApiTableController.listAll(f)",
"/api/table/{f}/page": "com.litongjava.tio.boot.admin.controller.ApiTableController.page(f,request)",
"/api/table/{f}/pageDeleted": "com.litongjava.tio.boot.admin.controller.ApiTableController.pageDeleted(f,request)",
"/api/table/{f}/recover": "com.litongjava.tio.boot.admin.controller.ApiTableController.recover(f,id)",
"/api/table/{f}/remove/{id}": "com.litongjava.tio.boot.admin.controller.ApiTableController.remove(f,id)",
"/api/table/{f}/total": "com.litongjava.tio.boot.admin.controller.ApiTableController.total(f)",
"/api/table/{f}/update": "com.litongjava.tio.boot.admin.controller.ApiTableController.update(f,request)",
"/mongodb/json/{f}/create": "com.litongjava.tio.boot.admin.controller.MongodbController.create(f,request)",
"/mongodb/json/{f}/delete/{id}": "com.litongjava.tio.boot.admin.controller.MongodbController.delete(f,id)",
"/mongodb/json/{f}/export-excel": "com.litongjava.tio.boot.admin.controller.MongodbController.exportExcel(f,request)",
"/mongodb/json/{f}/export-table-excel": "com.litongjava.tio.boot.admin.controller.MongodbController.exportAllExcel(f,request)",
"/mongodb/json/{f}/page": "com.litongjava.tio.boot.admin.controller.MongodbController.page(f,request)"
}
2024-10-30 10:12:56.752 [main] INFO c.l.t.b.h.r.TioBootHttpControllerRouter.printMapping:450 - variable path mapping
{
"/api/table/{f}/batchUpdate": "com.litongjava.tio.boot.admin.controller.ApiTableController.batchUpdate(f,request)",
"/api/table/{f}/columns": "com.litongjava.tio.boot.admin.controller.ApiTableController.proTableColumns(f)",
"/api/table/{f}/config": "com.litongjava.tio.boot.admin.controller.ApiTableController.fConfig(f,lang)",
"/api/table/{f}/create": "com.litongjava.tio.boot.admin.controller.ApiTableController.create(f,request)",
"/api/table/{f}/delete/{id}": "com.litongjava.tio.boot.admin.controller.ApiTableController.delete(f,id)",
"/api/table/{f}/export-excel": "com.litongjava.tio.boot.admin.controller.ApiTableController.exportExcel(f,request)",
"/api/table/{f}/export-table-excel": "com.litongjava.tio.boot.admin.controller.ApiTableController.exportAllExcel(f,request)",
"/api/table/{f}/get": "com.litongjava.tio.boot.admin.controller.ApiTableController.get(f,request)",
"/api/table/{f}/list": "com.litongjava.tio.boot.admin.controller.ApiTableController.list(f,request)",
"/api/table/{f}/listAll": "com.litongjava.tio.boot.admin.controller.ApiTableController.listAll(f)",
"/api/table/{f}/page": "com.litongjava.tio.boot.admin.controller.ApiTableController.page(f,request)",
"/api/table/{f}/pageDeleted": "com.litongjava.tio.boot.admin.controller.ApiTableController.pageDeleted(f,request)",
"/api/table/{f}/recover": "com.litongjava.tio.boot.admin.controller.ApiTableController.recover(f,id)",
"/api/table/{f}/remove/{id}": "com.litongjava.tio.boot.admin.controller.ApiTableController.remove(f,id)",
"/api/table/{f}/total": "com.litongjava.tio.boot.admin.controller.ApiTableController.total(f)",
"/api/table/{f}/update": "com.litongjava.tio.boot.admin.controller.ApiTableController.update(f,request)",
"/mongodb/json/{f}/create": "com.litongjava.tio.boot.admin.controller.MongodbController.create(f,request)",
"/mongodb/json/{f}/delete/{id}": "com.litongjava.tio.boot.admin.controller.MongodbController.delete(f,id)",
"/mongodb/json/{f}/export-excel": "com.litongjava.tio.boot.admin.controller.MongodbController.exportExcel(f,request)",
"/mongodb/json/{f}/export-table-excel": "com.litongjava.tio.boot.admin.controller.MongodbController.exportAllExcel(f,request)",
"/mongodb/json/{f}/page": "com.litongjava.tio.boot.admin.controller.MongodbController.page(f,request)"
}
2024-10-30 10:12:56.752 [main] INFO c.l.t.b.c.TioApplicationContext.run:333 - total:1368(ms), scan class:10(ms), init:29(ms), config:1242(ms), server:12(ms), http route:36(ms)
2024-10-30 10:12:56.752 [main] INFO c.l.t.b.c.TioApplicationContext.printUrl:355 - port:8100
http://localhost:8100/enote-admin
1558(ms)
结语
tio-boot-admin
作为一个高效的后台管理框架,结合 tio-boot
的强大功能,能够帮助开发者快速搭建稳定、可扩展的管理系统。通过本文的入门示例,相信你已经掌握了基本的集成和配置方法,接下来可以根据项目需求进行深入开发和定制。
如果在使用过程中遇到问题,欢迎参考 tio-boot-admin 官方文档 或在社区中寻求帮助。祝你开发顺利!