Hibernate Notes (a) CRUD CRUD

table of Contents:

  • Configuration and basics
  • The basic CRUD operations

Configuration

https://how2j.cn/k/hibernate/hibernate-tutorial/31.html

The final version of the Product Case Study

Step1: Create a database and table

CREATE TABLE product_ (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(30) ,
  price float ,
  PRIMARY KEY (id) ,
    ......
    
) DEFAULT CHARSET=UTF8;

Follow-up also adds other, not repeat them here

1580309531740

Step2: guide package

See links to the top

Step3: Creating an entity class

Member variables and database table column to one correspondence

public class Product {
    int id;
    String name;
    float price;
    Category category;
    Set<User> users;
    int version;

    public int getVersion() {
        return version;
    }
    public void setVersion(int version) {
        this.version = version;
    }
    public Set<User> getUsers() {
        return users;
    }
    public void setUsers(Set<User> users) {
        this.users = users;
    }
    public Category getCategory() {
        return category;
    }
    public void setCategory(Category category) {
        this.category = category;
    }
    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;
    }
    public float getPrice() {
        return price;
    }
    public void setPrice(float price) {
        this.price = price;
    }
}

Step4: xml configuration entity corresponding category

  • Product.hbm.xml P must be capitalized, and classes to be consistent
  • class label shows the relationship of the entity classes and table
  • id tag, <generator class="native">meaning the id from growth mode using a local database
  • version label, see (b) of the optimistic lock
  • property label indicates that the variable
  • set label is a variable, see (b) the relationship
<?xml version="1.0"?>
<!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.how2java.pojo">
    <class name="Product" table="product_">
        <id name="id" column="id">
            <generator class="native">
            </generator>
        </id>
        <version name="version" column="ver" type="int"/>
        <property name="name" />
        <property name="price" />
        <many-to-one name="category" class="Category" column="cid"/>
        <set name="users" table="user_product">
            <key column="pid"/>
            <many-to-many column="uid" class="User"/>
        </set>
    </class>

</hibernate-mapping>

Step5: Configure total xml

In the src directory, after each one more entity classes to add a mapping

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/how2java/pojo/Product.hbm.xml" />
        <mapping resource="com/how2java/pojo/Category.hbm.xml" />
        <mapping resource="com/how2java/pojo/User.hbm.xml" />
    </session-factory>

</hibernate-configuration>

principle:

Hibernate applications through a Product object into the database table product_
hibernate.cfg.xml configuration file provides links to a database of basic information about
account password database driver ip port
Product.hbm.xml provide an object mapping table and
which table corresponds ? What attributes correspond to what field

1580364049116

Specific operation

Create a Product object, and hibernate through this object, inserted into the database

Hibernate basic steps are:

  1. Get SessionFactory
  2. Fetching a Session SessionFactory
  3. Open a transaction on the basis of Session
  4. Session by calling the save method to save objects to the database
  5. Commit the transaction
  6. Close Session
  7. Close SessionFactory

Template below

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
import com.how2java.pojo.Product;
 
public class TestHibernate {
    public static void main(String[] args) {
 
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
 
        Session s = sf.openSession();
        s.beginTransaction();
 
        // TODO:事件操作
      
        s.getTransaction().commit();
        s.close();
        sf.close();
    }
 
}

Note: code after CRUD TODO is in part omitted before and after

By (insert)

        for (int i = 0; i < 10; i++) {
            Product p = new Product();
            p.setName("iphone"+i);
            p.setPrice(i);
            s.save(p);         
        }

Seek

        Product p =(Product) s.get(Product.class, 6);
        System.out.println("id=6的产品名称是: "+p.getName());

The second method is to get parameter id, it is a database key

delete

With the delete () method

        Product p =(Product) s.get(Product.class, 5);
        s.delete(p);

modify

By update () method

        System.out.println(p.getName());
        p.setName("iphone-modified");
        s.update(p);

Guess you like

Origin www.cnblogs.com/cpaulyz/p/12401594.html
Recommended