使用AOP思想封装JDBC

Posted 东子z

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用AOP思想封装JDBC相关的知识,希望对你有一定的参考价值。

看代码

package learning.aop2;

import org.springframework.stereotype.Component;

import java.sql.SQLException;
@Component
public class UserDAO {

    public void getAddUser(String name) throws SQLException {
        ConnMariaDB.getConn().createStatement().
                execute("insert into user(name) VALUE (‘" + name + "‘)");
    }

}

得到连接

package learning.aop2;

import java.sql.Connection;

public class ConnMariaDB {
    public static ThreadLocal<Connection> threadLocal = null;

    public static Connection getConn() {
        return threadLocal.get();
    }
}

调用方法前往当前线程注入一个连接再调用

package learning.aop2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
@Aspect
public class JDBCAdvice {

    @Around("execution(* learning.aop2.UserDAO.*(..))")
    public Object wrapDAO(ProceedingJoinPoint joinPoint) throws Throwable {
        Connection conn = null;
        try {
            Class.forName("org.mariadb.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/user", "root", "dz520123");
            conn.setAutoCommit(false);

            ConnMariaDB.threadLocal = new ThreadLocal<Connection>();
            ConnMariaDB.threadLocal.set(conn);

            Object proceed = joinPoint.proceed();

            conn.commit();

            return proceed;
        } catch (Throwable throwable) {
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException e) {
                }
            }
            throwable.printStackTrace();
            throw throwable;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }
}

扫描包

package learning.aop2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan(basePackages = "learning.aop2")
@EnableAspectJAutoProxy
public class SpringConfig {
}

测试.java

package learning.aop2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.sql.SQLException;

public class Main {
    public static void main(String[] args) throws SQLException {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        UserDAO bean = applicationContext.getBean(UserDAO.class);
        bean.getAddUser("nihao");
    }
}

 

以上是关于使用AOP思想封装JDBC的主要内容,如果未能解决你的问题,请参考以下文章

Spring框架基础2

看了绝对不会后悔之:spring AOP原理

day39-Spring 12-Spring的JDBC模板:快速入门

优化JDBC封装

Spring整合JDBC以及AOP管理事务

Spring整合JDBC以及AOP管理事务