


An example analysis on how spring integrates mybatis to realize the separation of reading and writing of mysql database
This article introduces spring to integrate mybatis to realize the separation of reading and writing of mysql database through example code. Friends who need it can refer to it
Preface
After the number of users of the website reaches a certain scale, the database becomes the bottleneck of the website due to excessive load pressure. Fortunately, most of the current mainstream databases provide master-slave hot standby function. By configuring the master-slave relationship between two databases, the data updates of one database can be synchronized to the other server. The website uses this function of the database to realize the separation of database reading and writing, thereby improving the database load pressure. As shown in the figure below:
When the application server writes data, it accesses the master database. The master database synchronizes data updates to the slave database through the master-slave replication mechanism, so that when the application When the server reads data, it can obtain the data from the database. In order to facilitate application programs to access the read-write separated database, a special database access module is usually used in the application server to make the database read-write separation transparent to the application.
This blog is to implement a "specialized database access module" to make the separation of database reading and writing transparent to the application. In addition, for the master-slave replication of the mysql database, you can refer to my installation and master-slave replication of mysql5.7.18. Note that only when the database implements master-slave replication can the database read and write be separated. Therefore, if you do not implement database master-slave replication, remember to implement the database's master-slave replication first.
Configure reading and writing Data source (master-slave database)
##mysqldb.properties
##
#主数据库数据源 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.0.4:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false jdbc.username=root jdbc.password=123456 jdbc.initialSize=1 jdbc.minIdle=1 jdbc.maxActive=20 jdbc.maxWait=60000 jdbc.removeAbandoned=true jdbc.removeAbandonedTimeout=180 jdbc.timeBetweenEvictionRunsMillis=60000 jdbc.minEvictableIdleTimeMillis=300000 jdbc.validationQuery=SELECT 1 jdbc.testWhileIdle=true jdbc.testOnBorrow=false jdbc.testOnReturn=false #从数据库数据源 slave.jdbc.driverClassName=com.mysql.jdbc.Driver slave.jdbc.url=jdbc:mysql://192.168.0.221:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false slave.jdbc.username=root slave.jdbc.password=123456 slave.jdbc.initialSize=1 slave.jdbc.minIdle=1 slave.jdbc.maxActive=20 slave.jdbc.maxWait=60000 slave.jdbc.removeAbandoned=true slave.jdbc.removeAbandonedTimeout=180 slave.jdbc.timeBetweenEvictionRunsMillis=60000 slave.jdbc.minEvictableIdleTimeMillis=300000 slave.jdbc.validationQuery=SELECT 1 slave.jdbc.testWhileIdle=true slave.jdbc.testOnBorrow=false slave.jdbc.testOnReturn=false
Addresses of the master and slave databases Remember to change it to your own, the account number and password also need to be changed to your own; for other configuration items, you can set them yourself as appropriate
mybatis-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- master数据源 --> <bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 基本属性 url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="initialSize" value="${jdbc.initialSize}" /> <property name="minIdle" value="${jdbc.minIdle}" /> <property name="maxActive" value="${jdbc.maxActive}" /> <property name="maxWait" value="${jdbc.maxWait}" /> <!-- 超过时间限制是否回收 --> <property name="removeAbandoned" value="${jdbc.removeAbandoned}" /> <!-- 超过时间限制多长; --> <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> <!-- 用来检测连接是否有效的sql,要求是一个查询语句--> <property name="validationQuery" value="${jdbc.validationQuery}" /> <!-- 申请连接的时候检测 --> <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 --> <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 --> <property name="testOnReturn" value="${jdbc.testOnReturn}" /> </bean> <!-- slave数据源 --> <bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${slave.jdbc.driverClassName}" /> <property name="url" value="${slave.jdbc.url}" /> <property name="username" value="${slave.jdbc.username}" /> <property name="password" value="${slave.jdbc.password}" /> <property name="initialSize" value="${slave.jdbc.initialSize}" /> <property name="minIdle" value="${slave.jdbc.minIdle}" /> <property name="maxActive" value="${slave.jdbc.maxActive}" /> <property name="maxWait" value="${slave.jdbc.maxWait}" /> <property name="removeAbandoned" value="${slave.jdbc.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${slave.jdbc.removeAbandonedTimeout}" /> <property name="timeBetweenEvictionRunsMillis" value="${slave.jdbc.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${slave.jdbc.minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${slave.jdbc.validationQuery}" /> <property name="testWhileIdle" value="${slave.jdbc.testWhileIdle}" /> <property name="testOnBorrow" value="${slave.jdbc.testOnBorrow}" /> <property name="testOnReturn" value="${slave.jdbc.testOnReturn}" /> </bean> <!-- 动态数据源,根据service接口上的注解来决定取哪个数据源 --> <bean id="dataSource" class="com.yzb.util.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <!-- write or slave --> <entry key="slave" value-ref="slaveDataSource"/> <!-- read or master --> <entry key="master" value-ref="masterDataSource"/> </map> </property> <property name="defaultTargetDataSource" ref="masterDataSource"/> </bean> <!-- Mybatis文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> <!-- 映射文件路径 --> <property name="mapperLocations" value="classpath*:dbmappers/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yzb.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 声明式开启 --> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" order="1"/> <!-- 为业务逻辑层的方法解析@DataSource注解 为当前线程的HandleDataSource注入数据源 --> <bean id="dataSourceAspect" class="com.yzb.util.DataSourceAspect" /> <aop:config proxy-target-class="true"> <aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="2"> <aop:pointcut id="tx" expression="execution(* com.yzb.service.impl..*.*(..)) "/> <aop:before pointcut-ref="tx" method="before" /> </aop:aspect> </aop:config> </beans>
AOP implements dynamic switching of data sources
DataSource.java
package com.yzb.util; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * RUNTIME * 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。 * */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface DataSource { String value(); }
DataSourceAspect.java
package com.yzb.util; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; public class DataSourceAspect { /** * 在dao层方法获取datasource对象之前,在切面中指定当前线程数据源 */ public void before(JoinPoint point) { Object target = point.getTarget(); String method = point.getSignature().getName(); Class<?>[] classz = target.getClass().getInterfaces(); // 获取目标类的接口, 所以@DataSource需要写在接口上 Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()) .getMethod().getParameterTypes(); try { Method m = classz[0].getMethod(method, parameterTypes); if (m != null && m.isAnnotationPresent(DataSource.class)) { DataSource data = m.getAnnotation(DataSource.class); System.out.println("用户选择数据库库类型:" + data.value()); HandleDataSource.putDataSource(data.value()); // 数据源放到当前线程中 } } catch (Exception e) { e.printStackTrace(); } } }
DynamicDataSource.java
package com.yzb.util; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource { /** * 获取与数据源相关的key 此key是Map<String,DataSource> resolvedDataSources 中与数据源绑定的key值 * 在通过determineTargetDataSource获取目标数据源时使用 */ @Override protected Object determineCurrentLookupKey() { return HandleDataSource.getDataSource(); } }
HandleDataSource.java ##
package com.yzb.util; public class HandleDataSource { public static final ThreadLocal<String> holder = new ThreadLocal<String>(); /** * 绑定当前线程数据源 * * @param key */ public static void putDataSource(String datasource) { holder.set(datasource); } /** * 获取当前线程的数据源 * * @return */ public static String getDataSource() { return holder.get(); } }
Apply @DataSource on the service interface to implement the data source Specification
package com.yzb.service; import java.util.List; import com.yzb.model.Person; import com.yzb.util.DataSource; public interface IPersonService { /** * 加载全部的person * @return */ List<Person> listAllPerson(); /** * 查询某个人的信息 * @param personId * @return */ @DataSource("slave") // 指定使用从数据源 Person getPerson(int personId); boolean updatePerson(Person person); }
When testing, how do you know what is being read from? Database? We can modify the value of a certain field of the record queried from the database to distinguish the master and slave databases;
Transactions need to be noted and try to ensure that transactions are conducted on one data source; When there are multiple aops on a service, you need to pay attention to the order of aop weaving, and use the order keyword to control the weaving order; SummaryThe above is the detailed content of An example analysis on how spring integrates mybatis to realize the separation of reading and writing of mysql database. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.