模拟mybatis 将数据库中表中数据封装成集合对象

1.首先创造表头的类


public class stu {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "编号:   "+id+"   姓名:   "+name;
    }
}


2.转换方法

 public List<stu> FindAll(){
      Connection connection=null;
      Statement statement=null;
      ResultSet resultSet=null;
      ArrayList<stu> stus=null;

      try {
          //注册驱动
          Class.forName("com.mysql.jdbc.Driver");
          //拿到con对象
          connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db3", "root", "fjtgyxa828998");
          //写sql语句
          String sql="select * from stu";
          //获取SQL对象
          statement = connection.createStatement();
          //执行SQL语句
          resultSet = statement.executeQuery(sql);
          stus = new ArrayList<>();
          while(resultSet.next()){
              int id = resultSet.getInt("id");
              String name = resultSet.getString("name");
              stu stu = new stu();
              stu.setId(id);
              stu.setName(name);
              stus.add(stu);


          }


      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
      catch (SQLException e) {
          e.printStackTrace();
      }
      finally {
          if (resultSet!=null){
              try {
                  resultSet.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }

          }
          if (statement!=null){
              try {
                  statement.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }

          }
          if (connection!=null){
              try {
                  connection.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }

          }
      }
            return stus;

  }


3.展示方法

public static void main(String[] args) {
        List<stu> stus = new Damo2().FindAll();
        for(stu stu:stus){
            System.out.println(stu.toString());
        }
    }

4.结果

编号:   2   姓名:   jun
编号:   3   姓名:   feng
编号:   4   姓名:   niu
编号:   1   姓名:   jiang
编号:   5   姓名:   jiang1
编号:   6   姓名:   jiang2
编号:   7   姓名:   jiang3
编号:   8   姓名:   jiang4
编号:   9   姓名:   jiang5
编号:   10   姓名:   jiang5

Process finished with exit code 0


转载:https://blog.csdn.net/jiangjun_feng/article/details/107292084

附:mybatis原理:
1.手动写一个将数据库中查询的内容封装到对象中:
https://blog.csdn.net/yinbucheng/article/details/52037291?utm_medium=distribute.pc_relevant.none-task-blog-searchFromBaidu-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-searchFromBaidu-1.nonecase

2.Mybatis3源码分析(16)-Sql解析执行-结果集映射(ResultSetHandler)
https://blog.csdn.net/ashan_li/article/details/50379458

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值