分享web开发知识

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

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

Intellij Idea Hibernate 环境搭建

发布时间:2023-09-06 02:25责任编辑:顾先生关键词:Hibernate
  1. 新建hibernate项目
  2. 添加mysql-connector
    • ctrl+alt+shift+s lib add mysql-connector

  3. 创建实体类
    • src/entity/User.java
     ???package entity; ???public class User { ???????//hibernate 要求实体类有一个属性唯一 ???????private int uid; ???????private String username; ???????private String password; ???????private String address; ???????public int getUid() { ???????????return uid; ???????} ???????public void setUid(int uid) { ???????????this.uid = uid; ???????} ???????public String getUsername() { ???????????return username; ???????} ???????public void setUsername(String username) { ???????????this.username = username; ???????} ???????public String getPassword() { ???????????return password; ???????} ???????public void setPassword(String password) { ???????????this.password = password; ???????} ???????public String getAddress() { ???????????return address; ???????} ???????public void setAddress(String address) { ???????????this.address = address; ???????} ???}
  4. 创建src/rntity/User.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: 实体类的全路径 ???????????????- table: 数据库表名称 ???????????- id ???????????????- name: 实体类的id属性名称 ???????????????- column: 生成表的字段名称 ???????????- generator ???????????????- class ???????????????????- native: 生成表主键id自增长 ???????????- property ???????????????- name: 属性名 ???????????????- column: 字段名 ???????--> ???????<class name="entity.User" table="t_user"> ???????????<id name="uid"> ???????????????<generator class="native"/> ???????????</id> ???????????<property name="username"/> ???????????<property name="password"/> ???????????<property name="address"/> ???????</class> ???</hibernate-mapping>
  1. 配置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> ???????????<!--第一部分: 配置数据库信息--> ???????????<!--ctrl+alt+shift+s lib add mysql-connector--> ???????????<property name="connection.url"> ???????????????jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&amp;characterEncoding=utf- ???8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC ???????????</property> ???????????<property name="connection.driver_class">com.mysql.jdbc.Driver</property> ???????????<property name="connection.username">root</property> ???????????<property name="connection.password">passwd</property> ???????????<!--第二部分: 可选--> ???????????<!-- DB schema will be updated if needed --> ???????????<property name="hbm2ddl.auto">update</property> ???????????<!--输出sql语句, 并格式化--> ???????????<property name="show_sql">true</property> ???????????<property name="format_sql">true</property> ???????????<!--各个数据库的方言--> ???????????<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> ???????????<!--第三部分: 映射文件--> ???????????<mapping resource="entity/User.hbm.xml"/> ???????</session-factory> ???</hibernate-configuration>
  1. 修改Main.java
 ???import entity.User; ???import org.hibernate.*; ???import org.hibernate.cfg.Configuration; ???????????public class Main { ???????private static final SessionFactory ourSessionFactory; ???????????static { ???????????try { ???????????????//加载核心配置文件 ???????????????Configuration configuration = new Configuration(); ???????????????configuration.configure(); ???????????????????//创建SessionFactory对象, 并在数据库中把表创建出来 ???????????????????ourSessionFactory = configuration.buildSessionFactory(); ???????????} catch (Throwable ex) { ???????????????throw new ExceptionInInitializerError(ex); ???????????} ???????} ???????????public static Session getSession() throws HibernateException { ???????????//创建Session ???????????return ourSessionFactory.openSession(); ???????} ???????????public static void main(final String[] args) throws Exception { ???????????final Session session = getSession(); ???????????????try { ???????????????User user = new User(); ???????????????user.setUsername("小李"); ???????????????user.setPassword("123"); ???????????????user.setAddress("日本"); ???????????????session.save(user); ???????????????session.beginTransaction().commit(); ???????????} finally { ???????????????session.close(); ???????????} ???????} ???}
  1. 执行Main.java
  2. 注意事项
 ???<property name="connection.url"> ???????????????jdbc:mysql://127.0.0.1:3306/hibernate_test?useUnicode=true&amp;characterEncoding=utf-8&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC ???</property> ???<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> ???<mapping resource="entity/User.hbm.xml"/> 要放到最后

Intellij Idea Hibernate 环境搭建

原文地址:https://www.cnblogs.com/edhg/p/10090826.html

知识推荐

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