Java学习路线-61:MyBatis声明式事务

简介: Java学习路线-61:MyBatis声明式事务

1、完整配置

(1)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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <!-- 配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/data"/>
        <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"/>
        <property name="configLocation" value="mybatis-config.xml"/>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="StudentDao" class="com.pengshiyu.mybatis.dao.impl.StudentDaoImpl">
        <property name="sqlSession" ref="sqlSessionTemplate"/>
    </bean>

    <!--  声明式事务管理-->
    <!--  配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- 配置事务的传播特性-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="pointcut"
        expression="execution(* com.pengshiyu.mybatis.dao.impl.*.* (..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
</beans>

(2)mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd";>

<configuration>

<settings>
<!-- 打印sql日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

<typeAliases>
<package name="com.pengshiyu.mybatis.entity"/>
</typeAliases>

<mappers>
<mapper resource="StudentMapper.xml"/>
</mappers>

</configuration>

(3)StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd";>

<mapper namespace="com.pengshiyu.mybatis.entity.StudentMapper">
<select id="getById" resultType="Student">
select * from students where id = #{id}
</select>

<select id="selectAll" resultType="Student">
select * from students
</select>

<update id="updateById" parameterType="Student">
update students set age = #{age} where id = #{id}
</update>

<delete id="deleteById" parameterType="Student">
<!-- 故意将sql写错,触发事务管理-->
deletes from students where id = #{id}
</delete>

</mapper>

2、DAO

(1)接口

package com.pengshiyu.mybatis.dao;

import com.pengshiyu.mybatis.entity.Student;

public interface StudentDao {

public void update(Student studentA, int id);
public Student getById(int id);

}

(2)实现

package com.pengshiyu.mybatis.dao.impl;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.mybatis.spring.SqlSessionTemplate;

public class StudentDaoImpl implements StudentDao {
private SqlSessionTemplate sqlSession;

@Override
public Student getById(int id) {
return sqlSession.selectOne("com.pengshiyu.mybatis.entity.StudentMapper.getById", id);
}

@Override
public void update(Student studentA, int id) {
sqlSession.update("com.pengshiyu.mybatis.entity.StudentMapper.updateById", studentA);
sqlSession.delete("com.pengshiyu.mybatis.entity.StudentMapper.deleteById", id);
}

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

3、测试

package com.pengshiyu.mybatis.test;

import com.pengshiyu.mybatis.dao.StudentDao;
import com.pengshiyu.mybatis.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Demo {
public static void main(String[] args) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

StudentDao studentDao = (StudentDao) context.getBean("StudentDao");

Student studentA = studentDao.getById(1);
studentA.setAge(25);

studentDao.update(studentA, 3);
}
}
            </div>
目录
相关文章
|
6天前
|
SQL Java 数据库
2025 年 Java 从零基础小白到编程高手的详细学习路线攻略
2025年Java学习路线涵盖基础语法、面向对象、数据库、JavaWeb、Spring全家桶、分布式、云原生与高并发技术,结合实战项目与源码分析,助力零基础学员系统掌握Java开发技能,从入门到精通,全面提升竞争力,顺利进阶编程高手。
139 1
|
1月前
|
安全 Java 数据库连接
2025 年最新 Java 学习路线图含实操指南助你高效入门 Java 编程掌握核心技能
2025年最新Java学习路线图,涵盖基础环境搭建、核心特性(如密封类、虚拟线程)、模块化开发、响应式编程、主流框架(Spring Boot 3、Spring Security 6)、数据库操作(JPA + Hibernate 6)及微服务实战,助你掌握企业级开发技能。
245 3
|
3月前
|
前端开发 JavaScript Java
Java 学习路线规划及项目案例中的技术栈应用解析
内容包括:**Java 17核心特性**(如sealed class、record)与模块化开发;Spring Boot 3 + Spring Cloud微服务架构,涉及响应式编程(WebFlux)、多数据库持久化(JPA、R2DBC、MongoDB);云原生技术**如Docker、Kubernetes及CI/CD流程;性能优化(GraalVM Native Image、JVM调优);以及前后端分离开发(Vue 3、Spring Boot集成)。通过全栈电商平台项目实战,掌握从后端服务(用户、商品、订单)到前端应用(Vue 3、React Native)的全流程开发。
176 9
|
3月前
|
消息中间件 Java 微服务
2025 版 Java 学习路线实战指南从入门到精通
《Java学习路线实战指南(2025版)》是一份全面的Java开发学习手册,涵盖基础环境搭建、核心语法与新特性、数据结构与算法、微服务架构、云原生技术栈、AI融合及项目实战。内容包括JDK安装配置、IntelliJ IDEA设置、Records类与模式匹配增强、LeetCode题解、Spring Cloud微服务开发、Kubernetes部署、OpenAI API调用等。结合在线商城系统案例,采用Vue 3、Spring Boot 3.5、MySQL、Elasticsearch等技术,提供从理论到实践的完整路径,助力开发者掌握2025年最新趋势与最佳实践。
327 4
|
9天前
|
SQL 算法 Java
零基础到精通的史上最强 Java 学习路线图推荐
史上最全Java学习路线图,涵盖基础语法、面向对象、数据结构与算法、多线程、JVM、Spring框架、数据库及项目实战,助你从零基础到精通Java开发,附完整代码与工具推荐。
63 3
零基础到精通的史上最强 Java 学习路线图推荐
|
17天前
|
NoSQL Java 关系型数据库
超全 Java 学习路线,帮你系统掌握编程的超详细 Java 学习路线
本文为超全Java学习路线,涵盖基础语法、面向对象编程、数据结构与算法、多线程、JVM原理、主流框架(如Spring Boot)、数据库(MySQL、Redis)及项目实战等内容,助力从零基础到企业级开发高手的进阶之路。
108 2
|
20天前
|
前端开发 Java 数据库连接
帮助新手快速上手的 JAVA 学习路线最详细版涵盖从入门到进阶的 JAVA 学习路线
本Java学习路线涵盖从基础语法、面向对象、异常处理到高级框架、微服务、JVM调优等内容,适合新手入门到进阶,助力掌握企业级开发技能,快速成为合格Java开发者。
258 3
|
1月前
|
算法 Java 测试技术
适合新手小白的史上最强 Java 学习路线图从基础到进阶全程指南
本Java学习路线图涵盖从基础语法、面向对象、集合框架到进阶编程、JVM原理、Spring Boot框架,再到数据库操作与实战项目开发,内容系统全面,适合零基础新手入门与进阶提升,助力掌握Java全栈开发技能。
199 4
|
1月前
|
Java 数据库连接 微服务
零基础自学 Java 全栈必备最全学习路线及知识清单方向指引
本文为零基础学习者提供完整的Java全栈学习路线,涵盖Java基础、进阶、框架、项目实战及拓展方向,助你系统掌握全栈开发技能,快速成长为优秀Java工程师。
213 6
|
1月前
|
NoSQL Java 关系型数据库
Java 从入门到进阶完整学习路线图规划与实战开发最佳实践指南
本文为Java开发者提供从入门到进阶的完整学习路线图,涵盖基础语法、面向对象、数据结构与算法、并发编程、JVM调优、主流框架(如Spring Boot)、数据库操作(MySQL、Redis)、微服务架构及云原生开发等内容,并结合实战案例与最佳实践,助力高效掌握Java核心技术。
214 2