Hibernate操纵PostgreSQL数据库

Hibernate操纵PostgreSQL数据库

环境

java 12.0.1
Apache Maven 3.6.3
MySQL Server version: 5.7.18-20170830-log 20170531
hibernate-core-5.4.27.Final.jar
PostgreSQL 12.4
IntelliJ IDEA 2020.2.3 (Ultimate Edition)

Maven配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo.postgresql</groupId>
    <artifactId>PostgreSQL</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-agroal</artifactId>
            <version>5.4.27.Final</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>12</source>
                    <target>12</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
    </build>


</project>

Hibernate配置文件

src/main/resources/hibernate.cfg.xml

数据库参数

  • [hostname] : 主机名(本地为localhost
  • [port] : 端口号(默认端口号为5432
  • [database] : 数据库名
  • [username] : 用户名
  • [password] : 密码

配置文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- dialect configuration -->
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>

        <!-- database configuration -->
        <property name="hibernate.connection.url">jdbc:postgresql://[hostname]:[port]/[database]</property>
        <property name="hibernate.connection.username">[username]</property>
        <property name="hibernate.connection.password">[password]</property>

        <!-- hibernate configuration-->
        <property name="hibernate.show_sql">true</property>
        <!-- update: create if table not exist, else update-->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- List of XML mapping files -->
        <mapping resource="com/demo/postgresql/User.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

Hibernate映射文件

src/main/java/com/demo/postgresql/User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.demo.postgresql">
    <class name="com.demo.postgresql.User" table="public.user" catalog="web">
        <id name="id" column="id">
            <generator class="native" />
        </id>

        <property name="username" column="username" length="128"></property>
        <property name="password" column="password" length="128"></property>
    </class>
</hibernate-mapping>

Java代码

User类

src/main/java/com/demo/postgresql/User.java

package com.demo.postgresql;

public class User {
    
    
    private Integer id;
    private String username;
    private String password;

    public User() {
    
    

    }

    public User(String username, String password) {
    
    
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
    
    
        return id;
    }

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

    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    public String getPassword() {
    
    
        return password;
    }

    public void setPassword(String password) {
    
    
        this.password = password;
    }

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
};

Hibernate类

src/main/java/com/demo/postgresql/Hibernate.java

package com.demo.postgresql;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import java.util.ArrayList;
import java.util.List;

public class Hibernate {
    
    
    private Configuration configuration;
    private StandardServiceRegistry standardServiceRegistry;
    private SessionFactory sessionFactory;
    protected Session session;
    protected Transaction transaction;

    public Hibernate() {
    
    
        try {
    
    
            configuration = new Configuration();
            configuration.configure("hibernate.cfg.xml");
            standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
            sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    public void insertUser(List<User> userList) {
    
    
        for (User user : userList) {
    
    
            session.save(user);
        }
        transaction.commit();
    }

    public static void main(String[] args) {
    
    
        Hibernate hibernate = new Hibernate();
        List<User> userList = new ArrayList<User>();
        userList.add(new User("Tony", "123456"));
        userList.add(new User("Tom", "123456"));
        userList.add(new User("Tim", "123456"));
        userList.add(new User("Charles", "123456"));
        userList.add(new User("Bill", "123456"));

        hibernate.insertUser(userList);
    }
}

测试结果

select * from public.user;

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!

猜你喜欢

转载自blog.csdn.net/qq_44486439/article/details/113463566
今日推荐