Aop 通知五种通知类型 (基于xml的形式)
前置通知:
后置通知:
异常抛出通知:
最终通知:
环绕通知:
总结: 后置通知和异常抛出通知, 只能执行其中的一个。 两个不可能同时执行。
环绕通知可以模拟以上四个通知。
2 aop通知基于注解式开发
开发步骤:
2.1 copy jar包: 4+2+4(AOP)
2.2 引入相关的约束:
2.3 定义接口:UserDao
2.4 定义接口的实现类:
2.5 定义一个切面(通知的集合)
@Component("myAspectAnto")
//告知spring 该类是一个切面类:
@Aspect
public class MyAspectAnto {
@Before(value="execution ( * com.yidongxueyuan.spring.dao.impl.*.*(..))")
public void checkPri() {
System.out.println("权限的校验");
}
}
2.6 使用注解配置:
ApplicationContext.xml当中:
<!-- 开启扫描器 -->
<context:component-scan base-package="com.yidongxueyuan"></context:component-scan>
<!-- 采用注解的方式:
支持注解形式的aop配置:
-->
<aop:aspectj-autoproxy/>
3 基于注解的其他通知
3.1 后置通知
@AfterReturning(value="execution ( * com.yidongxueyuan.spring.dao.impl.*.*(..))")
public void logger() {
System.out.println("日志的记录: ");
}
3.2 异常抛出通知
@AfterThrowing(value="execution ( * com.yidongxueyuan.spring.dao.impl.*.*(..))",throwing="e")
public void afterThrowing(Throwable e) {
System.out.println("异常抛出通知。。。"+e.getMessage());
}
3.3 最终通知:
无论有没有异常都会被触发。 使用注解的形式开发, 最终的通知和异常抛出通知的执行顺序。可能不是你想要的, 这是无法改变的。
后置通知和最终通知的执行顺序也不是你想要的。
环绕通知来解决这个问题。
@Test
public void test1() throws Exception {
userDao.saveUser(new User());
}
3.4 环绕通知
//环绕通知;
@Around(value="execution ( * com.yidongxueyuan.spring.dao.impl.*.*(..))")
public Object around(ProceedingJoinPoint joinPoint) {
Object obj=null;
try {
System.out.println("前置通知。。。");
obj = joinPoint.proceed();
System.out.println("后置通知。。。");
} catch (Throwable e) {
System.out.println("异常抛出通知。。。");
e.printStackTrace();
} finally {
System.out.println("最终通知");
}
return obj;
}
环绕通知: 环绕通知可以模拟其他的四种通知。 并且环绕通知需要进行业务方法的放行。 ProceedingJoinPoint 对象。 proceed可以进行业务方法的放行。
3.5 注解的形式定义一个通用的切点表达式: 其他的表达式可以引用该切点表达式、
package com.yidongxueyuan.spring.pojo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component("myAspectAnto")
//告知spring 该类是一个切面类:
@Aspect
public class MyAspectAnto {
@Pointcut(value="execution ( * com.yidongxueyuan.spring.dao.impl.*.*(..))")
private void point1() {}
/*@Before(value="point1()")
public void checkPri() {
System.out.println("权限的校验");
}
@AfterReturning(value="point1()")
public void logger() {
System.out.println("日志的记录: ");
}
@AfterThrowing(value="point1()",throwing="e")
public void afterThrowing(Throwable e) {
System.out.println("异常抛出通知。。。"+e.getMessage());
}
@After(value="point1()")
public void after() {
System.out.println("最终通知。。。");
}*/
//环绕通知;
@Around(value="point1()")
public Object around(ProceedingJoinPoint joinPoint) {
Object obj=null;
try {
System.out.println("前置通知1。。。");
obj = joinPoint.proceed();
System.out.println("后置通知1。。。");
} catch (Throwable e) {
System.out.println("异常抛出通知1。。。");
e.printStackTrace();
} finally {
System.out.println("最终通知1");
}
return obj;
}
}
4 JdbcTemplate对象
4.1 概述
Spring一站式框架, 在各个层次都提供了解决方法; dao层: 提供了一个技术:
JDBCTemplete对象。 对jdbc的封装, 使用方式跟DBUtils相似。
4.2 JdbcTemplate的入门案例
(1)引入jar包:
(2)引入约束:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
(3)开发:
开发步骤:
0:准备数据源
A:创建一个对象: 注入一个数据源。
B:直接调用对象的方法, 操作数据库:
代码:
public class TestJdbcTemplcate {
public static void main(String[] args) {
//准备数据源: spring自身提供的数据源:
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///customer");
dataSource.setUsername("root");
dataSource.setPassword("root");
//创建一个JdbcTemplate对象;
JdbcTemplate template = new JdbcTemplate(dataSource);
template.execute("insert into account (id,name, money)values(10,'wanlin',30)");
}
}
4.3 将spring数据源交给spring管理
4.3.1 配置数据源:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///customer"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
4.3.2 使用数据源;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestJdbcTemplcate {
@Autowired
private DriverManagerDataSource dataSource;
@Test
public void test1() throws Exception {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.execute("insert into account (id,name, money)values(11,'xxx',30)");
}
}
4.4 spring引入外部的数据源: c3p0
4.4.1 引入c3p0的jar包
4.4.2 核心配置文件当中配置
<!-- 配置外部的数据源: c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///customer"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
4.4.3 在测试类当中使用数据源:
@Autowired
private ComboPooledDataSource dataSource;
@Test
public void test1() throws Exception {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.execute("insert into account (id,name, money)values(12,'yyy',30)");
}
4.5 spring引入外部的数据源dbcp
4.5.1 导入dbcp相关的jar包:
和数据源相关的:
4.5.2 核心配置文件当中配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///customer"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
4.5.3 测试:
//使用dbcp数据源:
@Autowired
private DataSource dataSource;
@Test
public void test2() throws Exception {
JdbcTemplate template = new JdbcTemplate(dataSource);
template.execute("insert into account (id,name, money)values(13,'zzz',30)");
}
5:JdbcTemplate对象实现增删改查操作
package com.yidongxueyuan.spring.jdbctemplate;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yidongxueyuan.spring.pojo.Account;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUD {
@Autowired
private DataSource dataSource;
//添加操作:
@Test
public void test1() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
jt.update("insert into account (id,name,money)values(?,?,?)", 9,"xxx",30);
}
//更新操作:
@Test
public void test2() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
jt.update("update account set name=?,money=? where id=?", "XXX",40,9);
}
//删除操作:
@Test
public void test3() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
jt.update("delete from account where id=?", 9);
}
//以上操作是对数据库的写的操作;
@Test
public void test4() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
/**
* sql:执行的sql语句:
* args: sql语句的参数: 类型:Object[]
* rowMapper:RowMapper 接口类型, 用来封装结果的。 结果集处理器设计: 采用策略模式。
*/
//jt.query("select * from account where id =?", 1, rowMapper)
/*
* 参数的类型: 可变参数: 在JDK1.5之后使用。
*/
//jt.query(sql, rowMapper, args)
List<Account> query = jt.query("select * from account where id =?",new myRowMapper(), 1);
Account account = query.get(0);
System.out.println(account);
}
//根据名称查询;
@Test
public void test5() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
List<Account> list = jt.query("select * from account where name =?",new myRowMapper(), "aaa");
System.out.println(list.size());
for (Account account : list) {
System.out.println(account);
}
}
//列表查询;
@Test
public void test6() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
List<Account> list = jt.query("select * from account ",new myRowMapper());
System.out.println(list.size());
for (Account account : list) {
System.out.println(account);
}
}
//使用spring当中提供的结果处理器: id查
@Test
public void test7() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
List<Account> list = jt.query("select * from account where id=?", new BeanPropertyRowMapper(Account.class),1);
System.out.println(list.size());
for (Account account : list) {
System.out.println(account);
}
}
//使用spring当中提供的结果处理器: 列表查
@Test
public void test8() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
List<Account> list = jt.query("select * from account", new BeanPropertyRowMapper(Account.class));
System.out.println(list.size());
for (Account account : list) {
System.out.println(account);
}
}
//查询总记录数: select count(*) from account;
@Test
public void test9() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
// Integer queryForObject = jt.queryForObject("select count(*) from account", Integer.class);
Long records = jt.queryForObject("select count(*) from account", Long.class);
System.out.println(records);
}
@Test
public void test10() throws Exception {
JdbcTemplate jt = new JdbcTemplate(dataSource);
// Integer queryForObject = jt.queryForObject("select count(*) from account", Integer.class);
Long records = jt.queryForObject("select count(*) from account where name=?", Long.class, "aaa");
System.out.println(records);
}
}
/**
* 自定义结果集的处理:
* @author Mrzhang
*
*/
class myRowMapper implements RowMapper<Account>{
@Override
public Account mapRow(ResultSet rs, int arg1) throws SQLException {
//创建一个对象:
Account account = new Account();
//将rs结果集当中的数据封装到对象当中: '
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getFloat("money"));
return account;
}
}