Hibernate学习笔记:IDEA下Maven工程使用Hibernate示例

开发步骤

1、使用IDEA创建Maven工程;
2、添加MySQL和Hibernate依赖,并通过IDEA生成Hibernate配置文件hibernate.cfg.xml;
3、通过IDEA生成持久化类和对象-关系映射文件*.hbm.xml;
4、通过Hibernate API编写访问MySQL数据库的代码。

创建Maven工程

创建步骤如图所示:


14305916-6c643bc4a8ae3149.png
选择Create New Project.png
14305916-5fab2f1bb735fe98.png
选择Maven.png
14305916-b081b9d4faa53e36.png
填写坐标.png
14305916-a6690b3dc291283f.png
单击Finish.png
14305916-c55159a409f4062b.png
Maven工程创建完成.png

添加依赖,并生成hibernate.cfg.xml配置文件

在pom.xml文件中添加Hibernate、MySQL、Junit三个依赖:

    <dependencies>
        <!-- Hibernate依赖 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.1.Final</version>
        </dependency>

        <!-- MySQL数据库依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.15</version>
        </dependency>

        <!-- Junit依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

通过IDEA生成Hibernate.cfg.xml配置文件步骤如图:

14305916-ef0727a81c22a44b.png
选择Project Structure....png
14305916-62b436b26896b4b6.png
选择Facets、Hibernate.png
14305916-b02bf6226cd4d9b7.png
选择HibernateProject.png
14305916-4624ae596db2edfc.png
选择配置文件.png
14305916-1dce0493c141b3cf.png
选择配置文件路径在resources下.png
14305916-cea67c937c352f42.png
配置文件完成.png

此时要在hibernate.cfg.xml配置文件中写入配置信息:

14305916-00eab7483fa1aa78.png
写入配置信息.png

代码如下,其中connection.username是MySQL的登录账户名,connection.password是密码,我这里账户名是root,密码是123。

<session-factory>
    <!-- MySQL登录账户名 -->
    <property name="connection.username">root</property>
    <!-- 账户密码 -->
    <property name="connection.password">123</property>
    <!-- 一些属性信息 -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306</property>
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- 指定在程序运行时在控制台输出SQL语句 -->
    <property name="show_sql">true</property>
    <!-- 输出的SQL语句格式化 -->
    <property name="format_sql">true</property>
    <!-- 运行时在数据库自动生成数据表,这里选择update -->
    <property name="hbm2ddl.auto">update</property>
  </session-factory>

使用IDEA生成持久化类及*.hbm.xml文件

在MySQL数据库中建立students数据库并新建student表,如图:

14305916-be237aa8cc0a8668.png
在MySQL中建数据库、建表.png

将id列的自增勾选上:

14305916-d5e42638d337226a.png
勾选id的自增.png

在IDEA中生成持久化类、*.hbm.xml文件:

14305916-78f14cac3b57fbc8.png
IDEA连接数据库.png
14305916-81c2a1129462a2e9.png
填写用户名和密码测试连接成功后确定.png
14305916-ea291148823c5f74.png
选择Persistence.png
14305916-1016827d0bccc249.png
选择By Database Schema.png

进入Import Database Schema界面,Choose Data Source项选择@localhost后可以看到MySQL中的数据库以及其中的表信息,勾选student、age、id、lastName。Package是存放持久化类StudentEntity和映射文件StudentEntity.hbm.xml的包,需要提前建好,我这里在java下建的包名是test。勾选Add to Session Factory和Generate Separ...后单击OK即可生成持久化类和映射文件。

14305916-0e74a9760d1a8052.png
生成映射类及hbm文件.png
14305916-665ef962a9fa959b.png
生成的持久化类和映射文件.png

此时需要:

  • 将StudentEntity.hbm.xml文件移至resources文件夹下,否则配置文件hibernate.cfg.xml会找不到它,运行时会报错;
  • 更改StudentEntity.hbm.xml文件中id的generator,填写class为native。
14305916-af3f4cbcf8131b2a.png
修改一些东西.png

再回到hibernate.cfg.xml文件中可以看到IDEA已经帮我们填写好了映射文件的地址:


14305916-96e782a95d03f0bd.png
IDEA填写映射文件地址.png

通过Hibernate API编写访问数据库代码

新建HibernateTest类进行测试:

public class HibernateTest {
    @Test
    public void Test(){
        //SessionFactory是生成Session的工厂
        SessionFactory sessionFactory=null;
        //Configuration类负责管理Hibernate的配置信息
        Configuration configuration=new Configuration().configure();
        //Hibernate4之后新增ServiceRegistry接口,所有基于Hibernate 的配置都必须统一向这个ServiceRegistry注册后才能生效
        ServiceRegistry serviceRegistry=new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .buildServiceRegistry();
        //生成SessionFactory类
        sessionFactory=configuration.buildSessionFactory(serviceRegistry);
        //生成Session类
        Session session=sessionFactory.openSession();
        //事务
        Transaction transaction=session.beginTransaction();
        //新建一个StudentEntity实例,将它插入数据库
        StudentEntity studentEntity=new StudentEntity();
        studentEntity.setId(1);
        studentEntity.setAge(20);
        studentEntity.setLastName("乔峰");
        //Session的save操作将这个实例从临时状态变为持久化状态
        session.save(studentEntity);
        //提交事务
        transaction.commit();
        //关闭操作
        session.close();
        sessionFactory.close();
    }
}

运行结果后如图,输出了一条SQL的插入语句。

14305916-d7d1e5f24b227079.png
运行成功.png

查看数据库,已经将刚才的实例成功插入了数据库。

14305916-c006379918d75e65.png
数据库插入成功.png

猜你喜欢

转载自blog.csdn.net/weixin_34092455/article/details/87638977
今日推荐