分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

Hibernate环境搭建

发布时间:2023-09-06 01:57责任编辑:白小东关键词:Hibernate
  • 1.去官网下载Jar包:以hibernate-release-5.2.16.Final为例

解压后将hibernate-release-5.2.16.Final\lib\required目录中的所有jar包导入

  • 2.配置文件

将hibernate-release-5.2.16.Final\project\etc中hibernate.cfg.xml文件复制到src下

并且修改配置如下:

 

<!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><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?serverTimezone=GMT</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">zhangpn</property><property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="com/zhangpn/pojo/User.hbm.xml"/></session-factory></hibernate-configuration>
  • 3.为测试而创建一个pojo类
 1 package com.zhangpn.pojo; 2 ?3 public class User { 4 ????private int id; 5 ????private String name; 6 ????private String password; 7 ?8 ????public int getId() { 9 ????????return id;10 ????}11 12 ????public void setId(int id) {13 ????????this.id = id;14 ????}15 16 ????public String getName() {17 ????????return name;18 ????}19 20 ????public void setName(String name) {21 ????????this.name = name;22 ????}23 24 ????public String getPassword() {25 ????????return password;26 ????}27 28 ????public void setPassword(String password) {29 ????????this.password = password;30 ????}31 32 }
User类
  • 4.配置*.hbm.xml文件
<?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> ???<class name="com.zhangpn.pojo.User" table="user"> ???????<id name="id" column="id"> ???????????<generator class="native"/> ???????</id> ???????<property name="name"/> ???????<property name="password"/> ???</class></hibernate-mapping>
  • 5.将User.hbm.xml加入hibernate.cfg.xml文件中
<!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><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///hibernate</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">zhangpn</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><mapping resource="com/zhangpn/pojo/User.hbm.xml"/></session-factory></hibernate-configuration>
  • 6.测试
package com.zhangpn.test;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.Metadata;import org.hibernate.boot.MetadataSources;import org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl;import org.hibernate.boot.registry.StandardServiceRegistry;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import com.zhangpn.pojo.User;public class Test {public static void main(String[] args) {/** * hibernate 4 版本 *///Configuration cfg = new Configuration().configure();//ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();//SessionFactory sf = cfg.buildSessionFactory(registry);//Session session = sf.openSession();/** * hibernate 5 版本 */StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().configure().build();Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();Session session = sessionFactory.openSession();Transaction tx = session.beginTransaction();User user = new User();user.setName("zhangpn");user.setPassword("888888");session.save(user);tx.commit();session.close();}}
  • 测试结果

  • 问题与解决:

如果遇到错误:Error executing DDL via JDBC Statement

建议:修改方言为:<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

Hibernate环境搭建

原文地址:https://www.cnblogs.com/batj/p/9126348.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved