使用 ApiTable 连接 Mysql
11.2.1.创建表,插入数据
创建一张简单的 student 表
CREATE TABLE `student` (
`id` bigint(20) NOT NULL,
`name` varchar(255),
`grade` varchar(255),
PRIMARY KEY (`id`)
);
INSERT INTO student VALUES (1, '沈', '一年级');
INSERT INTO student VALUES (2, '李', '一年级');
INSERT INTO student VALUES (3,'张', '二年级');
11.2.2.添加依赖
<ApiTable.version>1.3.0</ApiTable.version>
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>ApiTable</artifactId>
<version>${ApiTable.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>4.0.3</version>
</dependency>
11.2.3.配置文件-app.properties
jdbc.url=jdbc:mysql://192.168.3.9/table_to_json_test?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC
jdbc.user=root
jdbc.pswd=robot_123456#
jdbc.MaximumPoolSize=2
11.2.7.编写启动类
import com.litongjava.hotswap.wrapper.tio.boot.TioApplicationWrapper;
import com.litongjava.jfinal.aop.annotation.AComponentScan;
@AComponentScan
public class TioBootWebApp {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
TioApplicationWrapper.run(TioBootWebApp.class, args);
long end = System.currentTimeMillis();
System.out.println("started:" + (end - start) + "(ms)");
}
}
11.2.5.编写配置类
DbConfig.java, 注意观察 DataSource 的 priority 是 1,priority 表示 bean 启动的优先级,值越小,启动的优先级越高
import javax.sql.DataSource;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.jfinal.aop.annotation.ABean;
import com.litongjava.jfinal.aop.annotation.AConfiguration;
import com.litongjava.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.litongjava.jfinal.plugin.activerecord.OrderedFieldContainerFactory;
import com.litongjava.tio.boot.constatns.ConfigKeys;
import com.litongjava.tio.utils.environment.EnvUtils;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
@AConfiguration
public class DbConfig {
@ABean(priority = 1, destroyMethod = "close")
public DataSource dataSource() {
String jdbcUrl = EnvUtils.get("jdbc.url");
String jdbcUser = EnvUtils.get("jdbc.user");
String jdbcPswd = EnvUtils.get("jdbc.pswd");
int maximumPoolSize = EnvUtils.getInt("jdbc.MaximumPoolSize", 2);
HikariConfig config = new HikariConfig();
// 设定基本参数
config.setJdbcUrl(jdbcUrl);
config.setUsername(jdbcUser);
config.setPassword(jdbcPswd);
config.setMaximumPoolSize(maximumPoolSize);
HikariDataSource hikariDataSource = new HikariDataSource(config);
return hikariDataSource;
}
/*
*
* config ActiveRecordPlugin
*/
@ABean(destroyMethod = "stop", initMethod = "start")
public ActiveRecordPlugin activeRecordPlugin() throws Exception {
DataSource dataSource = Aop.get(DataSource.class);
String property = EnvUtils.get(ConfigKeys.APP_ENV);
ActiveRecordPlugin arp = new ActiveRecordPlugin(dataSource);
arp.setContainerFactory(new OrderedFieldContainerFactory());
if ("dev".equals(property)) {
arp.setDevMode(true);
}
Engine engine = arp.getEngine();
engine.setSourceFactory(new ClassPathSourceFactory());
engine.setCompressorOn(' ');
engine.setCompressorOn('\n');
arp.addSqlTemplate("/sql/all_sqls.sql");
//arp.start();
return arp;
}
}
11.2.6.编写 Controller
查询 student 表中的所有数据,代码如下
package com.enoleap.manglang.pen.api.server.controller;
import java.util.List;
import com.jfinal.kit.Kv;
import com.litongjava.data.model.DbJsonBean;
import com.litongjava.data.services.DbJsonService;
import com.litongjava.data.utils.DbJsonBeanUtils;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.tio.http.common.HttpRequest;
import com.litongjava.annotation.RequestPath;
@RequestPath("/db/student")
public class DbTestController {
DbJsonService dbJsonService = DbJsonService.getInstance();
@RequestPath("/list")
public DbJsonBean<List<Kv>> list(HttpRequest request) {
String tableName = "student";
DbJsonBean<List<Kv>> jsonBean = DbJsonBeanUtils.recordsToKv(dbJsonService.listAll(tableName));
return jsonBean;
}
}
访问 http://localhost/db/student/list 输出如下
{"code":0,"data":[{"grade":"一年级","name":"沈","id":"1"},{"grade":"一年级","name":"李","id":"2"},{"grade":"二年级","name":"张","id":"3"}],"msg":""}