Spring-ioc-aop-tx 整合练习

目录

0.项目需求

1.项目目录

2.所需依赖

3.config配置层

4.pojo实体表数据

5.dao持久层

6.service业务层

7.controller控制层

8.aop_advice增强层

9.test测试层

10.运行结果

10.1 仅添加 IoC 结果

10.2 只添加 IoC 和 AoP 结果

10.3 全部添加IoC-AoP-Tx 结果


0.项目需求

基于spring实现对数据库表的查询

1.项目目录

Spring-ioc-aop-tx 整合练习_第1张图片

2.所需依赖


        
        
            org.springframework
            spring-context
            5.3.23
        

        
        
            org.junit.jupiter
            junit-jupiter-api
            5.3.1
        


        
        
            mysql
            mysql-connector-java
            8.0.30
        

        
            com.alibaba
            druid
            1.2.8
        

        
        
            org.springframework
            spring-jdbc
            5.3.18
        

        
            org.springframework
            spring-test
            5.3.27
            test
        

        
        
            org.springframework
            spring-tx
            5.3.24
        


        
        
            org.springframework
            spring-aspects
            6.0.9
        

    

3.config配置层

package com.atjialeten.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration  // 声明配置类
@ComponentScan(basePackages = "com.atjialeten")  // 包组件扫描
@PropertySource(value = "classpath:jdbc.properties")  // 引入外部配置文件
@EnableAspectJAutoProxy  // 开启aop注解aspectj的支持
@EnableTransactionManagement  // 开启事务
public class JavaConfig {

    // DruidDataSource
    @Bean
    public DruidDataSource dataSource(
            @Value("${atjiale.url}") String url,
            @Value("${atjiale.driver}") String driver,
            @Value("${atjiale.username}") String username,
            @Value("${atjiale.password}") String password
    ){

        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

	// JdbcTemplate
    @Bean
    public JdbcTemplate jdbcTemplate(DruidDataSource dataSource){
        JdbcTemplate jdbcTemplate =new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    // 事务配置,装配事务管理实现对象,之后直接在核心方法上声明事务即可
    @Bean
    public TransactionManager transactionManager(DruidDataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }


}

4.pojo实体表数据

package com.atjialeten.pojo;

public class Students {

    private Integer id;
    private String name;
    private String gender;
    private Integer age;
    private String classes;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getClasses() {
        return classes;
    }

    public void setClasses(String classes) {
        this.classes = classes;
    }

    @Override
    public String toString() {
        return "Students{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", classes='" + classes + '\'' +
                '}';
    }
}

5.dao持久层

package com.atjialeten.dao.impl;

import com.atjialeten.pojo.Students;
import com.atjialeten.dao.StudentDao;
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 StudentDaoImpl implements StudentDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List queryAll() {

        String sql = "select id,name,age,gender,class as classes from students";

        List query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Students.class));

        return query;
    }
}

6.service业务层

package com.atjialeten.service.impl;

import com.atjialeten.pojo.Students;
import com.atjialeten.service.StudentService;
import com.atjialeten.dao.StudentDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    public List findAll() {

        List studentList = studentDao.queryAll();

        return studentList;
    }
}

7.controller控制层

   
package com.atjialeten.controller;

import com.atjialeten.pojo.Students;
import com.atjialeten.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Controller
public class StudentContorller {

    @Autowired
    private StudentService studentService;

    // 事务属性设置
    @Transactional(readOnly = false,
                    timeout = -1,
                    rollbackFor = Exception.class,
                    isolation = Isolation.READ_COMMITTED,
                    propagation = Propagation.REQUIRED
    )
    public void findAll(){
        List all = studentService.findAll();
        System.out.println("Spring-ioc-aop-tx整体练习,学生数据All = "+all);
    }
}

8.aop_advice增强层

package com.atjialeten.aop_advice;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/*
    AOP 的实现:
        1.编写增强类
        2.编写增强方法
        3.定位切点(定位横切到核心方法的位置)
        4.开启 aop
 */

@Component //  加入 ioc
@Aspect  // 切面配置
public class StudentControllerTenAdvice {

    // 提取切点表达式
    @Pointcut("execution(* com.atjialeten.controller.StudentContorller.findAll())")
    public void pointCut(){}

    @Before("pointCut()")
    public void start(){
        System.out.println("......非核心代码切入......");
        System.out.println("开始连接数据库查询学生数据");
    }

    @AfterReturning("pointCut()")
    public void after(){
        System.out.println("数据库查询结束,感谢使用!");
    }

}

9.test测试层

package com.atjialeten.test;

import com.atjialeten.controller.StudentContorller;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

@SpringJUnitConfig(com.atjialeten.config.JavaConfig.class)
public class TenTest {

    @Autowired
   private StudentContorller studentContorller;

    @Test
    public void test(){
        studentContorller.findAll();
    }


}

10.运行结果

10.1 仅添加 IoC 结果

核心方法结果
在这里插入图片描述

10.2 只添加 IoC 和 AoP 结果

aop:对核心方法添加非核心方法,前置和后置,提高可读性
Spring-ioc-aop-tx 整合练习_第2张图片

10.3 全部添加IoC-AoP-Tx 结果

先执行事务,再执行非核心代码的操作,最后运行核心代码
Spring-ioc-aop-tx 整合练习_第3张图片

你可能感兴趣的:(Spring,java,spring)