欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring、mybatis、事务项目整合,附完整代码和数据库文件

程序员文章站 2022-06-23 23:40:50
配置依赖项 pom.xml

配置依赖项

pom.xml

<project xmlns="http://maven.apache.org/pom/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <groupid>my_ms</groupid>
    <artifactid>my_ms</artifactid>
    <version>0.0.1-snapshot</version>
    <!-- 持久层依赖:mysql驱动、dbcp连接池、mybatis、mybatis和spring整合依赖、spring依赖 -->
    <dependencies>
        <!-- spring ioc组件需要的依赖包 -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-expression</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <!-- 基于aspectj的aop依赖包 -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aspects</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <dependency>
            <groupid>aopalliance</groupid>
            <artifactid>aopalliance</artifactid>
            <version>1.0</version>
        </dependency>
        <!-- spring 事务管理和jdbc依赖包 -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-tx</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-jdbc</artifactid>
            <version>5.2.1.release</version>
        </dependency>
        <!-- spring 单元测试组件包 -->
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-test</artifactid>
            <version>5.2.1.release</version>
            <scope>test</scope>
        </dependency>
        <!-- 单元测试junit -->
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- mysql驱动 -->
        <dependency>
            <groupid>mysql</groupid>
            <artifactid>mysql-connector-java</artifactid>
            <version>8.0.18</version>
        </dependency>
        <!-- dbcp连接池依赖包 -->
        <dependency>
            <groupid>commons-dbcp</groupid>
            <artifactid>commons-dbcp</artifactid>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupid>javax.annotation</groupid>
            <artifactid>javax.annotation-api</artifactid>
            <version>1.3.1</version>
        </dependency>
        <!-- mybatis依赖 -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis</artifactid>
            <version>3.5.3</version>
        </dependency>
        <!-- mybatis-spring整合依赖 -->
        <dependency>
            <groupid>org.mybatis</groupid>
            <artifactid>mybatis-spring</artifactid>
            <version>2.0.3</version>
        </dependency>
    </dependencies>
    <!-- 业务层依赖:aop相关依赖 -->
    <build>
        <plugins>
            <!-- 配置maven的jdk编译级别 -->
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-compiler-plugin</artifactid>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

创建配置文件

applicationcontext-dao.xml(持久层)

路径:src/main/resources/spring/applicationcontext-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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
        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">
    <!-- 配置数据源 -->
    <bean id="datasource"
        class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
        <property name="driverclassname"
            value="com.mysql.cj.jdbc.driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/demo?useunicode=true&amp;characterencoding=utf-8&amp;servertimezone=asia/shanghai"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!-- 配置sqlsessionfactory -->
    <bean id="sqlsessionfactory"
        class="org.mybatis.spring.sqlsessionfactorybean">
        <!-- 注入datasource -->
        <property name="datasource" ref="datasource"></property>
        <!-- mybatis批量别名配置 -->
        <property name="typealiasespackage" value="com.cyb.ms.po"></property>
        <!-- 注入mybatis的全局配置文件路径(该部分可以被省略) -->
        <!-- <property name="configlocation" value="mybatis/sqlmapconfig.xml"></property> -->
    </bean>
    <!-- 相当于配置之前的accountdao持久层bean -->
    <!-- 配置mapper代理对象方式一: mapperfactorybean -->
    <!-- 通过mapperfactorybean生成的代理对象,一次只能针对一个接口进行生成 -->
    <!-- 注意事项:mapper接口类和mapper映射文件同包同名 -->
    <bean id="accountmapper"
        class="org.mybatis.spring.mapper.mapperfactorybean">
        <!-- 注入sqlsessionfactory -->
        <property name="sqlsessionfactory" ref="sqlsessionfactory"></property>
        <!-- 注入目标接口类 -->
        <property name="mapperinterface"
            value="com.cyb.ms.mapper.accountmapper"></property>
    </bean>
</beans>

方式二(推荐)

<?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"
    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
        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">
    <!-- 配置数据源 -->
    <bean id="datasource"
        class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
        <property name="driverclassname"
            value="com.mysql.cj.jdbc.driver"></property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/demo?useunicode=true&amp;characterencoding=utf-8&amp;servertimezone=asia/shanghai"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!-- 配置sqlsessionfactory -->
    <bean id="sqlsessionfactory"
        class="org.mybatis.spring.sqlsessionfactorybean">
        <!-- 注入datasource -->
        <property name="datasource" ref="datasource"></property>
        <!-- mybatis批量别名配置 -->
        <property name="typealiasespackage" value="com.cyb.ms.po"></property>
        <!-- 注入mybatis的全局配置文件路径(该部分可以被省略) -->
        <!-- <property name="configlocation" value="mybatis/sqlmapconfig.xml"></property> -->
    </bean>
    <!-- 配置mapper代理对象方式二:mapperscannerconfigurer -->
    <!-- 批量代理对象的生成 -->
    <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
        <!-- 指定需要生成代理的接口所在的包名 -->
        <property name="basepackage" value="com.cyb.ms.mapper"></property>
        <!-- 注意事项:不要配置sqlsessionfactory -->
        <!-- <property name="sqlsessionfactory" ref=""></property> -->
    </bean>
</beans>

applicationcontext-service.xml(业务层)

 路径:src/main/resources/spring/applicationcontext-service.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"
    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
        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">
    <!-- 扫描业务bean -->
    <context:component-scan
        base-package="com.cyb.ms.service"></context:component-scan>
</beans>

applicationcontext-tx.xml(事务)

 路径:src/main/resources/spring/applicationcontext-tx.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"
    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
        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">

    <!-- 配置平台事务管理器 -->
    <bean id="transactionmanager"
        class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
        <property name="datasource" ref="datasource"></property>
    </bean>
    <!-- 事务通知 -->
    <!-- tx:advice:对应的处理器类是transactioninterceptor类(实现了methodinterceptor) -->
    <!-- transactioninterceptor类实现事务是通过transaction-manager属性指定的值进行事务管理 -->
    <tx:advice id="txadvice"
        transaction-manager="transactionmanager">
        <!-- 设置事务管理信息 -->
        <tx:attributes>
            <!-- 增删改使用required事务传播行为 -->
            <!-- 查询使用read-only -->
            <tx:method name="transfer*" propagation="required"
                isolation="default" />
        </tx:attributes>
    </tx:advice>
    <!-- 基于aspectj+xml方式实现声明式事务 -->
    <aop:config>
        <!-- aop:advisor标签使用的是传统spring aop开发方式实现的 -->
        <!-- spring已经实现了该增强功能,spring使用的是实现methodinterceptor接口的方式实现的 -->
        <aop:advisor advice-ref="txadvice"
            pointcut="execution(* *..*.*serviceimpl.*(..))" />
    </aop:config>
</beans>

数据层

创建包:com.cyb.ms.mapper

 accountmapper.java

package com.cyb.ms.mapper;

import org.apache.ibatis.annotations.param;

public interface accountmapper {
    void update(@param("name") string name, @param("money") int money);

    int querymoney(string name);
}

accountmapper.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.cyb.ms.mapper.accountmapper">
    <!-- 查询 -->
    <select id="querymoney" parametertype="string" resulttype="int">
        select money from s_account where name = #{name}
    </select>
    <!-- 修改 -->
    <update id="update" parametertype="map">
        update s_account set money=#{money} where name = #{name}
    </update>
</mapper>

业务层

创建包:com.cyb.ms.service

accountservice.java

package com.cyb.ms.service;

public interface accountservice {
    void transfer(string from, string to, int money);
}

accountserviceimpl.java

package com.cyb.ms.service;

import javax.annotation.resource;
import org.springframework.stereotype.service;
import com.cyb.ms.mapper.accountmapper;

@service
public class accountserviceimpl implements accountservice {
    @resource
    private accountmapper mapper;

    @override
    public void transfer(string from, string to, int money) {
        // 先查询from账户的钱
        int frommoney =  mapper.querymoney(from);
        // 对from账户进行扣钱操作
        mapper.update(from, frommoney - money);
        // 先查询to账户的钱
        int tomoney = mapper.querymoney(to);
        // 对to账户进行加钱操作
        mapper.update(to, tomoney + money);
    }
}

测试类

package com.cyb.ms.service;

import javax.annotation.resource;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;

@runwith(springjunit4classrunner.class)
@contextconfiguration(locations = { "classpath:spring/applicationcontext-*.xml"})
public class accountservicetest {

    @resource
    private accountservice service;

    @test
    public void testtransfer() {
        service.transfer("老公", "老婆", 1000);
    }
}

项目结构图

spring、mybatis、事务项目整合,附完整代码和数据库文件

 

 

 表结构

spring、mybatis、事务项目整合,附完整代码和数据库文件