今日内容
AOP的概述
AOP 的底层实现
Spring 的AOP
使用AspectJ 实现AOP
Spring JdbcTemplate 使用
1.2 Spring整合WEB项目.
1.2.1 Spring整合WEB项目:
步骤一:创建一个WEB项目,引入jar包.
步骤二:创建包结构,及包中类:
步骤三:引入Spring的配置文件:
步骤四:将Service和DAO配置到Spring的配置文件中:
<beanid="userService"class="cn.itcast.service.UserService">
<propertyname="userDao"ref="userDao"/>
</bean>
<beanid="userDao"class="cn.itcast.dao.UserDao"></bean>
步骤五:WEB层调用业务层,业务层调用DAO:
UserServlet的代码:
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 调用业务层:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserService userService = (UserService) applicationContext
.getBean("userService");
System.out.println("用户管理的WEB层的代码执行了...");
userService.save();
}
UserService的代码:
publicclass UserService { private UserDao userDao; publicvoid setUserDao(UserDao userDao) { this.userDao = userDao; } publicvoid save() { System.out.println("用户管理的业务层的类执行了..."); userDao.save(); } }
发现问题:
每次执行WEB层代码,都会创建一个Spring的工厂.如何在WEB项目中只初始化一个工厂?
* 在Servlet的init方法中创建工厂?不可行,一个Servlet有一个init方法.创建一次工厂.也会资源浪费.
* 将工厂的创建,放到ServletContext对象中.每次获取的时候,从ServletContext中获取!!!
* 使用ServletContextListener:
步骤六:引入一个Spring整合WEB项目的jar包
步骤七:配置Spring的核心监听器:
<!-- Spring的核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
步骤八:修改WEB的代码:
获得工厂:
* 1.直接从ServletContext中获得工厂.
* this.getServletContext().getAttribute(key);
* 2.使用Spring提供工具类获取工厂.
* WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
1.3 Spring和JUnit单元测试的整合:
1.3.1 Spring和Junit单元测试的整合:
步骤一:引入一个jar包:
步骤二:创建一个包结构,编写测试类.
* cn.itcast.test
* SpringDemo1
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value="classpath:applicationContext.xml")
publicclass SpringDemo1 {
@Resource(name="userService")
private UserService userService;
@Test
publicvoid demo1(){
userService.save();
}
}
步骤三:引入Junit的环境.
1.4 Spring的传统AOP
1.4.1 AOP的概述:
什么是AOP:
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码
AOP的底层实现:
代理机制:
AOP的优点:
性能监视、事务管理、安全检查、缓存
AOP的开发相关术语:
Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象
Weaving(织入):是指把增强应用到目标对象来创建新的代理对象的过程.
spring采用动态代理织入,而AspectJ采用编译期织入和类装在期织入
Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类
Aspect(切面): 是切入点和通知(引介)的结合
1.4.2 手动实现AOP:
JDK的动态代理(针对实现了接口的对象产生代理)
publicclass MyJdbProxy implements InvocationHandler{
private UserService userService;
public MyJdbProxy(UserService userService){
this.userService = userService;
}
public UserService createProxy(){
// 生成UserSErvice的代理:
UserService userServiceProxy = (UserService) Proxy.newProxyInstance(
userService.getClass().getClassLoader(), userService.getClass()
.getInterfaces(), this);
return userServiceProxy;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// 判断是否是save方法:
if("save".equals(method.getName())){
// 增强:
System.out.println("权限校验===========");
return method.invoke(userService, args);
}
return method.invoke(userService, args);
}
}
Cglib的动态代理(针对字节码进行增强,产生了目标类的子类对象)---了解
publicclass MyCglibProxy implements MethodInterceptor{
private CustomerService customerService;
public MyCglibProxy(CustomerService customerService){
this.customerService = customerService;
}
public CustomerService createProxy(){
// 创建核心类:
Enhancer enhancer = new Enhancer();
// 设置父类:
enhancer.setSuperclass(customerService.getClass());
// 设置回调:
enhancer.setCallback(this);
// 创建代理:
CustomerService customerServiceProxy = (CustomerService) enhancer.create();
return customerServiceProxy;
}
@Override
public Object intercept(Object proxy, Method method, Object[] arg,
MethodProxy methodProxy) throws Throwable {
if("delete".equals(method.getName())){
Object obj = methodProxy.invokeSuper(proxy, arg);
System.out.println("日志记录==========");
return obj;
}
return methodProxy.invokeSuper(proxy, arg);
}
}
1.4.3 Spring的传统的AOP:基于ProxyFactoryBean的方式的代理(不是特别好)
Spring中的通知的类型:
前置通知 org.springframework.aop.MethodBeforeAdvice
在目标方法执行前实施增强
后置通知 org.springframework.aop.AfterReturningAdvice
在目标方法执行后实施增强
环绕通知 org.aopalliance.intercept.MethodInterceptor
在目标方法执行前后实施增强
异常抛出通知 org.springframework.aop.ThrowsAdvice
在方法抛出异常后实施增强
引介通知 org.springframework.aop.IntroductionInterceptor
在目标类中添加一些新的方法和属性
Spring中的切面的类型:
Advisor : 代表一般切面,Advice本身就是一个切面,对目标类所有方法进行拦截.(不带有切入点的切面,默认增强类中所有方法)
PointcutAdvisor : 代表具有切点的切面,可以指定拦截目标类哪些方法.(带有切入点的切面)
IntroductionAdvisor : 代表引介切面,针对引介通知而使用切面(不要求掌握)
Spring的传统的AOP快速入门(不带有切入点的切面)
步骤一:引入AOP开发的相关的jar包.
步骤二:引入Spring的配置文件:
步骤三:创建包结构及接口和实现类:
* cn.itcast.aop.demo3
* ProductService
* ProductServiceImpl
步骤四:将业务层类配置到Spring中
<!-- 配置目标对象 -->
<beanid="productService"class="cn.itcast.aop.demo3.ProductServiceImpl"></bean>
步骤五:编写一个增强(前置增强):
publicclass MyBeforeAdvice implements MethodBeforeAdvice {
@Override
publicvoid before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println("前置增强=============");
}
}
步骤六:将增强配置到Spring中.
<!-- 配置前置增强 -->
<beanid="myBeforeAdvice"class="cn.itcast.aop.demo3.MyBeforeAdvice"/>
步骤七:配置生成代理:
基于ProxyFactoryBean配置:
<!-- 配置生成代理 -->
<beanid="productServiceProxy"class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- ProxyFactoryBean常用可配置属性
target : 代理的目标对象
proxyInterfaces : 代理要实现的接口
如果多个接口可以使用以下格式赋值
<list>
<value></value>
....
</list>
proxyTargetClass : 是否对类代理而不是接口,设置为true时,使用CGLib代理
interceptorNames : 需要织入目标的Advice
singleton : 返回代理是否为单实例,默认为单例
optimize : 当设置为true时,强制使用CGLib
-->
<propertyname="target"ref="productService"/>
<propertyname="proxyInterfaces"value="cn.itcast.aop.demo3.ProductService"/>
<propertyname="interceptorNames" value="myBeforeAdvice"/>
</bean>
步骤八:编写测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
publicclass SpringDemo3 {
@Resource(name="productServiceProxy") // 注意注入代理对象
private ProductService productService;
@Test
/**
* 传统方式:
*/
publicvoid demo1(){
productService.save();
productService.update();
productService.delete();
productService.find();
}
}
Spring的传统的AOP快速入门(带有切入点的切面)
步骤一:引入AOP开发的相关的jar包.
步骤二:引入Spring的配置文件:
步骤三:创建包结构及接口和类:
* cn.itcast.aop.demo4
* OrderService
步骤四:配置该类到Spring:
<bean id="orderService" class="cn.itcast.aop.demo4.OrderService"/>
步骤五:编写一个增强:
publicclass MyAroundAdvice implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕前增强===============");
// 执行目标方法:
Object obj = methodInvocation.proceed();
System.out.println("环绕后增强===============");
return obj;
}
}
步骤六:配置增强:
<bean id="myAroundAdvice" class="cn.itcast.aop.demo4.MyAroundAdvice"/>
步骤七:配置一个切面:
<beanid="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 正则表达式: . :代表任意字符 *:任意次数 -->
<propertyname="pattern" value=".*"/>
<propertyname="advice" ref="myAroundAdvice"/>
</bean>
步骤八:配置一个代理:
<beanid="orderServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<propertyname="target" ref="orderService"/>
<propertyname="proxyTargetClass" value="true"/>
<propertyname="interceptorNames" value="advisor"/>
</bean>
步骤九:编写测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
publicclass SpringDemo4 {
@Resource(name="orderServiceProxy")
private OrderService orderService;
@Test
publicvoid demo1(){
orderService.save();
orderService.update();
orderService.delete();
orderService.find();
}
}
基于ProxyFactoryBean的代理总结:
Spring会根据类是否实现接口采用不同的代理方式:
* 实现接口:JDK动态代理.
* 没有实现接口:CGLIB生成代理.
基于ProxyFactoryBean的方式生成代理的过程中不是特别理想:
* 配置繁琐,不利为维护.
* 需要为每个需要增强的类都配置一个ProxyFactoryBean.
1.4.4 Spring的传统的AOP:基于自动代理:
自动代理和基于ProxyFactoryBean代理方式比较:
自动代理基于BeanPostProcessor完成的代理.
* 在类的生成过程中就已经是代理对象.
基于ProxyFactoryBean方式代理:
* 先有目标对象,根据目标对象产生代理.
BeanNameAutoProxyCreator:基于Bean名称的自动代理
<!-- 基于Bean名称的自动代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<propertyname="beanNames"value="*Service"/>
<propertyname="interceptorNames"value="myBeforeAdvice"/>
</bean>
DefaultAdvisorAutoProxyCreator:基于切面信息的自动代理
<beanid="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<propertyname="advice" ref="myAroundAdvice"/>
<!-- <property name="pattern" value="cn\.itcast\.aop\.demo4\.OrderService\.save"/> -->
<propertyname="patterns" value="cn\.itcast\.aop\.demo4\.OrderService\.save,cn\.itcast\.aop\.demo3\.ProductService\.update"/>
</bean>
<!-- 配置基于切面信息自动代理 -->
<beanclass="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
1.5 Spring基于AspectJ的AOP的开发
1.5.1 AspectJ的概述:
什么是AspectJ:
AspectJ是一个面向切面的框架,它扩展了Java语言。AspectJ定义了AOP语法所以它有一个专门的编译器用来生成遵守Java字节编码规范的Class文件。
Spring中的AspectJ:
Spring为了简化自身的AOP的开发,将AspectJ拿过来作为Spring自身一个AOP的开发.
1.5.2 Spring的基于AspectJ的AOP的开发(注解)
步骤一:创建一个WEB项目,引入开发包:
步骤二:引入spring的配置文件:
引入约束:aop的约束.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
步骤三:创建包和类:
* cn.itcast.aspectj.demo1
* CustomerService
* CustomerServiceImpl
步骤四:将这个类配置到Spring中:
<!-- 目标对象 -->
<beanid="customerService"class="cn.itcast.aspectj.demo1.CustomerServiceImpl"/>
步骤五:使用AspectJ注解方式:
在Spring到配置文件中完成一个配置:
<!-- 使用注解完成AOP的开发 -->
<aop:aspectj-autoproxy/>
步骤六:编写一个切面类:
注解的概述:
* @Aspect:修饰一个类.代表这个类就是一个切面类.(切面是切入点和通知的组合)
* 通知的注解:
* @Before :前置通知.
* @AfterReturing :后置通知.
* @Around :环绕通知.
* @AfterThrowing :异常抛出通知.
* @After :最终通知.
* @DeclareParents :引介通知
定义切入点表达式:通知哪些类的哪些方法需要增强.
* 基于一个execution(“表达式”)
* 表达式语法: [访问修饰符] 方法返回值 方法包和类名称(参数)
* 1. public * *(..)
* 2. * cn.itcast.service.*Service.*(..)
* 3. * cn.itcast.service.CustomerService+.*(..)
* 4. * cn.itcast.service.*.*(..)
* 5. * cn.itcast.service..*.*(..)
@Aspect
publicclass MyAspectAnno {
@Before(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.save(..))")
publicvoid before(){
System.out.println("前置通知============");
}
}
步骤七:将切面配置到Spring中.
<!-- 配置切面 -->
<beanid="myAspectAnno"class="cn.itcast.aspectj.demo1.MyAspectAnno"/>
步骤八:编写测试:
@Aspect
publicclass MyAspectAnno {
@Before(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.save(..))")
publicvoid before(){
System.out.println("前置通知============");
}
}
1.5.3 AspectJ的通知的用法:
@Before:前置通知:
在执行目标方法之前完成一个操作,获得到切入点信息.
@AfterReturing:后置通知:
在目标方法执行之后完成一个操作,获得方法的返回值.
@AfterReturning(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.update(..))",returning="result")
publicvoid afterReturing(Object result){
System.out.println("后置通知============"+result);
}
@Around:环绕通知:
在目标方法执行的前和执行后完成一个操作,阻止目标方法执行.
@Around(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.delete(..))")
publicObject around(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("环绕前通知==========");
Object obj = joinPoint.proceed();
System.out.println("环绕后通知==========");
return obj;
}
@AfterThrowing:异常抛出通知:
在目标方法出现异常的时候,完成一个操作.获得异常信息.
@AfterThrowing(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))",throwing="e")
publicvoid afterThrowing(Throwable e){
System.out.println("异常抛出通知========="+e.getMessage());
}
@After:最终通知:
在目标方法任何情况下都会执行的操作.相当于finally中的代码.
@After(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))")
publicvoid after(){
System.out.println("最终通知===========");
}
1.5.4 AspectJ的切入点:
定义切入点:
统一管理切入点的表达式.
@Pointcut(value="execution(* cn.itcast.aspectj.demo1.CustomerService+.find(..))")
privatevoidmyPointcut1(){}
1.5.5 Aspect和Advisor的区别:
Advisor :传统的切面.传统切面一般都是由一个切入点和一个通知的组合.
Aspect :真正意义上的切面.由多个切入点和多个通知的组合.
1.5.6 Spring的基于AspectJ的AOP的开发(XML)
步骤一:创建一个WEB项目,引入开发包:
步骤二:引入spring的配置文件:
引入约束:aop的约束.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
步骤三:创建包和类:
* cn.itcast.aspectj.demo2
* OrderService
步骤四:配置这个类到Spring
<!-- 配置目标类 -->
<beanid="orderService"class="cn.itcast.aspectj.demo2.OrderService"></bean>
步骤五:编写一个切面:
publicclass MyAspectXml {
publicvoid before(){
System.out.println("前置通知===========");
}
}
步骤六:在Spring中配置切面:
<!-- 配置切面 -->
<beanid="myAspectXml"class="cn.itcast.aspectj.demo2.MyAspectXml"></bean>
步骤七:在Spring中配置AOP:
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.save(..))"id="pointcut1"/>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.update(..))"id="pointcut2"/>
<aop:aspect ref="myAspectXml">
<aop:before method="before"pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
步骤八:编写测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
publicclass SpringDemo2 {
@Resource(name="orderService")
private OrderService orderService;
@Test
publicvoiddemo1(){
orderService.save();
orderService.update();
orderService.delete();
orderService.find();
}
}
1.5.7 XML中配置其他的通知的类型:
后置通知:
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.save(..))"id="pointcut1"/>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.update(..))" id="pointcut2"/>
<aop:aspect ref="myAspectXml">
<aop:before method="before"pointcut-ref="pointcut1"/>
<aop:after-returning method="afterReturing" pointcut-ref="pointcut2" returning="result"/>
</aop:aspect>
</aop:config>
环绕通知:
<!-- AOP的配置 -->
<aop:config>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.save(..))"id="pointcut1"/>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.update(..))"id="pointcut2"/>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.delete(..))" id="pointcut3"/>
<aop:aspectref="myAspectXml">
<aop:beforemethod="before" pointcut-ref="pointcut1"/>
<aop:after-returningmethod="afterReturing"pointcut-ref="pointcut2"returning="result"/>
<aop:around method="around" pointcut-ref="pointcut3"/>
</aop:aspect>
</aop:config>
异常抛出通知:
<!-- AOP的配置 -->
<aop:config>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.save(..))"id="pointcut1"/>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.update(..))"id="pointcut2"/>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.delete(..))"id="pointcut3"/>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.find(..))" id="pointcut4"/>
<aop:aspectref="myAspectXml">
<aop:beforemethod="before"pointcut-ref="pointcut1"/>
<aop:after-returningmethod="afterReturing"pointcut-ref="pointcut2"returning="result"/>
<aop:aroundmethod="around"pointcut-ref="pointcut3"/>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut4" throwing="e"/>
</aop:aspect>
</aop:config>
最终通知:
<!-- AOP的配置 -->
<aop:config>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.save(..))"id="pointcut1"/>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.update(..))"id="pointcut2"/>
<aop:pointcutexpression="execution(* cn.itcast.aspectj.demo2.OrderService.delete(..))"id="pointcut3"/>
<aop:pointcut expression="execution(* cn.itcast.aspectj.demo2.OrderService.find(..))" id="pointcut4"/>
<aop:aspectref="myAspectXml">
<aop:beforemethod="before"pointcut-ref="pointcut1"/>
<aop:after-returningmethod="afterReturing"pointcut-ref="pointcut2"returning="result"/>
<aop:aroundmethod="around"pointcut-ref="pointcut3"/>
<aop:after-throwingmethod="afterThrowing"pointcut-ref="pointcut4"throwing="e"/>
<aop:after method="after" pointcut-ref="pointcut4"/>
</aop:aspect>
</aop:config>
1.5.8 Spring中AOP开发的总结:
Spring的传统AOP开发:
* 基于ProxyFactoryBean的开发:
* 基于自动代理(BeanPostProcessor):
Spring基于AspectJ的AOP的开发
* 注解方式:
* XML方式:
1.6 Spring的JdbcTemplate:
1.6.1 Spring的JdbcTemplate:
模板的概述:
JDBC模板的快速入门:
步骤一:创建一个web项目,引入jar包:
步骤二:创建包结构:
cn.itcast.jdbc.demo1
步骤三:创建数据库和表:
create database spring_day02;
use spring_day02;
create table customer(
cid int primary key auto_increment,
cname varchar(20),
age int
);
步骤四:使用JDBC的模板:
publicvoid demo1(){
DriverManagerDataSource dataSource = newDriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_day02");
dataSource.setUsername("root");
dataSource.setPassword("123");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into customer values (null,?,?)", "张三",23);
}
1.6.2 Spring配置连接池:
Spring内置的连接池:
<!-- 配置Spring的内置的连接池 -->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql:///spring_day02"/>
<propertyname="username"value="root"/>
<propertyname="password"value="123"/>
</bean>
DBCP连接池:
引入DBCP连接池的jar包:
<!-- 配置DBCP连接池 -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql:///spring_day02"/>
<propertyname="username"value="root"/>
<propertyname="password"value="123"/>
</bean>
C3P0连接池:
引入c3p0的连接池的jar包:
<!-- 配置C3P0连接池 -->
<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
<propertyname="driverClass"value="com.mysql.jdbc.Driver"/>
<propertyname="jdbcUrl" value="jdbc:mysql:///spring_day02"/>
<propertyname="user"value="root"/>
<propertyname="password" value="123"/>
</bean>
1.6.3 将数据库的连接参数配置到属性文件中:
定义一个属性文件:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_day02
jdbc.user=root
jdbc.password=123
在Spring的核心配置中引入属性文件:
两种方式引入:
引入的方式一:
<!-- 引入属性文件 -->
<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location"value="classpath:jdbc.properties"/>
</bean>
引入方式二:引入context的约束
<context:property-placeholder location="classpath:jdbc.properties"/>
1.6.4 Spring的JdbcTemplate的CRUD的操作:
通用的增删改的方法:
update(String sql,Object… args)
查询的方法:
queryForInt(String sql,Object… args);
queryForObject(String sql,Class clazz,Object… args);
queryForObject(String sql,RowMapper rowMapper,Object… args);
query(String sql,RowMapper rowMapper,Object… args);