Spring5

历史版本下载:https://repo.spring.io/release/org/springframework/spring/

Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

SpringBoot是一个快速开发的脚手架,基于SpringBoot可以快速地开发单个微服务。约定大于配置

SpringCloud是基于SpringBoot实现的

IOC理论推导

private UserDao userDao;
//利用set进行动态实现值的注入
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}
  • 之前,程序是主动创建对象,控制权在程序员手上。导致用户的需求变化可能导致我们需要去改代码
  • 使用set注入后,程序不再有主动性,而是被动的接收对象

这种思想从本质上解决了问题,我们不用再去管理对象的创建了,实现了程序的DIY功能。这是IOC的原型

HelloSpring

实体类

public class Hello {
    private String str;

    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

beans.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">

    <!--
    使用Spring来创建对象,,在Spring这些都成为Bean
        java写法:类型 变量名 = new 类型()
                 Hello hello = new Hello()
        Spring写法:id=变量名  class=new 对象 property相当于给对象中的属性设置值
    -->
    <bean id="hello" class="com.zaughter.pojo.Hello">
        <!--name是对象set方法setxxx后面单词的小写,Spring核心就是set方法-->
        <!--value:具体的之,剧本数据类型   ref:引用Spring容器中创建好的对象-->
        <property name="str" value="Spring"/>
    </bean>
</beans>

测试

public class MyTest {
    public static void main(String[] args) {
        //获取Spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //我们的对象现在都在Spring中管理,我们要使用,直接去里面取出来就可以
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

创建对象方式

无参构造(默认)

<bean id="user" class="com.zaughter.pojo.User">
    <property name="name" value="zaughter"/>
</bean>

有参构造

private String name;

public User(String name){
        System.out.println("User的无参构造");
    }
1.下标赋值
<bean id="user" class="com.zaughter.pojo.User">
	<constructor-arg index="0" value="Z呵呵"/>
</bean>

下标是指有参构造中传入的参数

2.类型匹配
<bean id="user" class="com.zaughter.pojo.User">
	<constructor-arg type="java.lang.String" value="Z呵呵"/>
</bean>

通过类型来匹配

  • 基本类型可以直接写,但是String不是基本类型,要写全路径
  • 如果有参构造参数中有两个同类型参数则会按顺序
3.直接通过参数名
<bean id="user" class="com.zaughter.pojo.User">
	<constructor-arg name="name" value="Z呵呵"/>
</bean>

Spring配置

别名alias

通过alias,可以给bean对象起一个别名

此时,本名与别名都可以调用这个对象

Bean的配置

  • id:bean对象的唯一标识符,相当于对象名
  • class:bean对象所对应的全限定名:包名+类型
  • name:也是别名,而且name可以取多个别名name="asaf,fafa"空格,逗号,分号都可以用来做间隔符
  • scope:作用域

import

假设现在项目中有多个人开发,有多个beans.xml(没人负责的类不同,不同的类注册了不同的beans.xml)

可以创建一个总的bean.xml,通过import将所有人的beans.xml合并为一个总的

最后使用的时候直接使用总的配置

依赖注入(DI)

构造器注入

前文已说

Set方式注入(重点)

  • 依赖注入本质是Set注入
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性由容器来注入
<bean id="address" class="com.zaughter.pojo.Address"/>
<bean id="student" class="com.zaughter.pojo.Student">
    <!--普通注入,value-->
    <property name="name" value="Z呵呵"/>
    <!--bean注入,ref-->
    <property name="address" ref="address"/>
    <!--数组-->
    <property name="books">
        <array>
            <value>红楼梦</value>
            <value>西游记</value>
            <value>水浒传</value>
            <value>三国演义</value>
        </array>
    </property>
    <!--List-->
    <property name="hobbies">
        <list>
            <value>听歌</value>
            <value>看电影</value>
            <value>打游戏</value>
        </list>
    </property>
    <!--Map-->
    <property name="card">
        <map>
            <entry key="1" value="一号人物"/>
            <entry key="2" value="二号人物"/>
        </map>
    </property>
    <!--Set-->
    <property name="games">
        <set>
            <value>LOL</value>
            <value>WOW</value>
        </set>
    </property>
    <!--null-->
    <property name="wife">
        <null/>
    </property>
    <!--Properties-->
    <property name="info">
        <props>
            <prop key="学号">20032</prop>
            <prop key="姓名">zaughter</prop>
        </props>
    </property>
</bean>

扩展方式注入

我们可以使用p命名空间和c命名空间进行注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       <!--下方两个分别为p,c命名空间的xml约束-->
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入,可以通过set方法注入属性的值-->
    <bean id="user" class="com.zaughter.pojo.User" p:name="zaughter" p:age="18"/>

    <!--c命名空间注入,可以通过构造器注入-->
    <bean id="user2" class="com.zaughter.pojo.User" c:age="18" c:name="zhehe"/>
</beans>

Bean作用域

singleton单例默示(默认)

无论通过getBean拿到几个对象,其实他们都是同一个对象

prototype原型默示

每次从容器中get的时候都会产生一个新对象

其他作用域

request,session,application,websocket这些只能在web开发里使用

Bean的自动装配

  • 自动装配是Spring满足bean依赖的一种方式
  • Spring会在上下文中自动寻找,并自动给bean装配属性

在Spring中有三种装配的方式

  1. 在xml中显示的配置
  2. 在java中显示配置
  3. 隐式的自动装配【重要】

ByName自动装配

原理:会自动在容器上下文中查询和自己对象set方法后面的值对应的bean_id

cat

public class Cat {
    public void shout(){
        System.out.println("喵");
    }
}

people

public class People {
    private Cat cat;
    private String name;

    @Override
    public String toString() {...}

    public Cat getCat() {...}
    public void setCat(Cat cat) {...}
    
    public String getName() {...}
    public void setName(String name) {...}
}

xml

<bean id="cat" class="com.zaughter.pojo.Cat"/>
<bean id="people" class="com.zaughter.pojo.People" autowire="byName">
    <property name="name" value="zaughter"/>
</bean>

people对象中有方法serCat,于是去找bean_id为cat的对象,如果有就自动装配

ByType自动装配

接上文

原理:会自动在容器上下文中查询和自己对象属性类型相同的bean,如果有多个,会直接报错(需要保证该类型的bean全局唯一)

<bean id="cat" class="com.zaughter.pojo.Cat"/>
<bean id="people" class="com.zaughter.pojo.People" autowire="byType">
    <property name="name" value="zaughter"/>
</bean>

使用注解实现自动装配

@Autowired

要使用注解:

  • 需要导入约束
  • 需要配置注解的支持
    • xmlns:context=“http://www.springframework.org/schema/context”
    • http://www.springframework.org/schema/context
    • http://www.springframework.org/schema/context/spring-context.xsd
    • context:annotation-config/
<?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:annotation-config/>
    
</beans>

直接在属性或set方法上使用即可

@Autowired
private Cat cat;
//如果在属性上使用,那么就不会用到set方法了 

@Autowired 注解注入时首先根据byType注入,当接口存在多个实现类且使用@Service注解的默认bean名字时,根据byName注入。在进行byName时,如果bean_id都不是set方法后面的值(setDog,结果id为dog222),可以额外加上注解@Qualifier(value=“xxxx”)来指定id

@Resource

这个是java自带的,不用导入配置支持

是线byName再byType。可以通过@Resource(name=“xxxx”)来指定

使用注解开发

在Spring4之后,使用注解必须要保证aop的包导入了

<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.zaughter"/>
  1. bean

@Component等价于在bean中注册,id默认为类的小写

  1. 属性如何注入

@Value相当于用property给bean对象赋值。可以放在属性或者set方法上面

  1. 衍生的注解
  • @Component有几个衍生注解,我们在web开发中会按照mvc三层架构分层。下面功能一样,只是习惯用专门的注解来做区分

    • dao【@Repository】

    • service【@Service】

    • controller【@Controller】

  1. 自动装配

@Autowired或者@Resource

  1. 作用

`@Scope

小结

  • xml更加万能,适用于任何场所,维护方便。注解不是自己类使用不了,维护相对复杂
  • 最佳实践
    • xml用来管理bean
    • 注解只负责完成属性的注入

使用Java的方式配置Spring

可以不适用Spring的xml配置,全权交给Java来做

JavaConfig是Spring的一个子项目,在Spring4之后成为了一个核心功能

实体类

//这个注解是为了说明这个类被Spring接管了,注册到了容器中
@Component
public class User {
    @Value("zaughter")//给属性注入值
    private String name;
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

配置类

@Configuration
//这个也会被Spring容器托管,注册到容器中,因为他本来就是一个@Component。
//@Configuration代表这是一个配置类就像beans.xml
public class MyConfig {
    @Bean//注册一个bean,id为方法的名字,class为方法的返回值
    public User getUser(){
        return new User();
    }
}

测试类

public class MyTest {
    public static void main(String[] args) {
        //如果完全使用了配置类方式去做,我们要通过AnnotationConfigApplicationContext上下文获取容器,通过配置类的class对象加载
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User getUser = (User) context.getBean("getUser");
        System.out.println(getUser);
    }
}

代理模式

代理模式就是SpringAOP的底层

静态代理

动态代理

动态代理的代理类是动态生成的

动态代理分为两大类:基于接口的动态代理,基于类的动态代理

  • 基于接口:JDK动态代理。。。
  • 基于类:cglib。。。
  • java字节码实现:javasist

需要了解两个类:Proxy:代理, :调用处理程序

以房屋出租为例

接口Rent

public interface Rent {
    public void rent();
}

房东Host

//房东,实现了Rent接口
public class Host implements Rent {
    public void rent() {
        System.out.println("房东要出租房子");
    }
}

动态代理类

//这个类用来自动生成代理类
public class ProxyInvocationHandler implements InvocationHandler {
    //被代理的接口
    private Rent rent;

    public void setRent(Rent rent) {
        this.rent = rent;
    }

    //生成得到代理类
    public Object getProxy(){
        //参数分别为:类加载器,接口,InvocationHandler(也就是这个类本身,所以用this)
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                rent.getClass().getInterfaces(),this);
    }

    //处理代理实例并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        seeHouse();
        //动态代理的本质就是使用反射机制实现
        Object result = method.invoke(rent, args);
        return result;
    }

    //程序扩展内容,利用代理可以不改变Host类rent方法的代码
    public void seeHouse(){
        System.out.println("中介带看房子");
    }
}

租房人Client

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();
    }
}

把动态代理当作工具类

public class ProxyInvocationHandler implements InvocationHandler {
    private Object = target;

    public void setTarget(Object target) {
        this.target = target;
    }

    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                target.getClass().getInterfaces(),this);
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        log(method.getName());
        Object result = method.invoke(target, args);
        return result;
    }

    public void log(String msg){
        System.out.println("执行了"+msg+"方法");
    }
}

动态代理优点:

  • 可以使真实角色的操作更加纯粹(Host),不用关注一些公共的业务(seeHouse方法)
  • 公共业务交给代理角色,实现了业务的分工
  • 公共业务发生扩展的时候,方便集中管理
  • 一个动态代理类代理的使一个接口,一般就是对应的一类业务
  • 一个动态代理类可以代理多个类,只要是实现了同一个接口即可

AOP(关键)

AOP是用来处理公共事务的代码,以日志功能为例:如果我们有许多类,我们不好将生成日志这一方法逐个添加到每个类中(从上面的知识我们可以知道,我们可以把公共事务交给代理,而AOP就是这个思想的升级)。同样的,随着系统越来越完善,类似这样的非核心业务也会越来越多,比如权限,异常处理,性能监控,性能监控等

AOP中,我们将这些公共事务单独提取出来,横切在核心代码上

使用Spring实现AOP

需要导入aspectjweaver包(AOP织入包)

方式一:使用Spring的API接口

xml

<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop约束-->
<aop:config>
   <!--切入点,expression:表达式,execution(要执行的位置:修饰词 返回值 类名 方法名 参数)-->
   <aop:pointcut id="pointcut" expression="execution(* com.zaughter.service.UserServiceImpl.*(..))"/>
   
   <!--执行环绕增强,log和after是两个自己洗的类-->
   <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
   <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>

log类

public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //target:参数
    //target:目标对象
    public void before(Method method, Object[] target, Object o) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

afterLog类

public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
    }
}

expression="execution(* com.zaughter.service.UserServiceImpl.*(..))

execution参数分析:

  • 第一个参数为返回类型,这里用*代表所有类型
  • 包名,如果在包名后面加两个句点,则表示当前包以及他的所有子包
  • 这里直接写到了UserServiceImpl类,如果用*则代表所有类
  • *(…)中,*表示所有方法。(…)代表方法的参数,用两个句点表示任何参数

方式二:自定义类实现AOP[主要是切面]

自定义类

public class DiyPointCut {
    public void before(){
        System.out.println("=============方法执行前=============");
    }

    public void after(){
        System.out.println("=============方法执行后=============");
    }
}

xml

<!--方式二:自定义类-->
<bean id="diy" class="com.zaughter.diy.DiyPointCut"/>
<aop:config>
    <!--自定义切面,ref:要引用的类-->
    <aop:aspect ref="diy">
        <!--切入点-->
        <aop:pointcut id="point" expression="execution(* com.zaughter.service.UserServiceImpl.*(..))"/>
        <!--通知-->
        <aop:before method="before" pointcut-ref="point"/>
        <aop:after method="after" pointcut-ref="point"/>
    </aop:aspect>
</aop:config>

方式三:使用注解实现AOP

使用注解的类

//方式三:使用注解方式实现AOP
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.zaughter.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("=============方法执行前===============");
    }

    @After("execution(* com.zaughter.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("=============方法执行后===============");
    }

    //在环绕增强中我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.zaughter.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("-------------环绕前-------------");
        //执行方法
        Object proceed = joinPoint.proceed();
        System.out.println("-------------环绕后-------------");
    }
}

xml

<!--方式三-->
<bean id="annotationPointCut" class="com.zaughter.diy.AnnotationPointCut"/>
<!--开启注解支持  JDK(默认)   cglib:proxy-target-class="true"-->
<aop:aspectj-autoproxy/>

整合Mybatis

步骤:

  1. 导入jar包
    • junit
    • mybatis
    • mysql
    • spring相关的
    • aop织入(aspectjweaver)
    • mybatis-spring
  2. 编写配置文件
  3. 测试

Mybatis-spring

  1. 编写数据源
  2. sqlSessionFactory
  3. sqlSessionTemplate
  4. 给接口加实现类
  5. 将自己写的实现类注入到Spring中
  6. 测试

分析例子:

我们要写一个查看数据库表中所有数据的事务

  • 先写出他的接口

UserMapper

public interface UserMapper {
    public List<User> selectUser();
}
  • 接着通过xml文件实现接口

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zaughter.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select * from mybatis.user
    </select>
</mapper>
  • 如今我们将Mybatis与Spring整合,Mybatis的所有内容都可以写到Spring的xml文件中,但是一般我们还是留下Mybatis的xml文件用来专门写别名(typeAliases)和设置(比如用setting开始日志功能)

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <package name="com.zaughter.pojo"/>
    </typeAliases>

    <!--设置-->
</configuration>

spring-dao.xml

SqlSessionTemplate是SqlSession的一个是西安,可以无缝替代SqlSession而且他是线程安全的

<?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
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--DataSource:使用Spring的数据源替换Mybatis的配置(代替Mybatis的environments标签),我们这里使用Spring提供的JDBC-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--原先我们借助mybatis-config.xml的内容来创建sqlSessionFactory,现在我们直接通过上方替代Mybatis的数据源来生成-->
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定Mybatis配置文件-->
        <!--第一个里面可能有别名和设置,第二个代表的是实现接口的那些xml文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/zaughter/mapper/*.xml"/>
    </bean>

    <!--SqlSessionTemplate(Template是模板的意思)就是我们使用的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--只能用构造器注入SqlSessionTemplate(不给参数赋值会报错),因为他没有set方法-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>
  • 与Mybatis相比,我们需要多出来一个接口的实现类。原因:在最终测试中,我们需要通过ClassPathXmlApplicationContext对象来从Spring容器中调取Bean对象,使用Bean对象的方法实现程序功能。但是Spring万物皆注入,我们需要set方法,所以额外增加一个接口实现类,在其中写上set方法。这样我们就可以把他UserMapper注入进去了

UserMapperImpl

public class UserMapperImpl implements UserMapper{
    //我们的原来所有操作都是用sqlSession来执行
    //现在我们都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }
	
    //现在我们要把这个类注入到Spring中,也就是以后调用的Bean对象是这个类。所以要写上selectUser方法,而这个方法的返回值就是我们通过xml文件实现的接口的selectUser方法的返回值
    public List<User> selectUser() {
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

注册

把spring-dao.xml中通过SqlSessionTemplate创建的sqlSession注入到UserMapperImpl类中的sqlSession中

<bean id="userMapper" class="com.zaughter.mapper.UserMapperImpl">
    <property name="sqlSession" ref="sqlSession"/>
</bean>
  • 测试类
public class MyTest {
    @Test
    public void test() throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        for (User user : userMapper.selectUser()) {
            System.out.println(user);
        }
    }
}

这里ClassPathXmlApplicationContext调用的文件是applicationContext.xml,原因如下:

  • 这其实是一个改进后的结果,我们额外创建一个叫applicationContext.xml的文件,通过import导入spring-dao.xml(改进前,测试类里面调用的就是他)。这样我们就可以把spring-dao.xml内容固定,而applicationContext就作为最终整合(后面mvc也导入到他里面)

SqlSessionDaoSupport

上面我们提到了SqlSessionTemplate可以代替SqlSession,现在我们可以更进一步

原先内容

private SqlSessionTemplate sqlSession;//先定义出来,下面用set方法注入
public void setSqlSession(SqlSessionTemplate sqlSession) {
    this.sqlSession = sqlSession;
}
public List<User> selectUser() {
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    return mapper.selectUser();
}

现在

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    public List<User> selectUser() {
        SqlSession sqlSession = getSqlSession();//直接得到,不需要注入
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }
}

整合精简版

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    public List<User> selectUser() {
        return getSqlSession().getMapper(UserMapper.class).selectUser();
    }
}

注册

现在我们注册后不用再注入sqlSession了(也就是说明通过模板创建sqlSession的那一步也可以省略 ),但是他的父类需要注入

<bean id="userMapper2" class="com.zaughter.mapper.UserMapperImpl2">
    <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

声明式事务

spring中的事务管理

  • 声明式事务:AOP
  • 编程式事务:要改变原本代码

为了保证不发生数据提交不一致的情况。

声明式事务:AOP

UserMapper

public interface UserMapper {
    public List<User> selectUser();
    public int addUser(User user);
    public int deleteUser(int id);
}

这里我们在xml中实现的时候,故意把deleteUser的SQL语句写错,模拟事务出错情况

spring-dao.xml完成声明式事务

<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!--结合AOP实现事务的织入-->
<!--配置事务通知:-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!--给哪些方法配置事务-->
    <!--配置事务的传播特性: propagation="" 默认REQUIRED-->
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
    <aop:pointcut id="txPoint" expression="execution(* com.zaughter.mapper.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值