框架(一)Spring4入门

1.spring_AOP的开发入门

1.创建web项目,引入jar包
在这里插入图片描述
2.引入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"> 

3.编写目标类并完成配置
创建dao和dao的实现

public interface ProductDao {
	public void save();
	
	public void delete();
	
	public void update();
	
	public void find();
}
public class ProductDaoImpl implements ProductDao {

	@Override
	public void save() {
		System.out.println("保存.....");
	}

	@Override
	public void delete() {
		System.out.println("删除.....");
	}

	@Override
	public void update() {
		System.out.println("更新.....");
	}

	@Override
	public void find() {
		System.out.println("查询......");
	}

}

完成xml的配置

 <!-- 配置目标对象        目标类-->
    <bean id="productDao" class="spring_aop2.ProductDaoImpl"/>

4.编写一个测试类
spring整合Junit测试类:引入一个jar包即可

spring-test-4.2.4.RELEASE

编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class Spring_aop_test {
	@Resource(name="productDao")//注入xml中的id
	private ProductDao productDao;
	public void demo(){
		productDao.save();//调用ProductDaoImpl方法实现
		productDao.delete();
		productDao.find();
		productDao.update();
	}
}

5.增强某个方法,编写一个切面类MyAspectXML.java

/**
 * @author Leonard
 *	切面类
 */
public class MyAspectXML {
	public void check(){
		System.out.println("权限校验==============");
	}
}

6.将切面类交给spring去管理

 <!-- 将切面类交由spring管理 -->
    <bean id="myAspectJ" class="spring_aop2.MyAspectXML" />

7.通过AOP配置实现方法增强

<!-- 通过AOP对目标类产生代理 -->
    <aop:config>
        <!-- expression表达式 -->
        <aop:pointcut expression="execution(* spring_aop2.ProductDaoImpl.save(..))" id="daoId"/>
        <!-- 配置切面 -->
        <aop:aspect ref="myAspectJ">
            <aop:before method="check" pointcut-ref="daoId"/>
        </aop:aspect>
    </aop:config>

8.SpringAop的切入点表达式写法
语法:

【访问修饰符】 方法返回值 包名.类名.方法名(参数);

例:

public void com.leonard.customerDao.save(..);

2.Spring的AOP基于AspectJ的注解开发

1.引入必要的jar包
2.编写配置文件ApplicationContext.xml

<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.编写目标类OrderDao.java实现增删改查

public class OrderDao {
	public void save(){
		System.out.println("保存订单.....");
	}
	public void delete(){
		System.out.println("删除订单......");
	}
	public void update(){
		System.out.println("更新订单......");
	}
	public void find(){
		System.out.println("查找订单......");
	}
}

4.配置目标类。xml

<!--     配置目标类 -->
<bean id="order" class="spring.demo1.OrderDao">  </bean>

5.编写切面类并配置

/**
 * @author Leonard
 *注解的切面类
 */
public class MyAspectAnno {
	public void before(){
		System.out.println("前置注解增强.......");
	}
}

<!-- 配置切面类 -->
    <bean id="myAspect" class="spring.demo1.MyAspectAnno"></bean>

6.使用注解的AOP对目标类进行增强
步骤如下:
-------6.1在配置文件中打开注解的AOP开发

<!--  配置文件中开启Aop注解开发 -->
    <aop:aspectj-autoproxy />

=====6.2在切面类上使用注解

@Aspect
public class MyAspectAnno {
	//代表OrderDao里的save方法使用前置增强
	@Before(value = "execution(* spring.demo1.OrderDao.save(..))")
	public void before(){
		System.out.println("前置注解增强.......");
	}
}

7.编写Spring测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo {
	@Resource(name="order")
	private OrderDao orderDao;
	@Test
	public void demo(){
		orderDao.update();
		orderDao.save();
		orderDao.find();
		orderDao.delete();
	}
}

控制台输出

更新订单......
前置注解增强.......
保存订单.....
查找订单......
删除订单......

注:后置增强只需要MyAspectAnno切面类加入注解即可,其他代码保持不变

@AfterReturning("execution(* spring.demo1.OrderDao.delete(..))")
	public void afterReturnning(){
		System.out.println("后置增强.....");
	}

3.Spring的JDBC模板使用

3.1入门————普通方法
1.引入jar包
在这里插入图片描述
2.创建数据库,建表
在这里插入图片描述
3.创建一个简单的JDBC模板


public class demo1_jdbc {
	@Test
	public void demo1(){
		//1.创建连接池
		DriverManagerDataSource datasource = new DriverManagerDataSource();
		//2.连接数据库
		datasource.setDriverClassName("com.mysql.jdbc.Driver");
		datasource.setUrl("jdbc:mysql:///spring_1");
		datasource.setUsername("root");
		datasource.setPassword("123456");
		//3.创建jdbc模板
		JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
		jdbcTemplate.update("insert into account values(null,?,?)", "abc",1000000d);
	}
}

4.结果
在这里插入图片描述
3.2JDBC模板使用升级-------交给spring管理【使用默认连接池-----不常用】
引入jar包,如上
1.配置spring的xml文件

<!--      1. 配置spring内置连接池 -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 属性注入 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///spring_1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
      </bean>      
       <!--  2.配置Spring的JDBC模板     -->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="datasource"></property>
       </bean>
</beans>

2.创建JDBC【注解方式】

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class spring_2 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbctemplate;
	
	@Test
	public void demo(){
		jdbctemplate.update("insert into account values(null,?,?)", "bbb",222222d);
	}
}

3.结果
在这里插入图片描述
3.3JDBC模板使用升级-------交给spring管理【配置使用DBCP连接池----了解】
1.额外引入jar包
在这里插入图片描述
2.xml配置文件

      <!-- 2.dbcp配置连接池 -->
      <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///spring_1"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
      </bean>
       <!--  2.配置Spring的JDBC模板     -->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="datasource"></property>
       </bean>

3.创建测试类,操作数据库

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class spring_2 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbctemplate;
	
	@Test
	public void demo(){
		jdbctemplate.update("insert into account values(null,?,?)", "ccc",333333d);
	}
}

结果
在这里插入图片描述

3.3JDBC模板使用升级-------交给spring管理【配置使用c3p0连接池----重点】
1.引入c3p0需要的jar
在这里插入图片描述
2.在xml文件中配置c3p0连接池

 <!-- c3p0配置连接池 -->
      <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql:///spring_1"/>
            <property name="user" value="root"/>
            <property name="password" value="123456"/>
      </bean>
       <!--  2.配置Spring的JDBC模板     -->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="datasource"></property>
       </bean>

3.创建测试类,操作数据库

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class spring_2 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbctemplate;
	
	@Test
	public void demo(){
		jdbctemplate.update("insert into account values(null,?,?)", "ddd",555555d);
	}
}

4.结果
在这里插入图片描述
3.4JDBC模板使用升级-------交给spring管理【提取再升级完成】
抽取配置到属性文件
1.定义属性文件:jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_1
jdbc.username=root
jdbc.password=123456

2.在Spring配置文件引入属性配置文件

 <!-- 引入配置文件 -->
      <context:property-placeholder location="classpath:jdbc.properties"/>
 <!-- 引入相应的值 -->
      <bean id="datasoource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
      </bean>

3.创建测试类,写入数据库

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class spring_2 {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbctemplate;
	
	@Test
	public void demo(){
		jdbctemplate.update("insert into account values(null,?,?)", "eee",555555d);
	}
}

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值