【Mybatis01】实现缩小版银行转账+分页查询功能

一、实现转账

1.1 项目实现图和结构图
在这里插入图片描述在这里插入图片描述
1.1.1 pojo实体类

public class Account {        //用户账户表
    private int id;
    private String accNo;
    private int password;
    private Double balance;
    private String name;
public class Log {          //转账日志表
    private int id;
    private String accIn;
    private String accOut;
    private double money;

!分页查询表

    private int pageSize;   //当前页显示的数据   
    private int pageNumber;  //当前页数
    private long total;     //总数量
    private List<?> list;     //当前页显示的总数数据

1.2 全局文件mybatis.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>
    <typeAliases>
        <package name="com.bjsxt.pojo"/>
    </typeAliases>
    <environments default="default">
        <environment id="default">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">  //三种方式此种是数据库连接池方式
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
                <property name="username" value="root"/>
                <property name="password" value="1234"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com\bjsxt\mapper\AccountMapper.xml"/>   //加载转账的xml
        <mapper resource="com\bjsxt\mapper\LogMapper.xml"/>         //加载转账日志的xml
</mappers>
</configuration>

1.3 mapper.xml的编写(用来进行查询!!!)
在这里插入图片描述

AccountMapper.xml

1.3.1分析:

  • (确保转账用户的正确性)因为需要确认转账用户的账号密码输入正确,所以需要根据账号和密码进行表查询并且拿出查出来的对象,以作后续的余额更改操作!
  • (确保入帐用户的正确性)同上述,查表,取出对象!
  • (根据账号修改余额)根据账号,修改各自的余额!
<?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.bjsxt.mapper.AccountMapper">
    <!--根据账号和密码查询账户信息-->
    <select id="selByAccnoPwd" resultType="account" parameterType="account">
        select * from account where accno=#{accNo} and password=#{password}
    </select>
    <!--根据账号和姓名查询账户信息-->
    <select id="selByAccnoName"  resultType="account" parameterType="account">
        select * from account where accno=#{accNo} and name=#{name}
    </select>
    <!--根据账号修改余额-->
    <update id="updBalanceByAccno" parameterType="account">
        update account set  balance=balance+#{balance} where accno=#{accNo}
    </update>
</mapper>

1.3.2写完mapper然后写service层!

-分析
  • 转账可能出现很多种情况,所以需要一一考虑,但是可能在事先考虑的不是很周全,所以可以在编码过程中思考问题。
int transfer(Account accIn,Account accOut) throws IOException;

impl层

  • 加载流
  • InputStream is=Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is); SqlSession session=factory.openSession();
  • 判断账号密码是否一致
  • Account accOutSelect=session.selectOne("com.bjsxt.mapper.AccountMapper.selByAccnoPwd",accOut); -accSelect为查找出来的对象
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值