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

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


文章目录


要求

  • 利用注解形式实现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;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private 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;
@Service
public 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
@Component
public 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;
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/

你可能感兴趣的文章
Objective-C实现euler method欧拉法算法(附完整源码)
查看>>
Objective-C实现euler modified变形欧拉法算法(附完整源码)
查看>>
Objective-C实现eulerianPath欧拉路径算法(附完整源码)
查看>>
Objective-C实现Eulers TotientFunction欧拉函数算法(附完整源码)
查看>>
Objective-C实现eulers totient欧拉方程算法(附完整源码)
查看>>
Objective-C实现EulersTotient欧拉方程算法(附完整源码)
查看>>
Objective-C实现eval函数功能(附完整源码)
查看>>
Objective-C实现even_tree偶数树算法(附完整源码)
查看>>
Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
查看>>
Objective-C实现exchange sort交换排序算法(附完整源码)
查看>>
Objective-C实现ExponentialSearch指数搜索算法(附完整源码)
查看>>
Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
查看>>
Objective-C实现ExtendedEuclidean扩展欧几里德GCD算法(附完整源码)
查看>>
Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
查看>>
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现factorial阶乘算法(附完整源码)
查看>>
Objective-C实现Fast Powering算法(附完整源码)
查看>>
Objective-C实现fenwick tree芬威克树算法(附完整源码)
查看>>
Objective-C实现FenwickTree芬威克树算法(附完整源码)
查看>>