在数字化转型的浪潮中,国产数据库正以前所未有的速度崛起。作为其中的佼佼者,KingbaseES凭借其自主可控、高性能、高可用的特性,在政务、金融、能源等关键领域大放异彩。而Spring JDBC框架作为Java生态中久经考验的数据访问利器,其简洁的设计和强大的功能,使其成为连接KingbaseES的理想选择。本文将探索Spring JDBC与KingbaseES的集成,构建高性能的国产数据库应用

KingbaseES作为金仓推出的企业级数据库,不仅兼容SQL标准,更在性能优化、高可用性、安全合规等方面展现出独特优势。其MySQL兼容版更是在易用性上迈出重要一步
Spring JDBC作为Spring框架的核心组件之一,通过JdbcTemplate等工具类封装了JDBC的冗长操作,提供了更简洁、更安全的数据库访问方式。其特点包括:
点击下载:spring-jdbc-kingbase-client
操作步骤:
例如:
./ksql -U system -d test -h xx.xx.xx.xx -p 54321
根据 Kingbase数据库连接串信息填写下面 URL 的对应信息
jdbc:kingbase8://$host:$port/$database_name?user=$user_name&password=$password
参数说明:
$host :提供 Kingbase数据库连接 IP。
$port :提供 Kingbase数据库连接端口。默认是54321 ,在部署 Kingbase数据库时可自定义端口号。
$database_name:需要访问的 database名称。
需注意:
连接租户的用户需要拥有该 database的 CREATE TABLE 、DROP TABLE 、 INSERT 、 DELETE 、 UPDATE 和 SELECT 权限
获取 Kingbase数据库 URL 中获取的信息修改文件 spring-jdbc-kingbase-client/src/main/java/com/example/Main.java 中的数据库连接信息
例如:
// Database Connect Information String url = "jdbc:kingbase8://192.168.xx.xxx:7901/test"; String username = "root"; String password = "123456";
运行的步骤博主统计并列出来了,小伙伴可以跟着操作
DDL语句示例:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(50),
salary NUMERIC(10,2),
hire_date DATE DEFAULT CURRENT_DATE
);
Java执行代码:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeRepository {
private final JdbcTemplate jdbcTemplate;
@Autowired
public EmployeeRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void createTable() {
jdbcTemplate.execute("""
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department VARCHAR(50),
salary NUMERIC(10,2),
hire_date DATE DEFAULT CURRENT_DATE
)
""");
}
}
单条插入示例:
public void insertEmployee(Employee employee) {
jdbcTemplate.update("""
INSERT INTO employees (name, department, salary, hire_date)
VALUES (?, ?, ?, ?)
""", employee.getName(), employee.getDepartment(),
employee.getSalary(), employee.getHireDate());
}
批量插入优化:
public void batchInsert(List<Employee> employees) {
jdbcTemplate.batchUpdate("""
INSERT INTO employees (name, department, salary, hire_date)
VALUES (?, ?, ?, ?)
""", new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Employee e = employees.get(i);
ps.setString(1, e.getName());
ps.setString(2, e.getDepartment());
ps.setBigDecimal(3, e.getSalary());
ps.setDate(4, new Date(e.getHireDate().getTime()));
}
@Override
public int getBatchSize() {
return employees.size();
}
});
}
基础查询:
public List<Employee> findAll() {
return jdbcTemplate.query("""
SELECT id, name, department, salary, hire_date
FROM employees
""", (rs, rowNum) -> new Employee(
rs.getLong("id"),
rs.getString("name"),
rs.getString("department"),
rs.getBigDecimal("salary"),
rs.getDate("hire_date")
));
}
条件查询:
public List<Employee> findByDepartment(String department) {
return jdbcTemplate.query("""
SELECT * FROM employees
WHERE department = ?
ORDER BY salary DESC
""", new Object[]{department}, new BeanPropertyRowMapper<>(Employee.class));
}
更新操作:
public void updateSalary(Long id, BigDecimal newSalary) {
jdbcTemplate.update("""
UPDATE employees
SET salary = ?
WHERE id = ?
""", newSalary, id);
}
删除操作:
public void deleteEmployee(Long id) {
jdbcTemplate.update("""
DELETE FROM employees
WHERE id = ?
""", id);
}
public void dropTable() {
jdbcTemplate.execute("DROP TABLE IF EXISTS employees");
}
配置事务管理器:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
@Configuration
public class TransactionConfig {
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
服务层事务管理:
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
@Service
public class EmployeeService {
private final EmployeeRepository employeeRepository;
public EmployeeService(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Transactional
public void transferDepartment(Long employeeId, String newDept) {
// 更新部门操作
employeeRepository.updateDepartment(employeeId, newDept);
// 记录变更日志(需在同一事务中)
employeeRepository.logDepartmentChange(employeeId, newDept);
}
}
@Transactional(
propagation = Propagation.REQUIRED,
isolation = Isolation.READ_COMMITTED,
timeout = 30,
rollbackFor = {SQLException.class, RuntimeException.class}
)
public void complexOperation() {
// 业务逻辑
}
全局异常处理器:
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(DataAccessException.class)
public ResponseEntity<ErrorResponse> handleDataAccessException(DataAccessException ex) {
ErrorResponse error = new ErrorResponse(
"DATABASE_ERROR",
ex.getMessage(),
LocalDateTime.now()
);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(error);
}
}
配置SQL日志:
logging:
level:
org.springframework.jdbc.core.JdbcTemplate: DEBUG
org.springframework.jdbc.core.StatementCreatorUtils: TRACE
自定义日志格式:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class JdbcLogger {
private static final Logger logger = LogManager.getLogger();
public static void logSql(String sql, Object... params) {
logger.debug("Executing SQL: {} with parameters: {}", sql, params);
}
}
// 使用参数化查询防止SQL注入
public List<Employee> findByNamePattern(String namePattern) {
return jdbcTemplate.query("""
SELECT * FROM employees
WHERE name LIKE ?
ESCAPE ''
""", new Object[]{namePattern + "%"},
new BeanPropertyRowMapper<>(Employee.class));
}
批量插入性能对比:
| 操作类型 | 1000条记录耗时 | 10000条记录耗时 |
|---|---|---|
| 单条插入 | 1200ms | 12000ms |
| 批量插入 | 80ms | 650ms |
优化技巧:
KingbaseES存储过程示例:
CREATE OR REPLACE PROCEDURE raise_salary(
IN emp_id BIGINT,
IN percent NUMERIC
)
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE employees
SET salary = salary * (1 + percent/100)
WHERE id = emp_id;
END;
$$;
Java调用代码:
public void callRaiseSalary(Long empId, BigDecimal percent) {
jdbcTemplate.execute("""
CALL raise_salary(?, ?)
""", empId, percent);
}
物理分页实现:
public Page<Employee> findPaginated(int page, int size) {
int offset = (page - 1) * size;
List<Employee> employees = jdbcTemplate.query("""
SELECT * FROM employees
ORDER BY salary DESC
LIMIT ? OFFSET ?
""", new Object[]{size, offset},
new BeanPropertyRowMapper<>(Employee.class));
long total = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM employees", Long.class);
return new PageImpl<>(employees, Pageable.ofSize(size).withPage(page), total);
}
KingbaseES特有函数:
-- 日期处理
SELECT to_char(hire_date, 'YYYY-MM-DD') FROM employees;
-- JSON处理
SELECT json_object('name': name, 'dept': department)
FROM employees;
-- 全文检索
SELECT * FROM employees
WHERE to_tsvector('zh', name) @@ to_tsquery('zh', '张三');
CREATE INDEX idx_employee_salary ON employees(salary);
SELECT /*+ INDEX(employees idx_employee_salary) */ * FROM employees WHERE salary > 5000;
连接问题:
测试数据库连接
telnet localhost 54321
检查连接池状态
http://localhost:8080/actuator/hikari
本文详细介绍如何使用 Spring JDBC框架和 kingbase 数据库构建一个应用程序,实现创建表、插入数据、查询数据,更新数据、删除数据和删除表等基本操作。有任何问题可联系博主解决,后面博主将带着伙伴们探索更多 “知识”。
未来,随着云计算、大数据技术的发展,KingbaseES正在向云原生、分布式架构演进。Spring框架也在不断进化