봄 ----- 통합 MyBatis로, 트랜잭션 관리 구성 봄

준비 :
1. 완전한 MyBatis로 만들기
만들 매퍼에서 XML 패키지를 2, UserMapper.xml를 만들


导入依赖
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.2</version>
</dependency>
<!-- 如果xml在java文件下,就要设置资源过滤,过滤掉properties和xml-->

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

  • 데이터 소스의 1. 통합
<!--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/mybatis? useSSL=true;useUnicode=true;characterEncoding=utf8"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>
  • 2. 채우기 SqlSessionFactory는
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.yang.pojo"/>
    <!--<property name="configLocation" value="classpath:mybatis-config.xml"/>-->
    <property name="mapperLocations" value="classpath:com/yang/mapper/xml/*.xml"/>
    <!--<property name="typeAliasesPackage" value="com.yang.pojo"-->
</bean>
  • SQLSESSION를 작성합니다
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

만들기 UserMapperImpl 구현 클래스

package com.yang.mapper.impl;

import com.yang.mapper.UserMapper;
import com.yang.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;


import java.util.List;

public class UserMapperImpl implements UserMapper {

    //注入sqlSession
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession){
        this.sqlSession=sqlSession;
    }

public List<User> getUserList() {
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    return  mapper.getUserList();
}
       
  • 콩을 추가하십시오
<bean id="userMapperImpl" class="com.yang.mapper.impl.UserMapperImpl">
    <property name="sqlSession" ref="sqlSession"/>
</bean>
  • 5. 시험
@Test
public void test2(){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    UserMapper userMapperImpl = (UserMapper) context.getBean("userMapperImpl");

    for (User user : userMapperImpl.getUserList()) {
        System.out.println(user);
    }
}

통합 서비스 : 실패하는 동시에 이벤트의 성공을 보장하기 위해
헤더 파일 텍사스를 추가하는 기억

<!--整合事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <constructor-arg ref="dataSource"/>
</bean>
<!--配置事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <!--事务的传播特性-->
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="txPointCut" expression="execution(* com.yang.mapper.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
게시 80 개 원래 기사 · 원 찬양 7 · 전망 4751

추천

출처blog.csdn.net/y18791050779/article/details/105036274