14 mybatis转钱和查询余额(优化后)

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>mybatisaddsub</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--打包类型-->
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!--所有依赖-->
    <dependencies>
        <!--MySQL驱动-->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</version>
        </dependency>

        <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.15</version>
        </dependency>


        <!--测试环境-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>



    </dependencies>



</project>

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>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/dict"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/AccountMapper.xml"></mapper>
    </mappers>
</configuration>

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="mapper.AccountMapper">

    <resultMap id="account" type="entity.Account">
        <id property="acid" column="acid" />
        <result property="acname" column="acname" />
        <result property="acpassword" column="acpassword" />
        <result property="acmoney" column="acmoney" />
    </resultMap>

    <select id="selectById" parameterType="java.lang.Integer" resultMap="account" >
        select * from account where acid = #{accountId}
    </select>

  <update id="updateById" >
      update account set acname = #{acname},acpassword = #{acpassword}, acmoney = #{acmoney}
      where acid= #{acid}
  </update>


</mapper>

Account.java


package entity;

public class Account {

    private Integer acid; // acc_id , accId
    private String acname;
    private String acpassword;
    private Integer acmoney;
    public Integer getAcid() {
        return acid;
    }

    public void setAcid(Integer acid) {
        this.acid = acid;
    }

    public String getAcname() {
        return acname;
    }

    public void setAcname(String acname) {
        this.acname = acname;
    }

    public String getAcpassword() {
        return acpassword;
    }

    public void setAcpassword(String acpassword) {
        this.acpassword = acpassword;
    }

    public Integer getAcmoney() {
        return acmoney;
    }

    public void setAcmoney(Integer acmoney) {
        this.acmoney = acmoney;
    }




}


AccountMapper.java


package mapper;

import entity.Account;

public interface AccountMapper {


    public int updateById(Account account);// update account set ... where acc_id = 101

    public Account selectById(Integer accountId);

}

AccountService.java


package service;

import entity.Account;

public interface AccountService {
    public boolean toMoney(Account account1,Account account2,int money);

    public Account getById(int id);
}

AccountServiceImpl.java


package service.impl;

import entity.Account;
import mapper.AccountMapper;
import org.apache.ibatis.session.SqlSession;
import service.AccountService;
import utils.DButil;

public class AccountServiceImpl implements AccountService {



    @Override
    public boolean toMoney(Account account1, Account account2, int money) {
        //
        int row1 = 0;
        int row2 = 0;
        SqlSession sqlSession = null;
        try {
            sqlSession = DButil.getSqlSession();
           AccountMapper mapper =  sqlSession.getMapper(AccountMapper.class);
            account1.setAcmoney(account1.getAcmoney()-money);
            account2.setAcmoney(account2.getAcmoney() +money);

            row1 =  mapper.updateById(account1); //  1
            //int x = 10/0; // exception
            row2 = mapper.updateById(account2);  // 1


          sqlSession.commit();



        } catch (Exception e) {
            sqlSession.rollback();

        } finally {
            DButil.closeSqlSession(sqlSession);
        }

    if(row1+row2 >=2)
        return true;
    else
        return false;
    }

    public Account getById(int id) {
        SqlSession sqlSession = DButil.getSqlSession();
      AccountMapper mapper =  sqlSession.getMapper(AccountMapper.class);
      Account account = mapper.selectById(id);
      DButil.closeSqlSession(sqlSession);
        return account;
    }



   
}

DButil.java


package utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

public class DButil {

    private static SqlSessionFactory sqlSessionFactory = null;



    static {
        //1.builder 一旦创建了SqlSessionFactory,就不再需要它
        //2

        try {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //根据资源文件创建工厂
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


    public static SqlSession getSqlSession(){
        //获得SqlSession
        return sqlSessionFactory.openSession(false);
    }

    public static void closeSqlSession(SqlSession sqlSession){
        if(sqlSession !=null)
            sqlSession.close();
    }



}

TestAccountService.java


import entity.Account;
import org.junit.Test;
import service.AccountService;
import service.impl.AccountServiceImpl;

public class TestAccountService {
    @Test
    public void testToMoney(){
        AccountService accountService = new AccountServiceImpl();
        Account account1 = accountService.getById(1);
        Account account2 = accountService.getById(2);

        int money1 = account1.getAcmoney();
        int money2 = account2.getAcmoney();
        System.out.println(money1);
        System.out.println(money2);

        boolean result = accountService.toMoney(account1,account2,20);
        System.out.println(result);
        money1 = account1.getAcmoney();
        money2 = account2.getAcmoney();
        System.out.println(money1);
        System.out.println(money2);
    }
}

相关推荐

  1. 14 mybatis查询余额(优化)

    2024-03-28 19:40:02       20 阅读
  2. 13 查询余额

    2024-03-28 19:40:02       15 阅读
  3. 查询到List再进行分页(mybatis-plus

    2024-03-28 19:40:02       30 阅读
  4. springMybatis的各种查询

    2024-03-28 19:40:02       12 阅读
  5. 【MySQL】子查询优化、排序优化覆盖索引

    2024-03-28 19:40:02       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-28 19:40:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-28 19:40:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-28 19:40:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-28 19:40:02       20 阅读

热门阅读

  1. 从fread 到 磁盘驱动

    2024-03-28 19:40:02       25 阅读
  2. 每天一个数据分析题(二百三十三)

    2024-03-28 19:40:02       23 阅读
  3. 只出现一次的数字——2个解题猜想

    2024-03-28 19:40:02       19 阅读
  4. 面试中高频出现的Redis面试题

    2024-03-28 19:40:02       24 阅读
  5. 【Hive】with 语法 vs cache table 语法

    2024-03-28 19:40:02       21 阅读
  6. C++进阶学习(5)继承中的重名成员与静态成员

    2024-03-28 19:40:02       19 阅读
  7. 每日一题 --- 反转字符串中的单词[力扣][Go]

    2024-03-28 19:40:02       22 阅读