落落的spring
# spring概述及IOC理论
官网 : http://spring.io/
官方下载地址 : https://repo.spring.io/libs-release-local/org/springframework/spring/
GitHub : https://github.com/spring-projects
使用一句话概括spring就是:
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器(框架)。
## Boot与Cloud Spring Boot : 一个快速开发的脚手架,基于Spring Boot可以快速的开发 约定大于配置 Spring Cloud : 是基于Spring Boot实现的
Spring Boot : 一个快速开发的脚手架,基于Spring Boot可以快速的开发
约定大于配置
Spring Cloud : 是基于Spring Boot实现的
## IOC本质
控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法。没有IoC的程序中 , 我们使用面向对象编程 , 对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了。
*IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)
## spring配置说明
1.别名:ailas>
alias name=“user” alias=“别名,别名2”/>
2.汇口:import>:把import中的配置合并到这个配置中.
import resource==“spring_dao.xml”/> 把这个导入本配置中.
3.DI依赖注入
3.1
## Bean的自动配置
在spring中有三种配置的方式
1.在xml中显示的配置
2.在java中显示的配置
3.隐式的自动装配bean 核心
3.1:在中添加autowire==“byType”
bean id=“people” class=“com.ll.pojo.People” autowire=“byType”>
3.2:在实现的类的对象上@Autowired
注解实现自动装配
1.导入约束:context 约束
2.配置注解的支持 context:annotation-config/>
3. @Autowired 与@Resource
4. 在注解的属性行使用即可.在对应set方法上使用也行
5.@Autowired(required = false) :说明该对象可以为空,否则不允许为空.
未实现
6.@Autowired 与@Resource
@Component注解
完全可以舍弃bean.
配置中开启注解支持和指定扫描的包
context:component-scan base-package=“com.ll.dao”/>
衍生注解:
@Controller web层
@Service Service层
@Repository dao层
@Configuration:相当于配置文件的beans
@ComponentScan(“com.ll.pojo”):相当于 context:component-scan base-package=“com.ll.dao”/>
@Import(Config2.class):@Import通过快速导入的方式实现把实例加入spring的IOC容器中
@bean:注册一个bean,就相当于我们之前写的一个bean标签
这个方法的名字,就相当于bean标签中的id属性
这个方法的返回值,就相当于bean标签中的class属性
## 作用域
@scope(“xx”)
singleton:默认的,spring会采用单例模式创建.关闭工厂,所有对象都会销毁.
局限:高并发情况下,会产生延迟和数据不一致的情况.
prototype:多例模式.每次容器中get的时候,都会产生一个新对象.关闭工厂,所有的对象不会销毁.内部的垃圾回收机制回收
1.创建一个实体类
package com.ll.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//这里这个注解的意思就是说明这个类被Spring接管了.注册到了容器中
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("落落呀233")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}}"
2.创建一个config配置包,编写一个Myconfig配置类
package com.ll.config;
import com.ll.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Component
//@Configuration 代表这是一个配置类,beans.xml
@Configuration
@ComponentScan("com.ll.pojo")
@Import(Config2.class)
public class Config {
@Bean
public User getUser(){
return new User();
}
}
# 代理模式
## 1静态代理
1.1抽象角色
package com.ll.demo01;
//租房
public interface Rent {
public void rent();
}
1.2真实角色
package com.ll.demo03;
//房东
public class Host implements Rent {
public void rent(){
System.out.println("房东要出租房子!");
}
}
1.3代理角色
package com.ll.demo01;
public class Proxy implements Rent{
private Host host;
public Proxy(Host host) {
this.host = host;
}
public Proxy() {
}
public void rent(){
host.rent();
}
public void seeHouse(){
System.out.println("中介带你看房");
}
public void fare(){
System.out.println("收中介费");
}
public void hetong(){
System.out.println("签租赁合同");
}
}
1.4即客户需求
package com.ll.demo01;
public class Client {
public static void main(String[] args) {
//房东要出租房子
Host host=new Host();
//代理,中介代房东租房子,但是呢?代理一般会有一些附属操作!
Proxy proxy=new Proxy(host);
//你不用面对房东,直接找中介租房子即可!
proxy.seeHouse();
proxy.hetong();
proxy.fare();
proxy.rent();
}
}
## 2动态代理
2.1抽象角色
package com.ll.demo03;
//租房
public interface Rent {
void rent();
}```
2.2真实角色
```java
package com.ll.demo03;
//房东
public class Host implements Rent {
public void rent(){
System.out.println("房东要出租房子!");
}
}
2.3 ProxyInvocationHandler.即动态代理角色
package com.ll.demo03;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//可以使用这个类,自动生成代理类!
public class ProxyInvocationHandler implements InvocationHandler {
//被代理的接口
private Rent rent;
public void setRent(Rent rent) {
this.rent = rent;
}
//生成得到代理类
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader(),
rent.getClass().getInterfaces(), this);
}
//处理代理实例,并返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//动态代理的本质,就是使用反射机制实现!
Object result = method.invoke(rent, args);//相当于rent方法
seeHouse();
fare();
return null;
}
public void seeHouse(){
System.out.println("中介带看房子");
}
public void fare(){
System.out.println("收中介费");
}
}
2.4 需求
package com.ll.demo03;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//真实角色
Host host = new Host();
//代理角色:现在没有
ProxyInvocationHandler pih = new ProxyInvocationHandler();
//通过调用程序处理角色 来处理我们要调用的接口对象!
pih.setRent(host);
Rent proxy = (Rent) pih.getProxy();//这里的proxy就是动态生成的,我们并没有写
proxy.rent();
}
}
# AOP
【重点】使用AOP织入,需要导入一个依赖包!
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
## 第一种方法:通过spring的API
需要配置 aop和context
第一步:编写业务接口和实现类
package com.bei.service;
public interface BeiService {
public void add();
public void delete();
public void update();
public void select();
}
package com.bei.service;
public class BeiServiceImpl implements BeiService{
public void add() {
System.out.println("添加一个数据");
}
public void delete() {
System.out.println("删除一个数据");
}
public void update() {
System.out.println("更新一个数据");
}
public void select() {
System.out.println("查询数据");
}
}
第二步:编写增强类:这里写一个前置增强和一个后置增强
前置增强:实现接口:MethodBeforeAdvice
method:对象的方法
objects:被调用方法的参数
o:被调用的对象
package com.bei.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
后置增强:实现接口:AfterReturningAdvice
o:返回值
method:被调用的方法
objects:被调用方法的目标对象
o1:被调用的对象
package com.bei.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+o1.getClass().getCanonicalName()+"的"+method.getName()+"方法"
+"返回值是:"+o);
}
}
第三步:去applicationContext.xml中注册,并实现aop
注:配置aop
解析:
aop:config:aop配置
aop:pointcut:zop切入点
aop:aspect:见第二种方法
<!-- 注册bean-->
<bean id="beiService" class="com.bei.service.BeiServiceImpl"/>
<bean id="log" class="com.bei.log.Log"/>
<bean id="afterLog" class="com.bei.log.AfterLog"/>
<!-- aop配置-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.bei.service.BeiServiceImpl.*(..))"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
第四步:测试
注:快捷 CPX
BeiService.class)和"beiService"都可以
public class BeiTest {
@Test
public void beiTest1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BeiService beiService = context.getBean(BeiService.class);
beiService.add();
}
}
## 第二种方法:自定义类来实现Aop
第一步:业务不变
第二步:写一个我们自己的切入点
package com.bei.diy;
public class DiyPointcut {
public void before(){
System.out.println("----------执行方法前----------");
}
public void after(){
System.out.println("------------执行方法后++++++++");
}
}
第三步:在applicationContext.xml中配置
<bean id="beiService" class="com.bei.service.BeiServiceImpl"/>
<bean id="diy" class="com.bei.diy.DiyPointcut"/>
<!-- aop的配置-->
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="diyPointcut" expression="execution(* com.bei.service.BeiServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="diyPointcut" />
<aop:after method="after" pointcut-ref="diyPointcut"/>
</aop:aspect>
</aop:config>
第四步:测试不变
## 第三种方式:使用注解实现
第一步:业务需求不变
第二步:编写一个注解实现的增强类
package com.bei.config;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.bei.service.BeiServiceImpl.*(..))")
public void before() {
System.out.println("-------方法执行前--------");
}
@After("execution(* com.bei.service.BeiServiceImpl.*(..))")
public void after() {
System.out.println("-------方法执行后--------");
}
@Around("execution(* com.bei.service.BeiServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("程序切入点"+jp.getSignature());
//执行目标方法
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
第三步:在applicationContext.xml中注册bean,并增加支持注解的配置
<bean id="beiService" class="com.bei.service.BeiServiceImpl"/>
<!-- 第三种方式:注解实现-->
<bean id="annotationPointcut" class="com.bei.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
aop:aspectj-autoproxy:说明
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class=“true”/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
第四步:测试 结 果
环绕前
程序切入点void com.bei.service.BeiService.add()
-------方法执行前--------
添加一个数据
环绕后
null
-------方法执行后--------
Process finished with exit code 0
# 整合MyBatis
spring:需要导入的包:
spring相关 spring-webmvc spring-jdbc
aop aspectjweaver
lombok junit
第一步:导入相关jar包
junit mybatis mysql-connector-java
spring相关 spring-webmvc spring-jdbc
aop aspectjweaver
mybatis-spring整合包 【重点】 mybatis-spring
lombok
配置Maven静态资源过滤问题!
src/main/java
/*.properties
/*.xml
true
## 回忆mybatis
第一步:编写pojo实体类
package com.ll.pojo;
import lombok.Data;
@Data
public class Luo {
private int id;
private String name;
private String sex;
}
第二步:实现mybatis的配置文件 mybatis_config.xml
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.ll.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/luoluo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class="com.ll.dao.LuoMapper"/>
</mappers>
</configuration>
第三步:接口编写LuoMapper
package com.ll.dao;
import com.ll.pojo.Luo;
import java.util.List;
public interface LuoMapper {
public List<Luo> selectLuo();
}
第四步:编写对应的Mapper映射文件LuoMapper.xml
把映射文件注册到 mybatis_config.xml中
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ll.dao.LuoMapper">
<select id="selectLuo" resultType="luo">
select * from luo
</select>
</mapper>
第五步:测试
@Test
public void luoTest() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis_config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
LuoMapper mapper = sqlSession.getMapper(LuoMapper.class);
List<Luo> luoList = mapper.selectLuo();
for (Luo luo : luoList) {
System.out.println(luo);
}
sqlSession.close();
}
## MyBatis-Spring学习
http://www.mybatis.org/spring/zh/index.html
MyBatis-Spring 会帮助你将 MyBatis 代码无缝地整合到 Spring 中。
MyBatis-Spring 需要以下版本:
MyBatis-Spring MyBatis Spring 框架 Spring Batch Java
2.0 3.5+ 5.0+ 4.0+ Java 8+
1.3 3.4+ 3.2.2+ 2.1+ Java 6+
如果使用 Maven 作为构建工具,仅需要在 pom.xml 中加入以下代码即可
mybatis-spring
要和 Spring 一起使用 MyBatis,需要在 Spring 应用上下文中定义至少两样东西:一个 SqlSessionFactory 和至少一个数据映射器类。
## Spring-Mybatis整合方式1
需要三个xml:
1: LuoMapper.xml:和mybatis一样
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bei.dao.LuoMapper">
<select id="selectLuo" resultType="com.bei.pojo.Luo">
select * from luo
</select>
</mapper>
2: mybatis-dao.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">
<!--1.配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的 变数1:数据库名称,账号密码名,或者跟换数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/luoluo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--2.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 变数2:绑定mybatis配置文件
配置和映射文件-->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
<property name="mapperLocations" value="classpath:com/bei/dao/*.xml"/>
</bean>
<!--3.注册SqlSessionTemplate,关联sqlSessionFactory,就是一个sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 5.通过bean把实体类注入实现 变数3:绑定接口实现类,???sqlSession-->
<bean id="luoMapper" class="com.bei.dao.LuoMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
3: mybatis_config.xml:可以为空,不能没有.通常设置别名和配置两种功能
<?xml version="1.0" encoding="UTF8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<!-- settings对位置有要求-->
<settings>
<setting name="" value=""/>
</settings>
<typeAliases>
<package name="com.bei.pojo"/>
</typeAliases>
</configuration>
类方面:2个类一个接口外加一个测试
一个实体类 Luo
package com.bei.pojo;
import lombok.Data;
@Data
public class Luo {
private int id;
private String name;
private String sex;
}
一个接口 LuoMapper
package com.bei.dao;
import com.bei.pojo.Luo;
import java.util.List;
public interface LuoMapper {
public List<Luo> selectLuo();
}
一个接口实现类 LuoMapperImpl
```java
package com.bei.dao;
import com.bei.pojo.Luo;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
//4.编写接口的实现类,私有化sqlSessionTemplate
public class LuoMapperImpl implements LuoMapper {
private SqlSessionTemplate sqlSession;
//sqlSession的set方法++接口方法重写
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public List<Luo> selectLuo() {
LuoMapper mapper = sqlSession.getMapper(LuoMapper.class);
return mapper.selectLuo();
}
}
一个测试类:LuoTest
import com.bei.dao.LuoMapper;
import com.bei.pojo.Luo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class LuoTest{
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("mybatis-dao.xml");
LuoMapper mapper = (LuoMapper) context.getBean("luoMapper",LuoMapper.class);
List<Luo> selectLuo = mapper.selectLuo();
for (Luo luo : selectLuo) {
System.out.println(luo);
}
}
}
页面布局
## Spring-Mybatis整合方式2
在整合方式1的基础上简化.可以完全舍弃mybatis-config.xml文件
在整合方式1的基础上变化:
第一变:接口的实现类变化 : 核心
LuoMapperImpl 在接口的基础上继承 SqlSessionDaoSupport 重写接口方法即可.
package com.bei.dao;
import com.bei.pojo.Luo;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
//4.编写接口的实现类,继承SqlSessionDaoSupport
public class LuoMapperImpl extends SqlSessionDaoSupport implements LuoMapper{
public List<Luo> selectLuo() {
LuoMapper mapper = getSqlSession().getMapper(LuoMapper.class);
return mapper.selectLuo();
}
}
第二变:5.处 修改mybatis-dao.xml中接口实现类的配置并在
变数2出删除mybatis的绑定文件配置.实现完全舍弃mybatis-config.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">
<!--1.配置数据源:数据源有非常多,可以使用第三方的,也可使使用Spring的
变数1:数据库名称,账号密码名,或者跟换数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/luoluo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--2.sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 变数2:删除mybatis-config.xml的配置信息,如下-->
<!-- <property name="configLocation" value="classpath:mybatis_config.xml"/>-->
<property name="mapperLocations" value="classpath:com/bei/dao/*.xml"/>
</bean>
<!--3.注册SqlSessionTemplate,关联sqlSessionFactory,就是一个sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 5.修改bean配置的属性 变数3:绑定接口实现类,???sqlSession-->
<bean id="luoMapper" class="com.bei.dao.LuoMapperImpl">
<!-- <property name="sqlSession" ref="sqlSession"/>-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
完成!!!
# spring中的事务管理
先在spring-dao.xml的头文件中的约束导入
tx
xmlns:tx=“http://www.springframework.org/schema/tx”
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
aop:
xmlns:aop=“http://www.springframework.org/schema/aop”
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
第一步:通过spring配置applicationContext.xml
<?xml version="1.0" encoding="UTF8"?>
<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">
<import resource="spring_dao.xml"/>
<bean id="luoMapper" class="com.ll.mapper.LuoMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>```
第二步:在spring-dao.xml中配置声明式事务
第三步:结合aop实现事务的织入
第四步:配置事务切入
都在spring-dao.xml完成具体如下:
```java
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- datasource: 使用spring的数据源替换mybatis的数据源 c3p0 dbcp druid
我们这里使用spring 提供的jdbc:org.springframework.jdbc.datasource.DriverManagerDataSource-->
<!-- 快捷DriM-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/luoluo"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置sqlSessionFactory,关联mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 绑定mybatis配置文件
配置和映射定位-->
<property name="configLocation" value="classpath:mybatis_config.xml"/>
<property name="mapperLocations" value="classpath:com/ll/mapper/*.xml"/>
</bean>
<!-- 第二步: 配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSource" />
</bean>
<!-- 第三步: 结合aop实现事务的织入-->
<!-- 配置事务通知:-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 给那些方法配置事务
配置事务的传播特性:new propagation -->
<tx:attributes>
<tx:method name="add" propagation="REQUIRED"/>
<tx:method name="delete" />
<tx:method name="query"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 第四步: 配置事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.ll.mapper.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
<!-- 接口实现类绑定-->
<bean id="luoMapper" class="com.ll.mapper.LuoMapperImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
注:接口的实现类绑定在spring_dao.xml和applicationContext.xml有其一就行
而applicationContext.xml中的
<import resource="spring_dao.xml"/>
不可以删除,applicationContext.xml这个文件必须存在.
为什么需要配置事务?
如果不配置,就需要我们手动提交控制事务;
事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!
完结撒花!!!!!!!!!!!