【Spring AOP + 自定义注解 + 动态数据源 实现主从库切换&读写分离】—— 案例实战(下)

简介: 【Spring AOP + 自定义注解 + 动态数据源 实现主从库切换&读写分离】—— 案例实战(下)

【Spring AOP + 自定义注解 + 动态数据源 实现主从库切换&读写分离】—— 案例实战(中):https://developer.aliyun.com/article/1390149?spm=a2c6h.13148508.setting.23.54b14f0eHMAggH

其他文件说明

UserController

💧这个文件是一个Spring Boot的控制器类,名为 UserController。它处理来自前端的HTTP请求,调用 UserService 中的方法来处理业务逻辑,并返回相应的结果。

package com.lxr.demo.controller;
import com.lxr.demo.entity.UserEntity;
import com.lxr.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Random;
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/listUser")
    public List<UserEntity> listUser() {
        List<UserEntity> users = userService.findAll();
        return users;
    }
    @RequestMapping("/insertUser")
    public void insertUser() {
        UserEntity userEntity = new UserEntity();
        Random random = new Random();
        userEntity.setUser_id(random.nextInt());
        userEntity.setAccount("22222");
        userEntity.setNickname("lxrlxrlxr");
        userEntity.setPassword("123");
        userService.insertUser(userEntity);
    }
}

UserEntity

💧一个普通的实体类,使用了lombok的@Data注解。

package com.lxr.demo.entity;
import lombok.Data;
@Data
public class UserEntity {
    private Integer user_id;
    private String account;
    private String nickname;
    private String password;
    private String headimage_url;
    private String introduce;
}

UserMapper

💧一个简单的dao层。

package com.lxr.demo.mapper;
import com.lxr.demo.entity.UserEntity;
import org.apache.ibatis.annotations.*;
import java.util.List;
/**
 * Spring通过@Mapper注解实现动态代理,mybatis会自动创建Dao接口的实现类代理对象注入IOC容器进行管理,这样就不用编写Dao层的实现类
 */
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user2")
    List<UserEntity> findAll();
    @Insert("insert into user2(user_id,account,nickname,password) values(#{user_id},#{account}, #{nickname}, #{password})")
    int insert(UserEntity user);
//    @Update("UPDATE user2 SET account=#{account},nickname=#{nickname} WHERE id =#{id}")
//    void update(UserEntity user);
//
//    @Delete("DELETE FROM user2 WHERE id =#{id}")
//    void delete(Long id);
}

UserService

💧一个简单的Service层。

package com.lxr.demo.service;
import com.lxr.demo.entity.UserEntity;
import com.lxr.demo.mapper.UserMapper;
import com.lxr.demo.config.Master;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    public List<UserEntity> findAll() {
        return userMapper.findAll();
    }
    @Master
    public int insertUser(UserEntity user) {
        return userMapper.insert(user);
    }
//    void update(UserEntity user);
//
//    void delete(Long id);
}

主启动类DemoApplication

package com.lxr.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.lxr.demo.mapper")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

功能演示

💧我们启动项目,打开浏览器或者postman等工具

💧分别访问:

  • 从库操作 :http://localhost:8080/listUser

  • 主库操作 :http://localhost:8080/insertUser


总结

💧通过本篇博客,我们学习了如何使用 Spring AOP 结合自定义注解和德鲁伊数据源来实现主从数据库切换方法的读写分离。通过自定义注解 @Master 来区分主库还是从库,通过切点来区分读写方法,我们成功地将读写操作路由到不同的数据源,从而提高了应用程序的性能和可伸缩性。读写分离是一个重要的数据库优化策略,在实际的生产环境中非常有用。

💧希望本篇博客对您有所帮助,如果您有任何问题或建议,欢迎在评论区留言。谢谢阅读!

相关文章
|
4月前
|
Java API 数据安全/隐私保护
(工作经验)优雅实现接口权限校验控制:基于自定义注解、AOP与@ConditionalOnProperty配置开关的通用解决方案
(工作经验)优雅实现接口权限校验控制:基于自定义注解、AOP与@ConditionalOnProperty配置开关的通用解决方案
123 1
|
2月前
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
2月前
|
设计模式 XML Java
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
本文详细介绍了Spring框架的核心功能,并通过手写自定义Spring框架的方式,深入理解了Spring的IOC(控制反转)和DI(依赖注入)功能,并且学会实际运用设计模式到真实开发中。
【23种设计模式·全精解析 | 自定义Spring框架篇】Spring核心源码分析+自定义Spring的IOC功能,依赖注入功能
|
2月前
|
NoSQL Java Redis
Spring Boot 自动配置机制:从原理到自定义
Spring Boot 的自动配置机制通过 `spring.factories` 文件和 `@EnableAutoConfiguration` 注解,根据类路径中的依赖和条件注解自动配置所需的 Bean,大大简化了开发过程。本文深入探讨了自动配置的原理、条件化配置、自定义自动配置以及实际应用案例,帮助开发者更好地理解和利用这一强大特性。
147 14
|
3月前
|
安全 Java 开发者
AOP中的JDK动态代理与CGLIB动态代理:深度解析与实战模拟
【11月更文挑战第21天】面向切面编程(AOP,Aspect-Oriented Programming)是一种编程范式,它通过将横切关注点(cross-cutting concerns)与业务逻辑分离,以提高代码的可维护性和可重用性。在Java开发中,AOP的实现离不开动态代理技术,其中JDK动态代理和CGLIB动态代理是两种常用的方式。本文将从背景、历史、功能点、业务场景、底层逻辑等多个维度,深度解析这两种代理方式的区别,并通过Java示例进行模拟和比较。
168 5
|
4月前
|
XML Java 数据格式
使用完全注解的方式进行AOP功能实现(@Aspect+@Configuration+@EnableAspectJAutoProxy+@ComponentScan)
本文介绍了如何使用Spring框架的注解方式实现AOP(面向切面编程)。当目标对象没有实现接口时,Spring会自动采用CGLIB库进行动态代理。文中详细解释了常用的AOP注解,如`@Aspect`、`@Pointcut`、`@Before`等,并提供了完整的示例代码,包括业务逻辑类`User`、配置类`SpringConfiguration`、切面类`LoggingAspect`以及测试类`TestAnnotationConfig`。通过这些示例,展示了如何在方法执行前后添加日志记录等切面逻辑。
535 2
使用完全注解的方式进行AOP功能实现(@Aspect+@Configuration+@EnableAspectJAutoProxy+@ComponentScan)
|
3月前
|
JSON Java 数据库
SpringBoot项目使用AOP及自定义注解保存操作日志
SpringBoot项目使用AOP及自定义注解保存操作日志
76 1
|
4月前
|
自然语言处理 Java API
Spring Boot 接入大模型实战:通义千问赋能智能应用快速构建
【10月更文挑战第23天】在人工智能(AI)技术飞速发展的今天,大模型如通义千问(阿里云推出的生成式对话引擎)等已成为推动智能应用创新的重要力量。然而,对于许多开发者而言,如何高效、便捷地接入这些大模型并构建出功能丰富的智能应用仍是一个挑战。
674 6
|
3月前
|
安全 Java 应用服务中间件
如何将Spring Boot应用程序运行到自定义端口
如何将Spring Boot应用程序运行到自定义端口
111 0
|
3月前
|
Java Maven Spring
Spring 小案例体验创建对象的快感
本文介绍了如何在IDEA中创建一个Spring项目,包括项目创建、配置pom.xml文件以引入必要的依赖、编写实体类HelloSpring及其配置文件applicationContext.xml,最后通过测试类TestHelloSpring展示如何使用Spring的bean创建对象并调用方法。
44 0