Hibernate入门

一、前往Hibernate官网下载Hibernate的相关包

        Hibernate下载地址

二、新建项目

1、编写Hibernate的配置文件 ---------   hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<!-- 指定连接数据库所需要的驱动 -->
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<!-- 指定连接数据库的url -->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8</property>
		<!--连接数据库是用户名-->
		<property name="hibernate.connection.username">root</property>
		<!--连接数据库是密码-->
		<property name="hibernate.connection.password">123456</property>
		<!--数据库连接池的大小-->
		<property name="hibernate.connection.pool.size">20</property>
		<!--
			是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,
			程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。
			项目部署后可以设置为false,提高运行效率
		-->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!--hibernate.dialect 只是Hibernate使用的数据库方言,
        	就是要用Hibernate连接那种类型的数据库服务器。-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property>
        <!-- 根据需要自动创建数据表 -->
        <property name="hbm2ddl.auto">update</property>
        
        <!-- 将sql脚本格式化后再输出 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 罗列所有持久化的类名 -->
        <mapping class="com.chen.pojo.Student"/>
	</session-factory>
</hibernate-configuration>
2、编写持久化类

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class Student {
	//学生学号
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	//学生姓名
	private String name;
	//学生性别
	private String sex;
	//学生居住地址
	private String address;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}
3、编写测试类

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

import com.chen.pojo.Student;

public class StudentTest {
	public static void main(String[] args) {
		//实例化Configuration
		//不带参数的configuration()方法默认加载hibernate.cfg.xml文件
		//若传入abc.xml作为参数,则不在加载hibernate.cgf.xml文件,改为加载abc.xml文件
		System.out.println("1");
		Configuration conf = new Configuration();
		conf.configure();
		StandardServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().configure().build();

		//以Configuration对象创建SessionFactory实例
		SessionFactory sf=new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
		//创建Session
		Session sess = sf.openSession();
		//开始事务
		Transaction tx = sess.beginTransaction();
		//创建消息对象
		Student s = new Student();
		s.setName("小玲");
		s.setSex("女");
		s.setAddress("江西赣州");
		//保存消息
		sess.save(s);
		//提交事务
		tx.commit();
		//关闭Session
		sess.close();
		sf.close();
	}
}
下面是控制台输出信息:

四月 06, 2017 8:14:45 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.6.Final}
四月 06, 2017 8:14:45 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
四月 06, 2017 8:14:45 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
四月 06, 2017 8:14:45 下午 org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
四月 06, 2017 8:14:46 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8]
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****, pool.size=20}
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
四月 06, 2017 8:14:46 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
四月 06, 2017 8:14:46 下午 org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
四月 06, 2017 8:14:46 下午 org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@3c9c0d96] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Hibernate: 
    
    create table student (
       id integer not null auto_increment,
        address varchar(255),
        name varchar(255),
        sex varchar(255),
        primary key (id)
    )
Hibernate: 
    insert 
    into
        student
        (address, name, sex) 
    values
        (?, ?, ?)
四月 06, 2017 8:14:47 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8]

数据库的截图:



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值