关于在IDEA中使用Hibernate框架,以及出现的错误总结

在IDEA中初次创建是没有Hibernate框架的,所以需要引入Hibernate插件,
在setting–>plugins中搜索此插件
在这里插入图片描述
安装即可;
安装好插件以后具体的配置信息可以看这篇文章:
https://www.cnblogs.com/solverpeng/p/5915823.html

关于一些基本的Hibernate配置:

<?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="connection.url">jdbc:mysql://localhost:3306/hbn?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->


        <!-- 配置 Hibernate 的基本信息 -->
        <!-- 每个数据库对应的Dialet,用来匹配其平台特性 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- 指定自动生成数据表的策略 -->
        <property name="hbm2ddl.auto">create</property>

        <!-- 显示sql语句  这下边的是我类里边的,后边我会把这个工程发上去,读者可自行下载 -->
        <property name="hibernate.show_sql">true</property>
        <mapping resource="com/entity/Address.hbm.xml" />
        <mapping resource="com/entity/Cclass.hbm.xml" />
        <mapping resource="com/entity/Cstudent.hbm.xml" />
        <mapping resource="com/entity/Emp.hbm.xml" />
        <mapping resource="com/entity/Student.hbm.xml" />
        <mapping resource="com/entity/TGroup.hbm.xml" />
        <mapping resource="com/entity/TRole.hbm.xml" />
    </session-factory>
</hibernate-configuration>

然后不想通过maven下载包的话可以自己下载好需要的数据库连接包放到你的lib目录下,一切完工以后项目的目录结构是这样的:

在这里插入图片描述
在这里插入图片描述
关于在这个过程中遇到的一些错误以及解决方案;
hibernate 报错: ```WARN: GenerationTarget encountered exception accepting command : Error executing
就是配置文件有点问题,看着这篇文章改;
https://blog.csdn.net/RANJIANGCHENG/article/details/77435203

The content of element type “session-factory” must match "(property*,mapping*,(class-cache| c

解决方案,配置文件的属性好好看看是不是丢失括号什么的,
https://blog.csdn.net/cq340321/article/details/58049100

Cannot resolve class or package ‘mysql’
https://blog.csdn.net/hadues/article/details/82354658
我的这个,解决方案,是直接把Tomcat工程引进去了就解决了这个问题。
在这里插入图片描述
在这里插入图片描述
引入以后,就可以解决了问题。
解决完以后就可以运行;
在这里插入图片描述
运行这个类以后就可以在数据库中自动生成表了
在这里插入图片描述
代码 访问这个链接获取:
https://download.csdn.net/download/delete_bug/53308493

猜你喜欢

转载自blog.csdn.net/delete_bug/article/details/121651565
今日推荐