关于Hibernate+Mysql8.0的一些注意事项

<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!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.cj.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myy?useSSL=false&amp;serverTimezone=UTC</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password">root</property>
	
	<!-- 设置方言 -->
	<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
	
	
	<!-- 注册当前session上下文:保证同一线程中获得的session是同一个session 
	<property name="hibernate.current_session_context_class">thread</property>
	-->
	
	<!-- 自动建表 -->
	<property name="hibernate.hbm2ddl.auto">update</property>
	
	<!-- 显示SQL -->
	<property name="hibernate.show_sql">true</property>
	
	<!-- 格式化SQL -->
	<property name="hibernate.format_sql">true</property>

		
		<!--指定关联的 。hbm。xml文件 -->
		<mapping resource="com/pyq/hibernate/helloworld/News.hbm.xml"></mapping>
	</session-factory>
</hibernate-configuration>

hibernate.cfg.xml配置在src目录下注意驱动,方言和url一定要按照上面的来写,这里就不说明原因了,已经与百度密切合作:

www.baidu.com

package com.pyq.hibernate.helloworld;

import java.sql.Date;

public class News {
	
	private Integer id;
	private String title;
	private String author;
	
	private Date date;

	public News() {
		super();
		// TODO Auto-generated constructor stub
	}

	public News(Integer id, String title, String author, Date date) {
		this.id = id;
		this.title = title;
		this.author = author;
		this.date = date;
	}
	public News( String title, String author, Date date) {
		this.title = title;
		this.author = author;
		this.date = date;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

}

News类

<mapping resource="com/pyq/hibernate/helloworld/News.hbm.xml"></mapping>在hibernate.cfg.xml关联

 

<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/dtd/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>

    <class name="com.pyq.hibernate.helloworld.News" table="News">
        <id name="id" type="java.lang.Integer">
        	<column name="ID"></column>
            <generator class="native"/>
        </id>
        
        <property name="title"  type="java.lang.String" >
        	<column name="TITLE"></column>
        </property>
        
        <property name="author"  type="java.lang.String" >
        	<column name="AUTHOR"></column>
        </property>
        
        <property name="date"  type="java.sql.Date" >
        	<column name="DATE"></column>
        </property>

    </class>
    
</hibernate-mapping>

News中的属性与数据库字段对应,我这里启动的是自增的ID :<generator class="native"/>

注意你的mysql8.0中ID需要设置为主键且自增,推荐大家使用Navicat Premium来开发

package com.pyq.hibernate.helloworld;


import java.sql.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class Test {
	
	
	public static void main(String[] args){
		//创建一个会话工厂对象SessionFactory
		SessionFactory sessionFactory = null;
		//创建Con对象:对应hibernate的基本配置信息和对象关系映射
		Configuration config = new Configuration().configure();
		//4.0之前这样创建
		//sessionFactory = config.buildSessionFactory();
		//4.x创建一个SessionFactoryRegistry对象hibernate的任何配置和服务都需要在该对象中注册后才有用
		StandardServiceRegistry standardRegistry = new 
                StandardServiceRegistryBuilder().configure().build();
		sessionFactory  = config.buildSessionFactory(standardRegistry);
		
		//创建并打开会话
		Session session =sessionFactory.openSession();
		//开启事务
		org.hibernate.Transaction transaction = session.beginTransaction();
		//执行保存操作
		News news = new News("Java", "pyq", new Date(new java.util.Date().getTime()));
		session.save(news);
		//提交事务
		transaction.commit();
		//关闭session
		session.close();
		//关闭sessionfactory
		sessionFactory.close();
	}

}

最后是测试类,我这里给我的News类重载了一个构造方法,即没有放入ID,因为我选择的是ID自增长,还有一点在hibernate.cfg.xml中<property name="hibernate.hbm2ddl.auto">update</property>我也自动建立了表,你不用去数据库建News表,他会自动生成。最后启动工程hibernate.cfg.xml中<property name="hibernate.show_sql">true</property>会在控制台打印建表和插入sql

 

这就是后台打印的sql语句,工程目录如下:

记得导入相应的jar包(官网下载Hibrenate即可,还需要一个数据库连接的)

Finish,入门操作完成.................

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值