简单使用hibernate(idea中使用)

首先创建一个maven项目

创建成功后,进行创建数据库的表

CREATE TABLE BOOK(
ID INT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(20),
NUMBER int );
insert into BOOK(NAME,NUMBER) VALUES
('Java程序设计',10),
('数据结构',10),
('设计模式',10)

然后再刚刚创建的项目里创建一个类,该类名称、字段要与数据库的一样

必须注意使用的注释

package com.wbg.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    Long id;
    String name;
    int number;

    public Book() {
    }

    public Book(Long id, String name, int number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public Long getBook_id() {
        return id;
    }

    public void setBook_id(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
@Override
public String toString() {
    return "Book{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", number=" + number +
            '}';
}
}

 

进行在pom.xml文件中添加依赖

<dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.6.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client -->
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

在resources资源文件夹目录下面创建一个hibernate.xml配置文件

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--数据库驱动-->
        <property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property>
        <!--连接-->
        <property name="hibernate.connection.url">jdbc:mariadb://localhost:3306/stu</property>
        <!--用户名-->
        <property name="hibernate.connection.username">root</property>
        <!--密码-->
        <property name="hibernate.connection.password">123456</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MariaDB10Dialect</property>

        <property name="hibernate.show_sql">true</property>

        <mapping class="com.wbg.entity.Book"/>
    </session-factory>
</hibernate-configuration>

测试成功

package com.wbg;


import com.wbg.entity.Book;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class test {

    public static void main(String[] args) {

        Configuration configuration=new Configuration();
        Configuration configure=configuration.configure("hibernate.xml");
        SessionFactory sessionFactory=configuration.buildSessionFactory();
        Session session=sessionFactory.openSession();
       Book book= session.get(Book.class,2L);
        System.out.println(book.toString());

    }
}

 

posted @ 2018-10-13 11:06  韦邦杠  阅读(2578)  评论(0编辑  收藏  举报