H2--小型數據庫

特色

  • 單一jar包引入,java語言開發
  • 可做爲內存數據
  • 提供jpa支持

demo

gitee地址

https://gitee.com/ichiva/h2-demo.git

主要依賴

<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.197</version>
        </dependency>

編寫測試用例

配置文件

spring.h2.driverClass=org.h2.Driver
spring.h2.jdbcUrl=jdbc:h2:./h2
spring.h2.username=sa
spring.h2.password=
public class Config {

    private static Map<String,String> map = new HashMap<>();

    static{
        try(InputStream inputStream = Config.class.getResourceAsStream("/application.properties")){
            Properties properties = new Properties();
            properties.load(inputStream);

            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()) {
                String key = (String) enumeration.nextElement();
                String val = properties.getProperty(key);
                map.put(key,val);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static String get(String key){
        return map.get(key);
    }
}

鏈接數據庫

@Data
public class H2Config {

    private String driverClass;
    private String jdbcUrl;
    private String username;
    private String password;

    public static H2Config getInstance(){
        H2Config config = new H2Config();
        config.setDriverClass(Config.get("spring.h2.driverClass"));
        config.setJdbcUrl(Config.get("spring.h2.jdbcUrl"));
        config.setUsername(Config.get("spring.h2.username"));
        config.setPassword(Config.get("spring.h2.password"));
        return config;
    }

    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        H2Config config = getInstance();
        Class.forName(config.getDriverClass());
        return DriverManager.getConnection(config.getJdbcUrl(),config.getUsername(),config.getPassword());
    }
}

建表

@Test
    public void createTable() throws Exception {
        InputStream inputStream = H2Base.class.getResourceAsStream("/application.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String driverClass = properties.getProperty("spring.h2.driverClass");
        String jdbcUrl = properties.getProperty("spring.h2.jdbcUrl");
        String username = properties.getProperty("spring.h2.username");
        String password = properties.getProperty("spring.h2.password");
        Class.forName(driverClass);

        String create_table_sql = "CREATE TABLE base_data (\n" +
                "\tid BIGINT(20) NOT NULL AUTO_INCREMENT,\n" +
                "\tval BLOB NOT NULL,\n" +
                "\tPRIMARY KEY (`id`)\n" +
                ")\n" +
                "COMMENT='基本的數據存儲'\n" +
                "AUTO_INCREMENT=1";

        Connection connection = DriverManager.getConnection(jdbcUrl,username,password);
        Statement st = connection.createStatement();
        System.out.println(st.execute(create_table_sql));
    }

寫入數據

@Test
    public void lastInsertId() throws Exception {
        String sql = "INSERT INTO base_data (val) VALUES (?)";
        String lastInsertID = "SELECT LAST_INSERT_ID()";
        try (Connection connection = H2Config.getConnection()){
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setBlob(1,new ByteArrayInputStream("ByteArrayInputStream".getBytes()));
            ps.executeUpdate();
            ResultSet res = connection.createStatement().executeQuery(lastInsertID);
            if (res.next()) {
                System.out.println(res.getLong(1));
            }
        }
    }

修改數據

遍歷

@Test
    public void select() throws Exception {
        String sql = "SELECT id,val FROM base_data";

        try (Connection connection = H2Config.getConnection()) {
            Statement st = connection.createStatement();
            ResultSet res = st.executeQuery(sql);
            while (res.next()) {
                long id = res.getLong("id");
                Blob val = res.getBlob("val");

                byte[] bytes = val.getBytes(0, (int) val.length());
                System.out.println("id = " + new String(bytes));
            }
        }
    }

總結,h2的使用相似sqlit,據測試,大多數狀況性能不如sqlit,通常用於原型開發 PS.做爲內存數據庫能夠用於生產環境java

gitee地址

https://gitee.com/ichiva/h2-demo.git
相關文章
相關標籤/搜索