spring笔记2

发布于:2024-05-07 ⋅ 阅读:(21) ⋅ 点赞:(0)

一、基于xml的AOP实现

基于注解管理Bean,注解+扫描

<context:component-scan base-package="com.zhou.spring.aop.xml"></context:component-scan>
    <aop:config>
<!--        设置一个公共的切入点表达式-->
        <aop:pointcut id="pointCut" expression="execution(* com.zhou.spring.aop.xml.CalculatorImpl.*(..))"/>
<!--        将IOC容器中的某个bean设置为切面-->
        <aop:aspect ref="loggerAspect">
            <aop:before method="beforeAdviceMethod" pointcut-ref="pointCut"></aop:before>
            <aop:after method="afterAdviceMethod" pointcut-ref="pointCut"></aop:after>
            <aop:after-returning method="afterReturningAdviceMethod" pointcut-ref="pointCut" returning="result"></aop:after-returning>
            <aop:after-throwing method="afterThrowingAdvice" pointcut-ref="pointCut" throwing="ex"></aop:after-throwing>
            <aop:around method="aroundAdviceMethod" pointcut-ref="pointCut"></aop:around>
        </aop:aspect>
    </aop:config>

二、JDBCTemplate

1.配置JDBCTemplate

首先引入jdbc的property文件,接着设置数据源,JDBCTemplate是一个对象,需要ioc的管理


<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean  id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
    <bean class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

2.spring整合junit

指定测试类在spring的测试环境下执行,此时就可以通过注入的方式直接获取IOC容器中的bean
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")

3.jdbcTemplate实现查询功能

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jdbc.xml")
public class JDBCTemplateTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Test
    public void testInsert(){
        String sql="insert into t_user values(null,?,?,?,?,?)";
        jdbcTemplate.update(sql,"root","123",23,"女","123@qq.com");

    }
    @Test
    public void testGetUserById(){
        String sql="select * from t_user where id=?;";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), 41);
        System.out.println(user);
    }
    @Test
    public void testGetAllUser(){
        String sql="select * from t_user";
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
        users.forEach(System.out::println);
    }
@Test
    public void testGetCount(){
        String sql="select count(*) from t_user";
    Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
    System.out.println(count);
}

三、声明式事务

1.声明式事务的概念

编程式 自己写代码 实现功能
声明式 :通过 配置 框架 实现功能

2.基于注解的声明式事务

a>无事务的实现
Mysql中一个sql语句独占一个事务且自动提交。
b>实现事务功能
* 声明式事务的配置步骤:
* 1.配置事务管理器
* 2.开启事务的注解驱动
* 在需要被事务管理的方法上,添加@Transactional注解,该方法就会被事务管理
* Transactional注解标识的位置:
* 1.标识在方法上
* 2.标识在类上,则类中所有的方法都会被事务管理
<!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    开启事务的注解驱动
将使用@Transactional注解所标识的方法或类中所有的方法使用事务管理进行管理
transaction-manager属性设置事务管理器的id

-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

3.声明式事务的属性

a>只读readOnly

只有当当前事务中全部都是查询操作时才可用只读,才可从数据库层面去优化当前的操作。

b>超时timeout=-1(默认一直等)
eg.timeout=3,超时时间是3秒钟,如果超过3秒事务没有执行完,当前的事务回滚并抛出异常
c>回滚策略
声明式事务默认只针对运行时异常回滚,编译时异常不回滚。
可以通过 @Transactional 中相关属性设置回滚策略
  @Transactional(
//            noRollbackFor = ArithmeticException.class
            noRollbackForClassName = "java.lang.ArithmeticException"
    )
rollbackFor 属性:需要设置一个 Class 类型的对象
rollbackForClassName 属性:需要设置一个字符串类型的全类名
noRollbackFor 属性:需要设置一个 Class 类型的对象
rollbackFor 属性:需要设置一个字符串类型的全类名
d>事务的隔离级别
 
读未提交: READ UNCOMMITTED
允许 Transaction01 读取 Transaction02 未提交的修改。
读已提交: READ COMMITTED
要求 Transaction01 只能读取 Transaction02 已提交的修改。
可重复读: REPEATABLE READ
确保 Transaction01 可以多次从一个字段中读取到相同的值,即 Transaction01 执行期间禁止其它
事务对这个字段进行更新。
串行化: SERIALIZABLE
确保 Transaction01 可以多次从一个表中读取到相同的行,在 Transaction01 执行期间,禁止其它
事务对这个表进行添加、更新、删除操作。可以避免任何并发问题,但性能十分低下。
e>传播行为
//            使用的是被调用事务本身的事务
//            propagation = Propagation.REQUIRES_NEW
//            使用的是调用的事务,默认
            propagation = Propagation.REQUIRED

4.基于xml的声明式事务

<!--    配置事务通知,即事物的属性,注意:切入点是连接点的实现方式-->
<tx:advice id="tx" transaction-manager="transactionManager">
    <tx:attributes>
<!--        表示切入点表达式所对应的连接点的所有方法都会被事务管理-->
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:advisor advice-ref="tx" pointcut="execution(* com.zhou.spring.impl.*.*(..))"></aop:advisor>
</aop:config>
注意:基于xml实现的声明式事务,必须引入aspect的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.1</version>
</dependency>