01 搭建hibernate工程

2020-05-01  本文已影响0人  张力的程序园

hibernate是较为常用的ORM框架,本专辑将介绍hibernate的使用。这一节将展示如何搭建一个hibernate工程。

1、软件约束

2、前提约束

3、操作步骤

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.6.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="connection.url">jdbc:mysql://127.0.0.1:3306/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">zhangli</property>

        <!-- 方言 -->
        <property name="dialect">org.hibernate.dialect.MySQL57Dialect</property>
        <!-- 全局 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>

        <mapping resource="net/wanho/user.hbm.xml"/>

    </session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>

    <class name="net.wanho.User" table="t_user">
        <id name="id" column="id" type="java.lang.Integer">
            <generator class="native"></generator>
        </id>
        <property name="name" column="name" type="java.lang.String" />
    </class>
</hibernate-mapping>
package net.wanho;

import java.io.Serializable;

public class User implements Serializable {
    private int id;
    private String name;

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
package net.wanho;


import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;

import java.util.List;

public class HibernateMainTest {
    public static void main(String[] args) {
        Configuration config = new Configuration();
        config.configure("hibernate.cfg.xml");
        SessionFactory factory = config.buildSessionFactory();

        Session session = factory.openSession();
        Transaction tran = session.beginTransaction();
        User user0 = new User();
        user0.setName("ali");
        session.save(user0);
        tran.commit();


        // 查询方式1
        User user = (User) session.get(User.class, 1);
        System.out.println(user);

        /**
         * 查询方式2
         */
        Query query = session.createQuery(" from User ");
        List ls = query.list();
        System.out.println(ls.get(0));

        /**
         * 查询方式3
         */
        User user1 = session.load(User.class,1);
        System.out.println(user1);

        /**
         * 查询方式4
         */
        SQLQuery sqlQuery = session.createSQLQuery("select * from t_user where id=1").addEntity(User.class);
        User user2 = (User) sqlQuery.uniqueResult();
        System.out.println(user2);

        factory.close();

    }
}

以上就是hibernate的搭建和入门程序。

上一篇下一篇

猜你喜欢

热点阅读