Spring的通知与数据库操作
-------------------------------------------------------------------------------------------------------------------------
一、Spring 通知(增强):
1、 类型:
1、前置通知(MethodBeforeAdvice) :在目标方法执行前实施增强;
2、后置通知(AfterReturningAdvice):在目标方法执行后实施增强;
3、环绕通知(MethodInterceptor):在目标方法执行前后实施增强;
4、异常抛出通知(ThrowsAdvice):在方法抛出异常后实施增强;
5、引介通知(IntroductionInterceptor):在目标类中添加一些新的方法和属性;
6、最终通知():不管是否出现异常都会执行。
2、使用AspectJ实现AOP(现在用此种方式)
需要导入架包:1、spring-aop-3.2.0.RELEASE.jar
2、com.springsource.org.aopalliance-1.0.0.jar
3、spring-aspects-3.2.0.RELEASE.jar
4、com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
实现步骤:
1、配置xml文件:
1、加命名空间: xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
2、开启AspectJ的自动代理: <aop:aspectj-autoproxy />
2、通知的注解:
@Before 前置通知
@AfterReturning 后置通知
@Around 环绕通知
@AfterThrowing抛出通知
@After 最终final通知
@DeclareParents 引介通知
3、增强类必须有 @Aspect注解,否则就并不是增强类。
注意:使用@Aspect注解并没有交给Spring容器去管理,所以必须要在xml配置文件中手动配置<bean>的增强类,Spring会根据@Aspect注解来标明此类为增强类。
4、增强类的增强方法通过execution函数,可以定义切点的方法切入---------------------》此函数加在增强类的方法上,括号的参数是增强到的目标类或者目标方法上。
语法要求:execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
1、匹配所有类public方法 execution(public * *(..))
2、匹配指定包下所有类方法 execution(* com.baidu.dao.*(..)) 不包含子包
3、execution(* com.baidu.dao..*(..)) ..*表示包、子孙包下所有类
4、匹配指定类所有方法 execution(* com.baidu.service.UserService.*(..))
5、匹配实现特定接口所有类方法 execution(* com.baidu.dao.GenericDAO+.*(..))
6、匹配所有save开头的方法 execution(* save*(..))
5、增强类的增强方法设置参数
1、前置通知传入JoinPoint对象,用来获得切点信息(传入目标对象)
2、后置对象传入JoinPoint对象和Object类型的res(res用于返回目标对象的的返回值)
3、环绕通知参数为ProceedingJoinPoint 可以调用拦截目标方法执行
Objext all=JoinPoint.proceed();------------------->环绕通知中的此方法返回的值为目标类的目标方法的返回值。
注意:如果不调用 ProceedingJoinPoint的 proceed方法,那么目标方法就被拦截了
4、抛出通知通过在注解上设置throwing属性,可以设置发生异常对象参数
@AfterThrowing(value="execution(* com.baidu.dao..*(..))",throwing="ox")
设置后在增强方法上写入参数
public void del(Throwable ox){
XXX---------处理程序
}
注意:切点方法:private void 无参数方法,方法名为切点名,当通知多个切点时可以用||连接。
TODO---》XML装配通知的方法
------------------------------------------------------------------------------------------------------------------------------
二、Spring操作数据库(JdbcTemplate技术)
1、Spring为各种支持的持久化技术,都提供了简单操作的模板和回调。
JDBC org.springframework.jdbc.core.JdbcTemplate
Hibernate3.0 org.springframework.orm.hibernate3.HibernateTemplate
IBatis(MyBatis) org.springframework.orm.ibatis.SqlMapClientTemplate
JPA org.springframework.orm.jpa.JpaTemplate
2、使用Spring JDBC是Spring提供的持久层技术
1、导入架包
1、spring-jdbc-3.2.0.RELEASE.jar
2、spring-tx-3.2.0.RELEASE.jar
还有数据库驱动的架包。
2、具体数据库操作(两种方式操作)
1、方法一:java代码实现:
// JDBC模板 依赖连接池获得数据库连接,所有必须先构造连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring");
dataSource.setUsername("root");
dataSource.setPassword("123");
// 创建JDBC模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
// 建表SQL语句
String sql = "create table customers(id int primary key auto_increment,name varchar(20))";
jdbcTemplate.execute(sql);
此案例证明SpringJDBC 模板的使用,必须依赖DataSource 数据库连接池。
实际开发中的常用数据源:
Spring 数据源实现类 DriverManagerDataSource
DBCP 数据源 BasicDataSource
C3P0 数据源 ComboPooledDataSource
2、方法二: 通过Spring配置文件来配置JdbcTemplate
在xml配置数据源生成数据库操作执行类(3种获取方式)
1、配置DBCP数据源
导入架包: com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
xml配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jdbcuser?useUnicode=true&characterEncoding=UTF-8"></property>
<property name="password" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
------------------------------------------------------------------------------------------------------------
2、使用properties文件配置数据库连接基本参数
properties文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/jdbcuser?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
xml配置:
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
------------------------------------------------------------------------------------------------------------
3、配置C3P0数据源
导入架包:
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
xml配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/jdbcuser?useUnicode=true&characterEncoding=UTF-8"></property>
<property name="password" value="root"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
------------------------------------------------------------------------------------------------------------
3、在dao层对数据库进行增删改查
使用 : private ClassPathXmlApplicationContext applicationContext = BeanllUtlis.getApplicationContext();
private JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
jdbcTemplate类会有增删改查的方法使用。
这里特别说一下查询操作:
在查询操作时返回的实体类集合和类对象时,必须new一个RowMapper<User>()接口实现其方法。
如:List<User> query = jdbcTemplate.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setTime(resultSet.getInt("time"));
user.setName(resultSet.getString("name"));
return user;
}
},"如根据id返回对象时此处可以带参数");