栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring AOP+Spring JDBC

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring AOP+Spring JDBC

目录

见代码目录

步骤:

 com.hrbust.po.Student类

com.hrbust.dao.StudentDao接口

studentDaoImpl

com.hrbust.aspect.LogAspect

applicationContext.xml

com.hrbust.test.StuTest

见代码目录

步骤:
  1. 在eclipse下创建项目stuManagement
  2. 添加.jar包(参照chapter03)
  3. 在src下创建com.hrbust.po.Student类,包含属性:String类型stuNo,stuName和score(Integer)
  4. 在src下创建com.hrbust.dao.StudentDao接口及其实现类StudentDaoImpl,使用jdbcTemplate实现Student的增删改查操作
  5. 在src下创建com.hrbust.aspect.LogAspect切面,定义环绕通知myAround(),实现日志环绕记录功能。
  6. 在src下创建applicationContext.xml文件,文件配置参考chapter03
  7. 在src下创建com.hrbust.test.StuTest类,调用selectStu()、insertStu()方法。

 com.hrbust.po.Student类
package com.hrbust.po;

public class Student {
       String stuNo;
       String stuName;
       Integer score;

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getScore() {
        return score;
    }

    public void setScore(Integer score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNo='" + stuNo + ''' +
                ", stuName='" + stuName + ''' +
                ", score=" + score +
                '}';
    }
}

com.hrbust.dao.StudentDao接口
package com.hrbust.dao;

import com.hrbust.po.Student;

import java.util.List;

public interface StudentDao {
    public int insert(Student student);
    public List select();
}

studentDaoImpl
package com.hrbust.dao;

import com.hrbust.po.Student;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;

public class StudentDaoImpl implements StudentDao{
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
    public int insert(Student student) {
      String sql = "insert into students(stuNo,stuName,score) value(?,?,?)";
      Object[] obj = new Object[]{
        student.getStuNo(),
        student.getStuName(),
        student.getScore()
      };
      int num = this.jdbcTemplate.update(sql,obj);
      System.out.println("插入信息成功");
      return num;
    }

    public List select() {
        String sql = "select * from students";
        RowMapper rowMapper = new BeanPropertyRowMapper(Student.class);
        System.out.println("查询用户信息");
        return this.jdbcTemplate.query(sql,rowMapper);
    }
}

com.hrbust.aspect.LogAspect
package com.hrbust.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect
public class LogAspect {
    @Pointcut("execution( * com.hrbust.dao.StudentDaoImpl.*(..))")
    public void pointcut(){

    }

    @Around("pointcut()")
    public Object myAround(ProceedingJoinPoint point)throws Throwable{
        System.out.println("这是环绕通知:执行目标方法之前,模拟开启事务");
        Object object = point.proceed();
        System.out.println("这是环绕之后的部分:执行方法之后,模拟关闭事务");
        return object;
    }

}

applicationContext.xml


    
        
        
        
        
    
    
        
    

    
        
    
    
    

com.hrbust.test.StuTest
package com.hrbust.test;

import com.hrbust.dao.StudentDao;
import com.hrbust.po.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class StuTest {
    public static void main(String[] arg){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao studentDao = context.getBean("studentDao",StudentDao.class);
        Student student = new Student();
        student.setStuNo("1234");
        student.setStuName("小彤彤");
        student.setScore(100);
        List studentList=studentDao.select();
        for(Student s : studentList)
        {
            System.out.println(s);
        }
        studentDao.insert(student);
    }
}

敲码不易 点赞收藏的人有好运 期末必过

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/849920.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号