小白学习java第19天(下):spring

发布于:2025-05-18 ⋅ 阅读:(18) ⋅ 点赞:(0)

1.spring-mybatis整合

因此我们下面主要就是讲解,spring-mybatis怎么整合?(那么为什么需要整合,最主要就是事务这方面,因为在mybatis里面我们进行操作的时候都需要进行操作,我们根据spring的特性AOP,我们可以横向切面将其进行插入!!!)

要了解整合我们就需要回顾一下我们的mybatis是什么结构,有了结构之后我们才能知道是怎么进行整合的!!!

mybatis框架:

Spring-mybatis整合框架:

整合上图中的2:

spring对于new出的东西可以进行bean容器存储的,因此我们可以对mybatisConfig.xml这个文件处理,然后我希望就是mybatis原有的别名以及设置我是不希望进行变化的!!!

还有就是简化关系dataSource进行简化

以及绑定

<?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:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       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-3.0.xsd">

<!--DataSource,使用spring的数据源替换mybatis配置-->
    <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/account?
        useUnicode=true&amp;characterEncoding=utf-8&amp;
        allowMultiQueries=true&amp;useSSL=false&amp;
        serverTimezone=GMT%2b8&amp;allowPublicKeyRetrieval=true
"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

<!--  sqlSessionFactory创建配置省略-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
<!-- 绑定原有mybatis的配置文件
其实可以全部省略,但是我们还是希望在原有的mybatis里面进行一些别名,设置等配置
-->
        <property name="configLocation" value="classpath:Mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/xcl/dao/*.xml"/>
    </bean>

<!-- sqlSessionTemplate配置,其实就是我们的sqlSession一样只是在spring里面换了一个名字!-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>




</beans>

因为我们需要使用注入bean,因此我们是需要创建一个实体类,去实现接口,然后注入我们sqlSessionTemplate,然后再xml里面进行什么声明!

package com.xcl.dao;

import com.xcl.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImp implements UserMapper{
    //我们所有操作。都是使用sqlSession来执行,现在都是使用SqlSessionTemplate
    private SqlSessionTemplate sqlSessionTemplate;

    //set方法,注入sqlsessionTemplate
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }
    @Override
    public List<User> findAll() {
        UserMapper mapper = sqlSessionTemplate.getMapper(UserMapper.class);
        return mapper.findAll();
    }
}

自此我们spring-mybatis整合已经弄完了!

2.使用spring中的AOP对事务进行处理

下面我们举个例子:

我故意把delete写成deletee,使得我们插入了但是并没有对其进行删除,但是结果是报错了但是我们也同样进行插入!

因此我们需要对这些操作进行事务的管理,其实就是加一下配置就行!!!


<!--结合AOP实现事务的织入-->
<!-- 配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性-->
        <tx:attributes>
            <tx:method name="addUser" propagation="REQUIRED"/>
            <tx:method name="deleteUser" propagation="REQUIRED"/>
            <tx:method name="updateUser" propagation="REQUIRED"/>
            <tx:method name="find" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

<!-- 事务的切入点-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.xcl.dao.UserMapper.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>


网站公告

今日签到

点亮在社区的每一天
去签到