ORM封装零散数据

ORM封装零散数据

在查询数据库的表得到的结果时,查询并取出的都是零散的数据,不能保存,在实际开发中,我们需要将零散的数据进行整理。

实体类:零散数据的载体

  • 在一行数据中,对多个零散的数据进行整理

  • 通过实体类(entity)的规则对零散数据进行封装。

  • 创建一个实体类,类名=数据库的表名,属性名=表的列名

  • 提供各属性的·get,set方法。

  • 提供无参构造,看情况添加有参构造。

实例:

测试类:

package com.qf.JDBC2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestORM {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<accounts> list = new ArrayList<>();
        try {
            connection = DBUtils.getConnection();
            preparedStatement = connection.prepareStatement("select * from accounts");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                String id=resultSet.getString("id");
                String name = resultSet.getString("name");
                String money = resultSet.getString("money");
//                System.out.println(id+"\t"+name+"\t"+money);
                //创建实体类对象,封装查询到的零散数据
                accounts accounts1 = new accounts();
                accounts1.setId(id);
                accounts1.setName(name);
                accounts1.setMoney(money);
//                System.out.println(accounts1);
                //创建一个list集合对象,把每次遍历到的结果存放在集合里面
                list.add(accounts1);

            }
            //集合遍历
            for (accounts accounts1:list){
                System.out.println(accounts1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closeAll(connection,preparedStatement,resultSet);
        }
    }
}

实体类:

package com.qf.JDBC2;
/**
 *entity实体类
 * 类名=表名,属性名=列名.
 */

public class accounts {
    private String id;
    private String name;
    private String money;
    public accounts(){

    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getMoney() {
        return money;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public accounts(String id, String name, String money){
        this.id=id;
        this.name = name;
        this.money = money;
    }

    @Override
    public String toString() {
        return "accounts{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", money='" + money + '\'' +
                '}';
    }
}

工具类:

package com.qf.JDBC2;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    //1.提供一个私有的静态常量,存储配置文件的map
    private  static final Properties PROPERTIES = new Properties();
    static {
        //2.拿到一个字节流,通过DBUtils类对象的一个方法拿到
        InputStream is = DBUtils.class.getResourceAsStream("/db.properties");
        try {
            PROPERTIES.load(is);//3.通过流,将配置文件内容以键值对的形式存储到PROPERTIES集合
            Class.forName(PROPERTIES.getProperty("driver"));//通过PROPERTIES.getProperty去拿那个键
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection= DriverManager.getConnection(PROPERTIES.getProperty("url"),PROPERTIES.getProperty("name"),PROPERTIES.getProperty("password"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
    public static void closeAll(Connection connection, Statement statement,ResultSet resultSet){
        try {
            if(resultSet!=null){
                resultSet.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

运行结果::

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
accounts{id='1', name='A', money='1000.0'}
accounts{id='2', name='B', money='1000.0'}
accounts{id='3', name='C', money='1000.0'}
accounts{id='4', name='null', money='null'}
accounts{id='5', name='null', money='null'}
accounts{id='6', name='A', money='1000.0'}
accounts{id='7', name='B', money='1000.0'}
accounts{id='8', name='C', money='1000.0'}
accounts{id='10', name='zht', money='1234.0'}
accounts{id='12', name='zht', money='1234.0'}
accounts{id='13', name='zht', money='1234.0'}
accounts{id='14', name='zht', money='1234.0'}

Process finished with exit code 0

 

posted on 2023-01-29 17:31  张铁蛋666  阅读(17)  评论(0编辑  收藏  举报

导航