前言
一个菜鸡的学习笔记,课程是尚硅谷的spring5教程。没有干货。
一、spring的俩大核心介绍
IOC(控制反转)
可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入,还有一种方式叫“依赖查找”。通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。
IOC接口
1:基于IOC容器完成,而IOC容器底层就是对象工厂
2:spring提供了IOC的俩种接口:
(1)BeanFactory:IOC容器基本实现,是spring内部使用的接口,一般使用的是ApplicarionContext。BeanFactory是懒加载,如在扫描加载xml配置文件时不会创建对象,在获取对象时才创建。
//这一步不会创建对象
BeanFactory beanfactory = new ClassPathXmlApplicationContext(bean.xml);
//获取对象时创建
beanfactory.getBean("User",User.Class);
(2)ApplicationContext:BeanFactory的子接口,提供更强大功能,开发时使用,加载xml配置时创建对象
(3)file是绝对路径,class是相对路径
AOP(面向切面编程)
通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP操作术语
1:连接点:类中可以被增强的方法被成为连接点
2:切入点:实际被真正增强的方法被称为切入点
3:通知:实际增强部分的逻辑,也就是怎么实现增强的逻辑被称为通知
a:前置通知:在增强方法执行之前执行
b:后置通知:在增强方法 执行之后执行
c:环绕通知:增强方法执行前后都执行
d:异常通知:增强方法出现异常执行
e:最终通知:无论是否异常都会执行,类似finally
4:切面:通知应用到切入点的过程
二、IOC-Bean管理
0:导入配置
1.创建maven项目,在pom.xml文件中导入依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<!--版本选择稳定版本-->
<version>5.3.15</version>
</dependency>
</dependencies>
2.在resources文件夹下创建xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
1.Bean管理xml
在没有创建构造方法时,Bean默认用无参构造创建对象,在创建了有参构造方法后,无参构造默认消失
(1):set注入属性
a:注入普通参数,set,list,map
创建类 ,创建set方法
public class Book {
private String book;
private String name;
private List booolist;
private Map<String, User> map;
private String[] books;
public void setBook(String book) {
this.book = book;
}
public void setName(String name) {
this.name = name;
}
public void setBooolist(List booolist) {
this.booolist = booolist;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
public void setBooks(String[] books) {
this.books = books;
}
public void show(){
System.out.println("我是通过set注入创建的对象");
System.out.println(name+"*"+book);
System.out.println(booolist);
System.out.println(map);
System.out.println(Arrays.toString(books));
}
}
在xml中配置bean
<!-- id是创建的对象的名字,class是类的地址-->
<bean id="Book" class="com.zhao.pojo.Book">
<!-- name是属性的名字,value是属性赋的值-->
<property name="name" value="作者"/>
<property name="book" value="书名"/>
<property name="books">
<array>
<value>数据库原理</value>
<value>Java语言设计</value>
</array>
</property>
<property name="map">
<map>
<!-- 属性是对象时应在外面注入对象属性的Bean-->
<entry key="Java" value-ref="User"/>
<!-- <entry key="C++" value="C++"/>-->
</map>
</property>
<property name="booolist">
<list>
<value>数据库库</value>
<value>Javajava</value>
</list>
</property>
</bean>
<bean id="User" class="com.zhao.pojo.dao.User">
<property name="name" value="User的名字"></property>
</bean>
测试
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Book book = context.getBean("Book",Book.class);
book.show();
//我是通过set注入创建的对象
//作者*书名
//[数据库库, Javajava]
//{Java=com.zhao.pojo.dao.User@5e853265}
//[数据库原理, Java语言设计]
b:注入外部bean,内部bean和级联操作
创建dao和service类
//User类
public class User {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("i am dao");
}
}
//service类
public class UserService {
private User user;
public void setUser(User user){
this.user = user;
}
public User getUser() {
return user;
}
public void show () {
System.out.println("i am service");
user.show();
}
}
注入bean
<!--先注入service类中所需的user类-->
<bean id="user" class="com.zhao.pojo.dao.User"/>
<bean id="service" class="com.zhao.pojo.service.UserService">
<!-- 外部bean name是类中的属性名称 -->
<property name="user" ref="user"/>
<!--内部bean
<property name="user">
<bean id="user" class="com.zhao.pojo.dao.User"></bean>
</property>-->
<!-- 级联操作,在必须service中要有get方法-->
<property name="user.name" value="级联"></property>
</bean>
测试
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService) context.getBean("service");
service.show();//i am service i am dao
(2):有参数构造注入属性
创建类
public class Book {
private String book;
private String name;
public Book(String book,String name){
this.book = book;
this.name = name;
}
public void show(){
System.out.println("我是通过有参构造注入的属性");
}
}
xml中配置bean
<bean id="Book" class="com.zhao.pojo.Book">
<!-- 这里采用了俩种有参构造注入属性的方法,第一种是直接写参数名,第二种是写下标,一般用name-->
<constructor-arg name="book" value="书名"/>
<constructor-arg index="1" value="作者"/>
</bean>
测试
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Book book = context.getBean("Book",Book.class);
book.show();//我是通过有参构造注入的属性
(3)Bean的作用域
默认情况是单例 scope可隐藏,单例在读取配置文件时对象就已经创建
<bean id="Book" class="com.zhao.pojo.Book" scope="singletion"/>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//book对象就已经创建,且只有一个对象
Book book1 = context.getBean("Book");//book1和book2是同一个对象
Book book2 = context.getBean("Book");
多例应该设置scope属性,在getBean时创建对象
<bean id="Book" class="com.zhao.pojo.Book" scope="prototype"/>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Book book1 = context.getBean("Book");//此时创建对象,book1和book2不是同一个对象
Book book2 = context.getBean("Book");
(4)Bean的生命周期
创建对象
//创建测试对象
public class BeanCycle implements BeanPostProcessor {
//bean后置处理器的方法
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("三:初始化之前执行的方法");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("五:初始化之后执行的方法");
return bean;
}
private String name;
public BeanCycle (){
System.out.println("一:通过无参构造创建");
}
public void setName(String name) {
this.name = name;
System.out.println("二:通过set方法注入");
}
public void initmethod(){
System.out.println("四:执行初始化方法");
}
public void destoryMethod(){
System.out.println("七:执行销毁方法");
}
}
//创建bean的后置处理器实现类
public class post implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("三:初始化之前执行的方法");
return null;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("五:初始化之后执行的方法");
return null;
}
}
配置xml
<!--用init—method指定初始化方法 destory——method指定销毁方法-->
<bean id="BeanCycle" class="com.zhao.pojo.BeanCycle" init-method="initmethod" destroy-method="destoryMethod">
<property name="name" value="name"/>
</bean>
<bean id="post" class="com.zhao.pojo.post" ></bean>
测试
//初始化之前后之后执行的方法在每个bean实例创建之前都会执行
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BeanCycle beanCycle = context.getBean("BeanCycle",BeanCycle.class);
System.out.println("六:getBean获取创建的bean实例对象");
context.close();
// 一:通过无参构造创建
// 二:通过set方法注入
// 三:初始化之前执行的方法
// 四:执行初始化方法
// 五:初始化之后执行的方法
// 六:getBean获取创建的bean实例对象
// 七:执行销毁方法
2.Bean管理注解方式
注解创建对象
(1)@Component//2.3.4的每个注解源码都包含Component,这四个注解没有本质区别,是为了方便开发人员区分
(2)@Service //一般用在Service层
(3)@Controller//一般用在Controller层
(4)@Repository//一般用在Dao层
1:创建对象
@Repository
public class User {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("i am dao"+ this.name);
}
}
2:配置xml
<!-- use-default-filters表示不在使用默认filter,自己配置filter -->
<context:component-scan base-package="com.zhao.pojo" use-default-filters="false">
<!-- 扫描包含Repository注解的类 include改为exclude时,表示扫描不包含Repository的类-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
3:测试
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user", com.zhao.pojo.dao.User.class);
user.show();//i am daonull
注解实现属性注入
(1)@Autowired //根据属性类型装配
(2)@Qualifier//根据属性名称装配,一般和Autowired一起使用。
(3)@Resource//名称类型都能装配
(4)@Value//普通类型注入
1:创建对象
//Dao层
public class User {
@Value(value = "value注解设置的属性")
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("i am dao"+ this.name);
}
}
//Service层
@Service
public class UserService {
@Autowired
private User user;
public void show () {
System.out.println("i am service");
user.show();
}
}
2:配置xml
<context:component-scan base-package="com.zhao.pojo"/>
3:测试
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService user = (UserService) context.getBean("userService");
user.show();//i am service
//i am daovalue注解设置的属性
完全注解开发
不需要配置xml,直接用类和注解代替
1:创建配置类对象
@Configuration//配置类,替代配置文件
@ComponentScan(basePackages = {"com.zhao.pojo"})
public class config {
}
2:测试
//以注解实现注入时的类为例
ApplicationContext context = new AnnotationConfigApplicationContext(config.class);
UserService user = (UserService) context.getBean("userService");
user.show();//i am service
//i am daovalue注解设置的属性
三:AOP操作
1.AspectJ注解实现
当一个方法有多个增强类时,可以在增强类上加@Order注解,注解中标明数值,数值大小表示优先级,越小优先级越高
1:创建User测试类
@Component
public class User {
public void add(){
System.out.println("add方法");
}
}
2:创建增强类
@Component
@Aspect
public class UserProxy {
// 相同切入点抽取
@Pointcut(value = "execution(* aopanno.User.add())")
public void pointdemo(){
}
// 作为前置通知 有切入点,value中的值可换为pointdemo
@Before(value = "pointdemo()")
public void before(){
System.out.println("前置通知");
}
// 后置通知
@After(value = "execution(* aopanno.User.add())")
public void after(){
System.out.println("最终通知");
}
//
@AfterReturning(value = "execution(* aopanno.User.add())")
public void afterreturn(){
System.out.println("后置通知");
}
@AfterThrowing(value = "execution(* aopanno.User.add())")
public void afterthrow(){
System.out.println("异常通知");
}
// 环绕通知
@Around(value = "execution(* aopanno.User.add())")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("环绕之前");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("环绕之后");
return proceed;
}
}
3:创建xml文件,开启扫描
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<context:component-scan base-package="aopanno"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4:测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user = context.getBean("user", User.class);
user.add();
}
// 环绕之前
// 前置通知
// add方法
// 环绕之后
// 最终通知
// 后置通知
2.AspectJ配置文件实现(了解)
1:创建测试类和增强类,不需要注解,测试类同上
2:配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="user" class="aopanno.User"></bean>
<bean id="userproxy" class="aopanno.UserProxy"></bean>
<!-- 增强aop配置-->
<aop:config>
<!-- 切入点-->
<aop:pointcut id="us" expression="execution( * aopanno.User.add(..))"/>
<!-- 配置切面-->
<aop:aspect ref="userproxy">
<!-- 增强具体方法-->
<aop:before method="before" pointcut-ref="us"></aop:before>
</aop:aspect>
</aop:config>
</beans>
3:测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
User user = context.getBean("user", User.class);
user.add();
}
// 前置通知
// add方法
三:JdbcTemplate
通过jdbcTemplate对数据库进行操作
一.操作数据库
1:配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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"
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">
<!-- 组件扫描-->
<context:component-scan base-package="aopanno"></context:component-scan>
<!-- 数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!-- 连接数据库-->
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="deptService" class="service.deptService"></bean>
</beans>
二:测试
1:创建实体类
package entity;
public class dept {
private Integer id;
private String name;
private String address;
@Override
public String toString() {
return "dept{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2:创建service类
@Service
public class deptService {
@Autowired
private Deptimpl deptimpl;
// 添加
public void addDept(dept dept1){
deptimpl.add(dept1);
}
// 修改
public void updata(dept dept){
deptimpl.updata(dept);
}
// 删除
public void delete(int id){
deptimpl.delete(id);
}
// 查询表记录总数
public void selectcount(){
int i = deptimpl.selectCount();
System.out.println(i);
}
// 根据id查询数据
public void findone(int id){
dept dept1 = deptimpl.findone(id);
System.out.println(dept1);
}
// 查询所有数据
public void findAll(){
final List<dept> all = deptimpl.findAll();
System.out.println(all);
}
// 批量增加
public void addAll(List<Object[]>list){
deptimpl.addAll(list);
}
//批量增加
public void addAll(List<Object[]>list) {
String sql = "insert into dept values(?,?,?)";
//批量修改,批量删除方法相同
jdbcTemplate.batchUpdate(sql,list);
}
}
3:创建dao类
import entity.dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class Deptimpl {
@Autowired
private JdbcTemplate jdbcTemplate;
// 添加
public void add(dept dept1) {
//sql语句
String sql = "insert into dept values(?,?,?)";
Object[] d = {dept1.getId(), dept1.getName(), dept1.getAddress()};
int update = jdbcTemplate.update(sql,d);
System.out.println(update);
}
//修改
public void updata(dept dept2) {
Object[] d = {dept2.getId(),dept2.getAddress()};
String sql = "update dept set id=? where addr=?";
int update = jdbcTemplate.update(sql, d);
System.out.println(update+1);
}
//删除
public void delete(int id) {
String sql = "delete from dept where id=?";
int update = jdbcTemplate.update(sql,id);
System.out.println(update+2);
}
//查询数据总数
public int selectCount() {
String sql = "select count(*) from dept";
Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
return integer;
}
//根据id查询数据
public dept findone(int id) {
String sql = "select * from dept where id=?";
dept dept1 = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<dept>(dept.class), id);
return dept1;
}
//查询所有数据
public List<dept> findAll() {
String sql = "select * from dept";
List<dept> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<dept>(dept.class));
return query;
}
}
4:测试
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
deptService deptService = context.getBean("deptService", service.deptService.class);
dept dept1=new dept();
dept1.setId(8);
dept1.setName("营销一部");
dept1.setAddress("苏州");
deptService.addDept(dept1);
// 修改 dept1.setId(5);
//修改 dept1.setAddress("苏州");
// 修改 deptService.updata(dept1);
// 删除 deptService.delete(3);
//查询表中数据总数 deptService.selectcount();
//根据id查询数据 deptService.findone(1);
//查询所有数据 deptService.findAll();
//批量增加 List<Object[]>list = new ArrayList<Object[]>();
//批量增加 Object[] o1 = {2,"测试部","天津"};
//批量增加 list.add(o1);
//批量增加 Object[] o2 = {3,"运营部","广州"};
//批量增加 list.add(o2);
//批量增加 Object[] o3 = {4,"宣传部","杭州"};
//批量增加 list.add(o3);
//批量增加 deptService.addAll(list);
2.事务
当包没有共同的包时,用纯注解开发应用componentscans扫描组件
a.基于注解开启事务管理
1:配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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: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/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
>
<!-- 组件扫描-->
<context:component-scan base-package="dao"></context:component-scan>
<!-- 数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<bean id="bankService" class="service.bankService"></bean>
</beans>
2:创建dao
package dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class bankimpl {
@Autowired
private JdbcTemplate jdbcTemplate;
public void reduce(){
String name = "张三";
String sql = "update bank set money=money-100 where name=?";
jdbcTemplate.update(sql,name);
}
public void add() {
String name = "李四";
String sql = "update bank set money=money+100 where name=?";
jdbcTemplate.update(sql,name);
}
}
3:创建service层
package service;
import dao.bankimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
//可以添加到类或方法上面,这里添加到类上,即所有方法都有事务
//参数说明
//一:propagation:传播行为
//二:isolation:隔离级别,隔离级别默认repeatable read
//三:timeout:超时时间,默认是-1,按秒为单位,超过设置时间未提交事务就进行回滚
//四:readOnly:只读:默认false,改为true后,只能读取数据不能修改数据
//五:rollbackFor,norollbackFor:设置出现哪些异常回滚,哪些异常不回滚
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout = 5,readOnly = true)
public class bankService {
@Autowired
private bankimpl bankimpl;
public void accountMoney(){
//放入try catch中时应抛出异常,否则不能触发回滚
bankimpl.add();
int a=10/0;
bankimpl.reduce();
}
}
4:测试
package Test;
import entity.dept;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.bankService;
import service.deptService;
public class test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
bankService bankService = context.getBean("bankService", bankService.class);
bankService.accountMoney();
}
}
b:基于xml开启事务管理
1:配置事务管理器
2:配置通知
3:配置切入点和切面
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 组件扫描-->
<context:component-scan base-package="dao"></context:component-scan>
<!-- 数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--jdbctemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 1:创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--2:配置通知-->
<tx:advice id="txadvice">
<!-- 配置事务参数-->
<tx:attributes>
<!-- 指定哪种规则的方法上添加事务-->
<tx:method name="accountMoney" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 3:配置切入点和切面-->
<aop:config>
<!-- 配置切入点-->
<aop:pointcut id="pt" expression="execution(* service.bankService.accountMoney())"/>
<!-- 配置切面 ,txadvice是配置通知的通知名-->
<aop:advisor advice-ref="txadvice" pointcut-ref="pt"></aop:advisor>
</aop:config>
</beans>
c:完全注解开启事务管理
package config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@ComponentScan(basePackages = "com.zhao")//组件扫描
@EnableTransactionManagement//开启事务
public class TxConfig {
//创建数据库连接池
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.alibaba.druid.pool.DruidDataSource");
dataSource.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
}
// 创建JdbcTemplate
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
//到ioc容器中找dataSource
JdbcTemplate jdbcTemplate = new JdbcTemplate();
// 注入dataSource
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
//创建事务管理器
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager transactionManager =new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource);
return transactionManager;
}
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。