美文网首页
mybatis-转账

mybatis-转账

作者: 李霖神谷 | 来源:发表于2019-12-04 17:20 被阅读0次

    需求:
    1.转账时,账户和密码不匹配返回错误信息
    2.余额不足是返回错误信息
    3.收款人账户和姓名不匹配返回错误信息
    由于没有设计前端,所以,传的数据都是自己设置的,业务都写在test上
    test层:

    public class test {
        @Test
        public  void test(){
            InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("Config.xml");
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession sqlSession=sqlSessionFactory.openSession();
            AccountMapper accountMapper=sqlSession.getMapper(AccountMapper.class);
            Account accountin=new Account();
            accountin.setId(1);
            accountin.setAccon("1");
            accountin.setPassword(111);
            Account accoin= accountMapper.isExistByap(accountin);
            Account accountout=new Account();
            accountout.setAccon("2");
            accountout.setName("zhao");
            Account accoout=accountMapper.isExistByan(accountout);
    //        判断转账的人账户密码是否有误,如果没有问题,判断转到的用户账号和姓名是否匹配
            if(accoin==null){
                System.out.println("账户或密码错误");
            }else {
                if (accoin.getMoney()<100){
                    System.out.println("余额不足");
                }else {
                    if (accoout == null) {
                        System.out.println("你转入的账户或姓名有误");
                    } else {
                        accoin.setMoney(accoin.getMoney() - 100.0);
                        accountMapper.transfer(accoin);
                        accoout.setMoney(accoout.getMoney()+100.0);
                        accountMapper.transfer(accoout);
                        sqlSession.commit();
                        System.out.println("转账成功");
                    }
                }
            }
    
        }
    }
    
    

    mapper 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.shuai.mapper.AccountMapper">
            <!--判断用户名和密码是否匹配-->
            <select id="isExistByap" resultType="Account" parameterType="Account">
                select  * from  account where  accon=#{accon} and password=#{password};
            </select>
            <!--判断用户名和姓名是否匹配-->
            <select id="isExistByan" resultType="Account" parameterType="Account">
                select  * from  account where  accon=#{accon} and name=#{name};
            </select>
            <update id="transfer"  parameterType="Account">
                    update  account set money=#{money} where accon=#{accon};
            </update>
    </mapper>
    

    mapper层:

    package com.shuai.mapper;
    
    
    import com.shuai.pojo.Account;
    
    public interface AccountMapper {
            Account isExistByap(Account account);
            Account isExistByan(Account account);
            void transfer(Account accout);
    }
    
    

    相关文章

      网友评论

          本文标题:mybatis-转账

          本文链接:https://www.haomeiwen.com/subject/jgmlgctx.html