使用 Hibernate 连接到平凯数据库

平凯数据库是一个兼容 MySQL 的数据库。Hibernate 是当前比较流行的开源 Java 应用持久层框架,且 Hibernate 在版本 6.0.0.Beta2 及以上支持了平凯数据库方言,完美适配了平凯数据库的特性。

本文档将展示如何使用平凯数据库和 Hibernate 来完成以下任务:

  • 配置你的环境。
  • 使用 Hibernate 连接到平凯数据库集群。
  • 构建并运行你的应用程序。你也可以参考示例代码片段,完成基本的 CRUD 操作。

前置需求

  • 推荐 Java Development Kit (JDK) 17 及以上版本。你可以根据公司及个人需求,自行选择 OpenJDKOracle JDK
  • Maven 3.8 及以上版本。
  • Git
  • 平凯数据库集群。

如果你还没有平凯数据库集群,可以按如下方式创建一个:

运行代码并连接到平凯数据库

本小节演示如何运行示例应用程序的代码,并连接到平凯数据库。

第 1 步:克隆示例代码仓库到本地

运行以下命令,将示例代码仓库克隆到本地:

git clone https://github.com/tidb-samples/tidb-java-hibernate-quickstart.git cd tidb-java-hibernate-quickstart

第 2 步:配置连接信息

  1. 运行以下命令,将 env.sh.example 复制并重命名为 env.sh

    cp env.sh.example env.sh
  2. 复制并粘贴对应 TiDB 的连接字符串至 env.sh 中。需更改部分示例结果如下:

    export TIDB_HOST='{host}' export TIDB_PORT='4000' export TIDB_USER='root' export TIDB_PASSWORD='{password}' export TIDB_DB_NAME='test' export USE_SSL='false'

    注意替换 {} 中的占位符为你的 TiDB 对应的值,并设置 USE_SSLfalse。如果你在本机运行 TiDB,默认 Host 地址为 127.0.0.1,密码为空。

  3. 保存 env.sh 文件。

第 3 步:运行代码并查看结果

  1. 运行下述命令,执行示例代码:

    make
  2. 查看 Expected-Output.txt,并与你的程序输出进行比较。结果近似即为连接成功。

示例代码片段

你可参考以下关键代码片段,完成自己的应用开发。

完整代码及其运行方式,见代码仓库 tidb-java-hibernate-quickstart。

连接到平凯数据库

编写配置文件 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property> <property name="hibernate.dialect">org.hibernate.dialect.TiDBDialect</property> <property name="hibernate.connection.url">${tidb_jdbc_url}</property> <property name="hibernate.connection.username">${tidb_user}</property> <property name="hibernate.connection.password">${tidb_password}</property> <property name="hibernate.connection.autocommit">false</property> <!-- Required so a table can be created from the 'PlayerDAO' class --> <property name="hibernate.hbm2ddl.auto">create-drop</property> <!-- Optional: Show SQL output for debugging --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> </session-factory> </hibernate-configuration>

请将 ${tidb_jdbc_url}${tidb_user}${tidb_password} 等替换为你的 TiDB 集群的实际值。随后编写以下函数:

public SessionFactory getSessionFactory() { return new Configuration() .configure("hibernate.cfg.xml") .addAnnotatedClass(${your_entity_class}) .buildSessionFactory(); }

在使用该函数时,你需要替换 ${your_entity_class} 为自己的数据实体类。如果你有多个实体类,需要添加多个 .addAnnotatedClass(${your_entity_class}) 语句。此外,这仅是 Hibernate 的其中一种配置方式。在配置中遇到任何问题,或想了解更多关于 Hibernate 的信息,你可参考 Hibernate 官方文档

插入或更新数据

try (Session session = sessionFactory.openSession()) { session.persist(new PlayerBean("id", 1, 1)); }

更多信息参考插入数据更新数据

查询数据

try (Session session = sessionFactory.openSession()) { PlayerBean player = session.get(PlayerBean.class, "id"); System.out.println(player); }

更多信息参考查询数据

删除数据

try (Session session = sessionFactory.openSession()) { session.remove(new PlayerBean("id", 1, 1)); }

更多信息参考删除数据

下一步

需要帮助?

如果在开发的过程中遇到问题,可以在 AskTUG 上进行提问,寻求帮助。