AOP(面向切面编程) 及Mybatis简介

本文详细介绍了AOP(面向切面编程)的核心概念,包括AOP与OOP的区别、AspectJ、切入点、通知类型以及Spring AOP的XML和注解配置方式。同时,文章还阐述了Spring声明式事务的配置,并给出了基于XML和注解的示例。此外,文章还简要探讨了Mybatis的基本依赖和两种方式(XML与注解)的映射SQL,包括数据库配置、Mapper接口和配置文件等内容。

文章目录

AOP(面向切面编程)

AOP和OOP的比较

OOP语言提供了类与类之间纵向的关系(继承、接口),而AOP补充了横向的关系(比如在不改变目标类中源代码的情况下给com.john.demo.dao包下所有类中以insert和update开头的方法添加事务管理)

AspectJ(切面)

我们将自己需要插入到目标业务逻辑中的代码模块化, 通过AOP使之可以横切多个类的模块,称之为切面。

在Spring AOP配置中切面通常包含三部分:

  • 切面模块本身
  • 通知
  • 切入点

示例:

<!-- 在beans标签中配置命名空间aop -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

   
<!-- 目标业务逻辑代码 -->
<bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>

<!-- 切面模块化对象(代表我们要附加到原始业务逻辑中的代码) -->
<bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />

<!-- 示例说明: 将切面calcAspect中的代码插入到calc原始业务代码中 -->
<aop:config>
    <!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
    <aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
    <aop:aspect ref="calcAspect">
        <!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
        <aop:around method="computeTime" pointcut-ref="all_calc_method" />
    </aop:aspect>
</aop:config>

</beans>

在pom.xml文件中添加依赖

<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>

切入点 (Pointcut)

在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。

AspectJ 指示器描述
execution ()用于匹配连接点的执行方法 最常用
args ()限制连接点的指定参数为指定类型的执行方法
@args ()限制连接点匹配参数类型由指定注解标注的执行方法
this ()限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类
target ()限制连接点匹配特定的执行对象,目标对象是指定的类型
@target ()限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解
within()限制连接点匹配指定类型,比如哪个包下,或哪个类里面
@within()限制连接点匹配指定注释所标注的类型(当使用 Spring AOP 时,方法定义在由指定的注解所标注的类里)
@annotation限制匹配带有指定注释的连接点

通知(Advice)

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="calc" class="com.lanou3g.spring.simple.calc.CalcImpl"/>

    <bean id="calcAspect" class="com.lanou3g.spring.simple.calc.CalcAspect" />

    <aop:config>
        <aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.simple.calc.CalcImpl.*(..))" />
        <aop:aspect ref="calcAspect">
            <aop:around method="aroundM" pointcut-ref="all_calc_method" />
            <aop:before method="beforeM" pointcut-ref="all_calc_method" />
            <aop:after-returning method="afterReturningM" pointcut-ref="all_calc_method" returning="retVal" />
            <aop:after-throwing method="afterThrowing" pointcut-ref="all_calc_method" throwing="throwable" />
            <aop:after method="afterFinallyM" pointcut-ref="all_calc_method" />
        </aop:aspect>
    </aop:config>

</beans>

配置logback.xml

<configuration>

    <property name="HOME_LOG" value="logs"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="RollingFile"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>

        <!-- 测试部署时使用如下配置 -->
        <!-- 可让每天产生一个日志文件,最多 30 个,更早的删除 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${HOME_LOG}/log-%d{yyyy-MM-dd}.log
            </fileNamePattern>
            <maxHistory>10</maxHistory>
        </rollingPolicy>

        <!--
             RollingFileAppender 一般情况下需要配置两个参数:
             RollingPolicy,负责滚动。TriggeringPolicy,决定是否以及何时进行滚动
             TimeBasedRollingPolicy比较特殊,它同时继承了RollingPolicy和TriggerPolicy。

        -->
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
                %msg%n
            </pattern>
        </encoder>

        <!-- 正式部署时使用此配置 -->
        <!--
            <file>${app.home}/logs/log.log</file>
            <append>true</append>
            <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
                <fileNamePattern>${app.home}/logs/log.%i.log.zip
                </fileNamePattern>
                <minIndex>1</minIndex>
                <maxIndex>7</maxIndex>
            </rollingPolicy>

            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger -
                    %msg%n
                </pattern>
            </encoder>

            <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
                <maxFileSize>50MB</maxFileSize>
            </triggeringPolicy>
         -->
    </appender>

    <logger name="com.lanou3g.spring" level="DEBUG"/>
    <logger name="org.springframework" level="ERROR"/>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="RollingFile" />
    </root>
</configuration>

环绕通知(around)

  • 在目标方法执行前、后被通知, 可以获取连接点对象(ProceedingJoinPoint, 该对象可以获取被拦截方法的签名、参数、返回值、包括调用与否)
  • 该方法的返回值,即代表了真正业务逻辑代码的返回值
  • 可以选择终止或正常执行目标方法

前置通知(before)

在目标方法调用前通知切面, 什么参数也无法获取。也不能终止目标方法执行

后置(返回值)通知(after returning)

只有在目标方法 正常 执行结束后才会通知, 在通知方法中可以获取到方法的返回值

后置(最终)通知 (after)

在目标方法执行结束后通知切面, 什么参数也无法获取。无论目标方法是正常执行结束还是抛出异常终止,都会被通知

异常通知(after throwing)

只有在目标方法 出现异常 才会通知, 在通知方法中可以获取到抛出的异常信息

连接点(JoinPoint)

连接点有很多种,比如方法执行期间(开始执行、执行结束、抛出异常)、字段修饰符、字段值被更改…

在Spring AOP中只支持方法连接点(因为Spring AOP底层是通过动态代理实现的)。

连接点与切入点的关系可以简单理解为: 切入点一定是连接点, 连接点不一定是切入点。

织入(Weaver)

织入的过程其实就是Spring AOP帮我们把切面中的代码织入到目标代码中的过程。

用XML方式启用Spring AOP

添加依赖

首先我们需要先将aspectJ的依赖导入maven项目中

<dependency>           
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.9.4</version>
</dependency>

使用示例

这里给出一个需要被织入的Java类的示例

public class CalcImpl implements Calc {
    /**
    * 加法
    */
    @Override
    public int add(int num1, int num2) {
        return num1 + num2;
    }
	/**
    * 减法
    */
    @Override
    public int subtract(int minuend, int subtrahend) {
        return minuend - subtrahend;
    }
    /**
    * 乘法
    */
    @Override
    public int multiply(int num1, int num2) {
        return num1 * num2;
    }
    /**
    * 除法
    */
    @Override
    public int divide(int dividend, int divisor) {
        return dividend / divisor;
    }
}

以及切面类

@Slf4j//该注解用于下面使用log输出(不想使用log的换成控制台输出语句即可)
public class CalcAspect {

    public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取连接点代表的方法的签名
        Signature signature = joinPoint.getSignature();
        // 获取目标方法名
        String methodName = signature.getName();
        // 获取目标方法的参数
        Object[] args = joinPoint.getArgs();
        log.debug("[aroundM] ---- 目标方法[" + methodName + "(" + Arrays.toString(args) + ")]开始执行");
        long start = System.currentTimeMillis();
        // 调用目标方法
        Object retVal = joinPoint.proceed();
        // 插入公共需要的逻辑代码
        long timer = System.currentTimeMillis() - start;
        log.debug("[aroundM] ---- 目标方法[" + methodName + "(" + Arrays.toString(args) + ")]" +
                "执行结束,返回值: " + retVal + ",耗时: " + timer + "ms.");
        // 正常返回目标方法的返回值
        return retVal;
    }
    /**
    * 前置通知
    */
    public void beforeM(){
        log.debug("[beforeM] ---- 目标方法开始执行");
    }
    /**
    * 后置(返回值)通知
    */
    public void afterReturningM(Object retVal){
        log.debug("[afterReturningM] ---- 目标方法执行结束,返回值: " + retVal);
    }
    /**
    * 后置(最终)通知
    */
    public void afterFinallyM(){
        log.error("[afterFinallyM] ---- 方法执行结束");
    }
    /**
    * 异常通知
    */
    public void afterThrowing(Throwable throwable){
        log.error("[afterThrowing] ---- 方法执行出错", throwable);
    }

}

准备工作做完了,就需要在spring的配置文件中进行配置了

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 创建两个类的bean对象 -->
    <!-- 目标业务逻辑代码 -->
    <bean id="calc" class="com.spring.demo.CalcImpl"/>
    <!-- 切面模块化对象(代表我们要附加到原始业务逻辑中的代码) -->
    <bean id="calcAspect" class="com.spring.demo.CalcAspect"/>
    
    <!-- 示例说明: 将切面calcAspect中的代码插入到calc原始业务代码中 -->
	<!-- 使用aop进行配置,需要在文件的头部加入aop schema-->
    <aop:config>
        <!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
        <aop:pointcut id="all_calc_method" expression="execution(* com.lanou3g.spring.CalcImpl.*(..))"/>
        <!-- 引用外部定义的aspect bean -->
        <aop:aspect ref="calcAspect">
            <!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
            <!-- 前置通知 -->
            <aop:before method="beforeM" pointcut-ref="all_calc_method"/>
            <!-- 环绕通知 -->
            <aop:around pointcut-ref="all_calc_method" method="aroundM"/>
            <!-- 最终通知 -->
            <aop:after method="afterFinallyM" pointcut-ref="all_calc_method"/>
            <!-- 后置(返回值)通知 -->
            <aop:after-returning method="afterReturningM" pointcut-ref="all_calc_method" returning="retVal"/>
            <!-- 异常通知 -->
            <aop:after-throwing method="afterThrowing" pointcut-ref="all_calc_method" throwing="throwable"/>
        </aop:aspect>
    </aop:config>
</beans>

最后通过获取Bean对象调用CalcImpl中的方法

/**
 * 测试spring aop
 */
public class App {
    
    public static void main(String[] args) {
        ApplicationContext config = new ClassPathXmlApplicationContext("applicationConfig.xml");//配置文件换成你自己的
        Calc calc =  config.getBean("calc",Calc.class);
        calc.add(800000000,900000000);
        calc.subtract(894739429,482942849);
        calc.multiply(843438,834993);
        calc.divide(843822,223);
    }
}

用注解的方式启用Spring AOP

开启自动织入支持

在xml中开启

<!-- 开启aop注解支持 -->
<aop:aspectj-autoproxy />

<!-- 开启注解支持,同时强制指定代理机制为cglib -->
<aop:aspectj-autoproxy proxy-target-class="true" />

通过注解开启

/**
 * 纯注解方式入口类
 */
@Slf4j
@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.simple.say")
@EnableAspectJAutoProxy //开启对AOP相关注解的处理
public class AppByAnnotation {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByAnnotation.class);
        ISayHello hello = ctx.getBean(ISayHello.class);
        hello.sayHello("JinSaiSai");
    }
}

定义切面类,添加@Aspect注解,@Component注解

由于@Aspect注解没有让Spring作为组件bean扫描的能力,所以我们需要额外添加@Component注解

/**
 * 定义一个切面,负责收集方法调用的入参、出参(返回值)
 */
@Slf4j
@Aspect     // 表示该类是一个切面
@Component  // Aspect切面首先必须也是一个普通的bean
public class MethodInOutAspect {

    // 指定该方法是一个环绕通知,通知注解的参数代表引用一个切入点表达式
    @Around("com.lanou3g.spring.GlobalPointcut.say_all_method()")
    public Object aroundM(ProceedingJoinPoint joinPoint) throws Throwable {

        // 获取连接点方法的名称
        String methodName = joinPoint.getSignature().getName();

        // 获取连接点方法的参数
        Object[] args = joinPoint.getArgs();

        log.debug("[aroundM] "+methodName+"("+ Arrays.toString(args) +") 开始执行");
        Object retuVal = joinPoint.proceed();
        log.debug("[aroundM] "+methodName+"("+ Arrays.toString(args) +") 返回值: " + retuVal);
        return retuVal;
    }

    @AfterReturning(pointcut = "com.lanou3g.spring.GlobalPointcut.say_all_method()", returning = "ret")
    public Object afterRM(Object ret) {
        log.debug("[afterRM] 返回值: " + ret);
        return ret;
    }

}

定义切入点(通过方法+注解)

/**
 * 定义系统中所有用到的切入点表达式
 */
@Component
public class GlobalPointcut {

    /**
     * 通过@Pointcut注解定义切入点表达式
     * 此处表达式含义:拦截com.lanou3g.spring.simple.say包下所有类(包括子包中所有类)中的所有方法
     */
    @Pointcut("execution(* com.lanou3g.spring.simple.say..*.*(..))")
    public void say_all_method() {}

}

定义通知(方法+注解)

在 MethodInOutAspect.java里写一个方法afterRM

@AfterReturning(pointcut = "com.lanou3g.spring.GlobalPointcut.say_all_method()", returning = "ret")
    public Object afterRM(Object ret) {
        log.debug("[afterRM] 返回值: " + ret);
        return ret;
    }

AOP的应用-通过xml方式和注解方式配置Spring声明式事务

通过xml方式配置Spring声明式事务

bean层实体类Teacher.java


@Getter
@Setter
public class Teacher {
    private Integer id;
    private String tname;

    public Teacher() {
    }

    public Teacher(Integer id, String tname) {
        this.id = id;
        this.tname = tname;
    }

    public Teacher(String tname) {
        this.tname = tname;
    }
}

dao层实现类TeacherDaoImpl.java

@Setter
@Getter
public class TeacherDaoImpl {

    private JdbcTemplate jdbcTemplate;

    /**
     * 查询teacher表所有数据
     * @return
     */
    public List<Teacher> queryAll() {
       List<Teacher> teachers = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
       return teachers;
    }

    /**
     * 插入teacher
     * @param teacher
     * @return 返回影响行数
     */
    public int insertTeacher(Teacher teacher) {
        // 通过伪代码演示如果我们手写事务控制代码的套路
        // setAutoCommit(false);
        // beginTransaction
        // try{
        int result = jdbcTemplate.update("insert into teacher (tname) values (?)", teacher.getTname());

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        int retVal = 9 / 0;
        // commit;
        // } catch (Exception e) {
        // rollback;
        // }
        //
        return result;
    }

}

数据库相关外部配置文件jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8

配置头文件,添加tx命名空间

<?xml version="1.0" encoding="UTF-8"?>
<!-- 通过xml配置声明式事务,我们需要添加tx命名空间 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
<!--开启注解支持并配置扫描包路径-->
<context:component-scan base-package="com.lanou3g.spring.transaction" />

<!--创建了一个JdbcTemplate类型的对象,JdbcTemplate是Spring提供给我们做jdbc CRUD增删改查
操作的类。将配置数据源创建的dataSource对象注入到jdbcTemplate对象中,那么我们的JdbcTemplate
知道要对哪个数据库进行增删改查的操作了。-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
<!--创建了一个TeacherDaoImpl类型的对象,将jdbcTemplate对象注入到TeacherDaoImpl对象中,那么TeacherDaoImpl对象知道了jdbcTemplate对象要对数据库进行哪些具体的操作了。-->
    <bean id="teacherDao" class="com.lanou3g.spring.transaction.dao.TeacherDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>

配置数据源

作用:连接数据库

数据源(DataSource)与数据库连接池(Database Connection Pool)的简单理解

 <!-- 第一步:配置数据源 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="${jdbc.url}" />
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="username" value="${jdbc.user}" />
        <property name="password" value="${jdbc.password}" />
        <property name="connectionProperties">
            <props>
                <prop key="characterEncoding">${jdbc.characterEncoding}</prop>
            </props>
        </property>
    </bean>

配置事务管理器

作用:告诉你我要往对这个数据库的相关操作加入事务了

创建txManager对象,将dataSource对象注入到txManager对象中,那么我们的事务管理器就知道了要管理的是与哪个数据库相关的事务了。

<!-- 第二步:初始化事务管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

配置AOP通知(advice)

作用:告诉你我要往操作这个数据库的什么方法加入什么事务

创建txAdvice对象,将txManager对象注入到txAdvice对象中。那么我们的通知对象就知道了要往操作相应数据库

的什么方法加入事务管理器中内置的哪些事务了。

 <!-- 第三步:配置事务AOP通知 -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="insert*" rollback-for="ArithmeticException" />
            <tx:method name="query*" read-only="true" />
        </tx:attributes>
    </tx:advice>

可以这样理解:Spring内置了一个切面类Aspect,里面包含了各种事务(例如rollback-for,read-only等),但是它处于闲置状态,没有哪一个事务被启用。配置事务AOP通知 (advice)相当于是激活了这个内置的切面类,并且指定了在什么方法加入什么具体的事务。然后再把这个切面类插入到下面的切入点,在切入点方法启用相应的事务。

配置AOP切入点

作用:让上面配置的这些事务生效,我要把事务插入到哪个切点,在哪里启用这些事务

<!-- 第四步:定义AOP配置(将上面的通知和表达式组装到一起) -->
    <aop:config>
        <aop:pointcut id="all_dao_method" expression="execution(* com.lanou3g.spring.transaction.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="all_dao_method" />
    </aop:config>

    <!-- 通过xml方式配置Spring声明式事务 配置结束 -->

xml方式入口类AppByTransactionXml.java

/**
 * 纯xml方式入口类
 */
@Slf4j
public class AppByTransactionXml {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("tx_conf.xml");
        testQuery(ctx);
       // testTransaction(ctx);
    }

    static void testQuery(ApplicationContext ctx) {
        TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
        List<Teacher> teacherList = teacherDao.queryAll();
        for(Teacher teacher : teacherList) {
            System.out.println("id: " + teacher.getId()+", tname: " + teacher.getTname());
        }
    }

    static void testTransaction(ApplicationContext ctx) {
        TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
        int rowEffect = teacherDao.insertTeacher(new Teacher("女儿国王"));
        System.out.println("影响了" + rowEffect + "行.");
    }
}

通过注解方式配置Spring声明式事务

配置数据源

对应xml方式中的"配置数据源"步骤。

@PropertySource此注解用于加载指定路径下的properties文件到环境中,然后我们就可以获取properties文件中定义的属性

@Component注解是一个通用注解,代表一个组件,可以用来标识所有希望让Spring管理的bean。

@Value(" j d b c . d r i v e r &quot; ) 获 取 p r o p e r t i e s 文 件 中 定 义 的 d r i v e r 属 性 值 , 相 当 于 有 一 个 g e t 方 法 拿 到 了 这 个 d r i v e r 属 性 值 。 @ V a l u e ( &quot; {jdbc.driver}&quot;)获取properties文件中定义的driver属性值,相当于有一个get方法拿到了这个driver属性值。 @Value(&quot; jdbc.driver")propertiesdrivergetdriver@Value("{jdbc.url}"),@Value(" j d b c . u s e r &quot; ) , @ V a l u e ( &quot; {jdbc.user}&quot;),@Value(&quot; jdbc.user")@Value("{jdbc.password}"),@Value("${jdbc.characterEncoding}")与之类似。

/**
 * 配置数据源
 * (连接数据库)
 */
@PropertySource("classpath:jdbc.properties")
@Component
public class MyDataSource extends DriverManagerDataSource{
    public MyDataSource(@Value("${jdbc.driver}") String driver, @Value("${jdbc.url}") String url, @Value("${jdbc.user}") String userName, @Value("${jdbc.password}") String password, @Value("${jdbc.characterEncoding}") String characterEncoding) {
        super.setDriverClassName(driver);
        super.setUrl(url);
        super.setUsername(userName);
        super.setPassword(password);
        Properties conProperties = new Properties();
        conProperties.setProperty("characterEncoding", characterEncoding);
        super.setConnectionProperties(conProperties);
    }
}

开启事务相关注解支持

@Configuration

@EnableTransactionManagement

定义事务管理器bean

/**
  * 通过注解的方式配置Spring声明式事务
  * 也可以将事务管理器定义到xml中,注解事务会按类型自动找到xml中配置的事务管理器
  */
@Configuration
public class MyTransactionConf {
 /**
   * 定义TransactionManager bean
   * @param dataSource
   * @return
   */
    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

@Configuration 注解的类相当于xml配置文件中的

类上面必须添加此注解,否则类里面无法用@Bean注解创建对象。

@Bean注解允许我们通过注解的方式定义在Java代码中定义bean的配置元数据,

相当于xml配置文件中的

/**
 * 纯注解方式入口类
 */
@Slf4j  
//日志相关注解
@Configuration
//相当于xml配置文件中的<beans />
@ComponentScan(basePackages = "com.lanou3g.spring.transaction.annotation")
//配置扫描包路径
@EnableTransactionManagement
// 开启事务相关注解支持
public class AppByTransactionAnnotation {

    /**
     * 创建JdbcTemplate对象,Spring给我们封装了所有的JDBC操作
     * 将DataSource对象注入到JdbcTemplate对象中,那么我们的JdbcTemplate
     * 对象知道要对哪个数据库进行增删改查的操作了。
     * @param dataSource
     * @return
     */
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 创建事务管理器对象(告诉你我要往哪里加入事务)
     * 将DataSource对象注入到PlatformTransactionManager对象中,那么
     * PlatformTransactionManager对象就知道了要往对哪个数据库的操作
     * 加入事务管理器中内置的事务了。
     * @param dataSource
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByTransactionAnnotation.class);
        testQuery(ctx);
       //testTransaction(ctx);
    }

    static void testQuery(ApplicationContext ctx) {
        TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
        List<Teacher> teacherList = teacherDao.queryAll();
        for(Teacher teacher : teacherList) {
            System.out.println("id: " + teacher.getId()+", tname: " + teacher.getTname());
        }
    }

    static void testTransaction(ApplicationContext ctx) {
        TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
        int rowEffect = teacherDao.insertTeacher(new Teacher("周伯通"));
        System.out.println("影响了" + rowEffect + "行.");
    }

}

在需要事务的方法上添加事务注解

要了解@Repository的作用,我们得先了解@Component的作用。

@Component、@Repository、@Service、@Controller

@Component注解是一个通用注解,代表一个组件,可以用来标识所有希望让Spring管理的bean。

@Repository、@Service、@Controller三个注解扩展自@Component,带有具体的语义色彩,专门用于修饰dao、service、controller层的类

上面这几个注解都可以通过给定value参数来指定bean的name,如果没有给定参数。那将由Spring的AnnotationBeanNameGenerator来自动生成(其实底层就是调用了java.beans.Introspector#decapitalize()方法来实现)

@Autowired自动注入。根据依赖的类型自动注入。该注解可以用于标注在 属性、setter方法、构造方法、或带参数的普通方法之上。Spring会将符合的参数自动注入到属性、或方法参数中。

@Repository相当于创建了一个TeacherDaoImpl对象,入口类就可以用这个对象调用类TeacherDaoImpl里面的方法了。

@Autowired相当于创建了一个JdbcTemplate对象,这个对象就可以用来对相应的数据库进行增删改查的操作了。

// 此注解和@Component作用一样, 只是含有特定的语义(一般用来标注dao层的类)
@Setter
@Getter
@Repository
public class TeacherDaoImpl {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**DataSource对象已经注入到了JdbcTemplate对象中,就是说我们的JdbcTemplate
     * 对象已经知道要对哪个数据库进行增删改查的操作了。
     */
    
    /**
     * 查询teacher表所有数据
     * @return
     */
    // 通过@Transactional 开启事务、设置事务属性
    @Transactional(readOnly = true)
    public List<Teacher> queryAll() {
       List<Teacher> teachers = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
       return teachers;

        //int rows = jdbcTemplate.update("delete from teacher;");
        //System.out.println("影响了" + rows + "行.");
        //return null;
    }

    /**
     * 插入teacher
     * @param teacher
     * @return 返回影响行数
     */
    // 通过@Transactional 开启事务、设置事务属性
    @Transactional(rollbackFor = {ArithmeticException.class})
    public int insertTeacher(Teacher teacher) {
        // 通过伪代码演示如果我们手写事务控制代码的套路
        // setAutoCommit(false);
        // beginTransaction
        // try{
        int result = jdbcTemplate.update("insert into teacher (tname) values (?)", teacher.getTname());
//        int retVal = 9 / 0;
        // commit;
        // } catch (Exception e) {
        // rollback;
        // }
        //
        return result;
    }

}

Mybatis

MyBatis框架概述

mybatis概述

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的XML或注解来配置和映射原生信息,将接口和java的POJO(Plain Old Java Objects,普通老式java对象)映射成数据库中的记录。

mybatis基础依赖

 <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
			<version>3.5.1</version>
        </dependency>
    </dependencies>

基于XML方式映射SQL

定义数据库相关外部配置文件jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8

定义 MyBatis 系统核心设置mybatis_conf.xml

配置文件中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。当然,还有很多属性可以在 XML 文件中进行配置,下面的示例指出的则是最关键的部分。 要注意 XML 头部的声明,它用来验证 XML 文档正确性。environment 元素体中包含了事务管理和连接池的配置。mappers 元素则是包含一组映射器(mapper),这些映射器的 XML 映射文件包含了 SQL 代码和映射定义信息。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties" />
    <typeAliases>
        <typeAlias type="com.lanou3g.mybatis.bean.Teacher" alias="Teacher" />
    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.user}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/TeacherMapper.xml" />
    </mappers>
</configuration>

定义XML SQL配置文件TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper
                PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


        <!-- namespace对应空Dao接口的全名 -->
<mapper namespace="com.lanou3g.mybatis.dao.TeacherDao">
<!-- 此处的id是查询语句的名称,对应接口中的方法名 -->
<select id="queryAll" resultType="Teacher">
    select * from teacher;
  </select>
</mapper>

定义对应的Mapper接口TeacherDao.java

public interface TeacherDao {

    List<Teacher> queryAll();

}

定义入口类App.java

@Slf4j
public class App {
    public static void main(String[] args) throws Exception {

        // 1. 读入配置文件
        String confPath = "mybatis_conf.xml";
        InputStream in = Resources.getResourceAsStream(confPath);

        // 2. 构建SqlSessionFactory(用于获取sqlSession)
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);

        // 3. 获取sqlSession对象(用于具体的CRUD)
        SqlSession sqlSession = sessionFactory.openSession();
        
        TeacherDao teacherDao = sqlSession.getMapper(TeacherDao.class);
        System.out.println(teacherDao.queryAll());

    }
}

基于注解方式映射SQL

mybatis最初设计时就是一个基于XML驱动的框架。虽然从Mybatis3开始Mybatis支持了很多基于注解方式的配置,但从实际使用来看,也仅适用于比较简单的场景下。像一些比较复杂的结果集映射用注解写出来可读性和可维护性就很低。

上面并不是说注解配置就一无是处, 比如一些简单的单表CRUD语句我们就可以直接通过注解来配置, 而不在需要单独写一个XML映射文件。

定义数据库相关外部配置文件jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/yanfa5
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
jdbc.characterEncoding=utf8

定义 MyBatis 系统核心设置mybatis_conf.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="jdbc.properties" />
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.user}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper class="com.lanou3g.mybatis.dao.StudentDao" />
    </mappers>
</configuration>

定义对应的Mapper接口StudentDao.java

public interface StudentDao {

    @Select("select sname, nick_name as nickName from student;")
    List<Student> queryAll();
}

定义入口类App.java

@Slf4j
public class App {
    public static void main(String[] args) throws Exception {

        // 1. 读入配置文件
        String confPath = "mybatis_conf.xml";
        InputStream in = Resources.getResourceAsStream(confPath);

        // 2. 构建SqlSessionFactory(用于获取sqlSession)
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);

        // 3. 获取sqlSession对象(用于具体的CRUD)
        SqlSession sqlSession = sessionFactory.openSession();

        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> studentList = studentDao.queryAll();
       log.info("studentList: " + studentList);
        //System.out.println(studentList);
    }
}
<think>好的,我现在需要帮用户检查一下他们提供的这句话有没有问题。首先,用户提到自己具备Spring Boot、Spring MVC和MyBatis的实战经验,接着是深入理解Spring Bean的生命周期机制,掌握自动配置原理和AOP面向切面编程。 首先,我要逐个检查技术术语的准确性。Spring Boot、Spring MVC和MyBatis都是正确的框架名称,没有问题。接下来是Spring Bean的生命周期机制,这里应该用“生命周期”而不是“生命周期机制”,不过可能用户是想强调机制,但通常我们会说“生命周期”。不过这里可能不算错误,但可以优化。 然后,自动配置原理,这里没问题,Spring Boot的自动配置是其核心特性之一。AOP面向切面编程,这里有点重复,因为AOP本身就是面向切面编程的缩写,所以正确的说法应该是“AOP面向切面编程)”或者直接说“面向切面编程AOP)”。用户这里用了“AOP面向切面编程”,虽然不算错误,但有点冗余,可以建议调整结构。 接下来,句子的结构问题。原句中的“深入理解Spring Bean生命周期机制,掌握自动配置原理与AOP面向切面编程”这部分,动词的使用是否恰当?“深入理解”和“掌握”都是合适的动词,但可能需要统一结构,比如都用“深入理解”或者“掌握”,不过分开使用也可以接受。 另外,标点符号方面,各部分之间用句号分隔是正确的,但可以考虑使用分号来连接相关的内容,使句子更流畅。例如,把“具备...实战经验。深入理解...”改为分号,但句号也是可以的,看用户偏好。 然后,是否存在技术表述不准确的地方?比如,Spring Bean的生命周期确实是Spring框架的核心内容,自动配置是Spring Boot的特性,AOP是Spring的一个模块,这些都是正确的。 总结问题点:主要是术语的准确性和表达的冗余。比如“AOP面向切面编程”可以优化,以及“生命周期机制”可能调整为“生命周期”。另外,句子结构可以稍微调整,使其更流畅。 可能的修改建议:将“AOP面向切面编程”改为“AOP面向切面编程)”,或者调整顺序。同时,检查是否有更合适的动词搭配,确保各部分表达清晰准确。 最后,确认用户的需求是检查简历或技术描述中的语句,所以需要确保专业性和准确性,同时避免冗余,使内容更精炼。可能需要建议用户将部分术语用括号补充说明,或者调整结构,使阅读更顺畅。</think>您提供的句子整体表述清晰,但在技术细节和表达方式上有以下可优化之处: ### 一、技术表述优化建议 1. **术语规范性问题** `AOP面向切面编程` 存在冗余,建议改为: `AOP面向切面编程)` *理由*:AOP 是 Aspect-Oriented Programming 的缩写,括号补充中文全称更符合技术文档规范。 2. **技术归属区分** `自动配置原理` 需要明确技术来源,建议改为: `Spring Boot 自动配置原理` *理由*:自动配置是 Spring Boot 的核心特性,与 Spring Framework 的 Bean 生命周期属于不同层级的技术点。 3. **生命周期表述** `Spring Bean生命周期机制` 建议调整为: `Spring Bean 生命周期及其管理机制` *理由*:生命周期本身包含阶段定义,而「机制」更强调 Spring 容器的具体实现逻辑。 ### 二、句式结构调整建议 ```markdown 具备 Spring Boot、Spring MVC 及 MyBatis 等框架的实战经验。深入理解 Spring Bean 生命周期及其管理机制,掌握 Spring Boot 自动配置原理与 AOP面向切面编程)的实现与应用。 ``` ### 三、改进后优势 1. 技术归属更清晰(明确区分 Spring Framework 与 Spring Boot 的不同技术点) 2. 术语表达更专业(通过括号补充英文缩写全称) 3. 逻辑层次更分明(「机制-原理-实现」的递进关系) ### 四、扩展建议 若面向技术面试场景,可补充具体量化指标: `例如:通过自定义 BeanPostProcessor 实现 Bean 初始化耗时监控(具体应用场景),基于 @EnableAutoConfiguration 实现自定义 Starter(实践案例)` 这样的调整既保持了专业性,又通过技术细节增强了说服力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值