博客
关于我
【Spring Aop】练习:注解实现Jdbc的Aop操作
阅读量:329 次
发布时间:2019-03-04

本文共 4257 字,大约阅读时间需要 14 分钟。


文章目录


要求

  • 利用注解形式实现在jdbc中的通知,通知方法形式是输出当前的时间~

代码实现

实体类

  • 根据数据库表格封装实例类
    在这里插入图片描述
package JDBC_AOP.Entry;public class User {       // 字段    private String userId;    private String userName;    private String userStatus;    //重写toString()    @Override    public String toString() {           return "User{" +                "userId='" + userId + '\'' +                ", userName='" + userName + '\'' +                ", userStatus='" + userStatus + '\'' +                '}';    }    // set\get    public void setUserId(String userId) {           this.userId = userId;    }    public void setUserName(String userName) {           this.userName = userName;    }    public void setUserStatus(String userStatus) {           this.userStatus = userStatus;    }    public String getUserId() {           return userId;    }    public String getUserName() {           return userName;    }    public String getUserStatus() {           return userStatus;    }}


Dao层

package JDBC_AOP.Dao;import JDBC_AOP.Entry.User;import java.util.List;public interface UserDao {       List
getAll();}
package JDBC_AOP.Dao;import JDBC_AOP.Entry.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;import java.util.List;@Repositorypublic class UserDaoImpl implements UserDao{       @Autowired    JdbcTemplate jdbcTemplate;  // 注入jdbcTemplate帮助类    @Override                   // 查询所有    public List
getAll() { String sql = "select * from t_user"; List
list = jdbcTemplate.query(sql,new BeanPropertyRowMapper
(User.class)); System.out.println(list.get(1).getUserName()); return list; }}


Service层

package JDBC_AOP.Service;import JDBC_AOP.Entry.User;import java.util.List;public interface UserService {       List
getAll();}
package JDBC_AOP.Service;import JDBC_AOP.Dao.UserDao;import JDBC_AOP.Entry.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService{       @Autowired    private UserDao dao;    @Override    public List
getAll() { return dao.getAll(); }}


Aop处理

package JDBC_AOP.Extra;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.omg.CORBA.PUBLIC_MEMBER;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;@Aspect@Componentpublic class Extra {       // 抽取公共切入点    @Pointcut("execution(* JDBC_AOP.Dao.UserDaoImpl.getAll(..))")    public void all(){   }    @Before(value="all()")   // 前置通知    public void showTimeBefore(){           Date date = new Date(System.currentTimeMillis());        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-hh HH:mm:ss:SSS");        String str = simpleDateFormat.format(date);        System.out.println(str+" Before");    }    @After(value="all()")    // 后置通知    public void showTimeAfter(){           Date date = new Date(System.currentTimeMillis());        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-hh HH:mm:ss:SSS");        String str = simpleDateFormat.format(date);        System.out.println(str+" After");    }}


xml配置


测试类

package JDBC_AOP;import JDBC_AOP.Dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {       @Autowired    private UserDao dao;      // 注入UserDao对象    // 测试方法     public void print(){           dao.getAll();    }    public static void main(String[] args) {           ApplicationContext context = new ClassPathXmlApplicationContext("JDBC_AOP/jdbcaop.xml");        Test test = context.getBean("test",Test.class);        test.print();    }}

在这里插入图片描述


转载地址:http://zjeq.baihongyu.com/

你可能感兴趣的文章
MySQL高级-SQL优化步骤
查看>>
MySQL高级-视图
查看>>
MySQL高级-触发器
查看>>
mysql高级查询~分页查询
查看>>
MySQL(2)DDL详解
查看>>
MySQL:MySQL执行一条SQL查询语句的执行过程
查看>>
Mysql:SQL性能分析
查看>>
MySQL:判断逗号分隔的字符串中是否包含某个字符串
查看>>
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
nacos config
查看>>
NacosClient客户端搭建,微服务注册进nacos
查看>>
Nacos原理
查看>>
Nacos在双击startup.cmd启动时提示:Unable to start embedded Tomcat
查看>>
Nacos如何实现Raft算法与Raft协议原理详解
查看>>
Nacos安装教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
nacos注册失败,Feign调用失败,feign无法注入成我们的bean对象
查看>>
nacos源码 nacos注册中心1.4.x 源码 nacos源码如何下载 nacos 客户端源码下载地址 nacos discovery下载地址(一)
查看>>
Nacos编译报错NacosException: endpoint is blank
查看>>