Hiernate框架学习(一)

Hibernate的简单的使用

(1)核心的配置文件(用来配置数据库的密码,显示sql语句,不同的数据库的方言,映射文件的位置等 hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/westos?serverTimezone=GMT%2B8&amp;useSSL=false&amp;useServerPrepStmts=true&amp;cachePrepStmts=true&amp;rewriteBatchedStatements=true&amp;useCursorFetch=true&amp;defaultFetchSize=100&amp;allowPublicKeyRetrieval=true</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123</property>
        <!--用来显示sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!--格式化sql语句-->
        <property name="hibernate.format_sql">true</property>
        <!--Hibernate方言的配置 不同的数据库之间的sql语句是不同的,可以帮你
         生成不同数据库的sql语句:
         下面代表的是使用MySQL的方言的配置,这个配置是必须的
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--路径是用斜杠表示-->
        <mapping resource="config/Account.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
(2)映射文件的配置(文件名:**.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>
    <!--实体类映射-->
    <class name="com.hibernate.pojo.Person" table="account">
      <!--用来与表中的主键进行对应-->
       <id name="id" column="id">
           <generator class="native"></generator>
       </id>
        <property name="name" column="name"></property>
        <property name="money" column="money"></property>
    </class>
</hibernate-mapping>
(3)测试类的编写
import com.hibernate.pojo.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

/**
 * @author kiosk
 * 测试类
 */
public class TestHibernate {
    @Test
    public void test() {
        //1)加载Hibernate的核心的配置文件(使用什么数据库?什么方言?等)
        Configuration configure = new Configuration().configure();
        //2)创建一个SessionFactory对象:类似于JDBC中的连接池,维护连接池或者是缓存
        SessionFactory factory = configure.buildSessionFactory();
        //3)有SessionFactory来创建一个Session的对象,类似于JDBC中的Connection的对象
        Session session = factory.openSession();
        //4)手动的开启事务
        Transaction transaction = session.beginTransaction();
        //5)编写代码
        Person person = new Person();
        person.setId(13);
        person.setMoney(1000d);
        person.setName("sunmoumou123456");
        session.save(person);
        //6)事务提交
        transaction.commit();
        //7)资源释放
        session.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40843624/article/details/88083252
今日推荐