MySQL如何设置Hibernate读写不同数据源?
在Java开发中,使用Hibernate作为ORM框架时,很多时候我们需要读写不同的数据源,例如读取主数据源,写入从数据源。本文将介绍如何使用Hibernate设置读写不同的MySQL数据源。
阅读更多:MySQL 教程
准备工作
在开始之前,需要准备好以下两个MySQL数据源:
- 主数据源,用于读取数据。
- 从数据源,用于写入数据。
在MySQL中可以新建两个数据库,分别为”master_db”和”slave_db”。
配置Hibernate
在Hibernate中,数据源的配置需要在hibernate.cfg.xml文件中进行,我们需要将其中的配置项分别修改为对应的主从数据源。
主数据源配置
首先,打开hibernate.cfg.xml文件,在该文件中的 <session-factory> 标签中添加以下配置:
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/master_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
上述配置项分别代表了MySQL连接驱动、主数据源的url、用户名和密码。其中,url中的”master_db”为我们在准备工作中新建的主数据源。
从数据源配置
接着,在 <session-factory> 标签中新增以下配置项:
<property name="hibernate.connection.provider_class">org.hibernate.hikaricp.internal.HikariCPConnectionProvider</property>
<property name="hibernate.hikari.dataSourceClassName">com.mysql.cj.jdbc.MysqlDataSource</property>
<property name="hibernate.hikari.dataSource.url">jdbc:mysql://localhost:3306/slave_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai</property>
<property name="hibernate.hikari.dataSource.user">root</property>
<property name="hibernate.hikari.dataSource.password">123456</property>
上述配置项代表了使用HikariCP作为连接池,从数据源的url、用户名和密码。其中,url中的”slave_db”为我们在准备工作中新建的从数据源。
配置SessionFactory
接着,我们需要在Java中将上述配置项与SessionFactory关联起来。具体代码如下:
Configuration configuration = new Configuration().configure();
HibernateUtil.addDialectProperty(configuration);
HibernateUtil.addDataSource(configuration, "primary", false);
HibernateUtil.addDataSource(configuration, "secondary", true);
// sessionFactory
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
上述代码中,我们通过 Configuration 类加载hibernate.cfg.xml文件中的配置项,并通过 HibernateUtil 类中的 addDialectProperty()、addDataSource() 方法进行数据源的配置。其中,第二个参数代表是否是从数据源。
数据库读写操作
最后,我们介绍如何对多个数据源进行读写操作。由于本文不涉及业务逻辑,我们以一个User实体类为例。
写入操作
User user = new User();
user.setUsername("Tom");
user.setPassword("123456");
user.setRole("admin");
Session session = sessionFactory.withOptions()
.dataSource("secondary")
.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
上述代码中,通过 sessionFactory.withOptions().dataSource("secondary").openSession() 方法获取到从数据源的Session,并向其中插入一条数据。
读取操作
Session session = sessionFactory.withOptions()
.dataSource("primary")
.openSession();
List<User> userList = session.createQuery("from User", User.class).list();
上述代码中,通过 sessionFactory.withOptions().dataSource("primary").openSession() 方法获取到主数据源的Session,并查询其中的所有User记录。
总结
本文介绍了如何使用Hibernate读写不同的MySQL数据源。首先需要在MySQL中准备好主从数据源,接着在Hibernate配置文件中分别进行主从数据源的配置,然后在Java代码中将配置与SessionFactory关联起来,并根据需求使用不同的数据源进行读写操作。
需要注意的是,在实际业务中需要根据数据源的不同,对数据连接进行合理的管理,以避免出现连接泄露等问题。另外,本文中的代码中包含了Session的beginTransaction和事务提交等操作,实际开发中也需要根据业务逻辑进行相应的处理。
总之,在使用Hibernate时,通过合理配置和管理数据源,可以更加灵活地对MySQL数据库进行读写操作,从而满足不同业务的需求。
极客教程