hibernate5.25简单配置

1.导入必要的包

在官网下载hibernate-release-5.2.5.Final,如果嫌麻烦就跟我一样直接将lib/required下的必要包全部导入,也不多。然后加上lib/spatial下slf4j-api-1.7.7.jar和slf4j-simple-1.7.7.jar。当然还需要加上数据库驱动包。


2.实体类,注解方式自动生成表

package com.example.hibernate.bean;

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

@Entity
@Table(name = "tb_student") //数据库自动成成表名
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	private String name;

	private Integer age;

	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 Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

}
3.hibernate.cfg.xml(放在src下就可以了)

<?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>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <!-- 数据库连接池的大小 -->
    <property name="connection.pool_size">5</property>
     <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢-->   
    <property name="jdbc.fetch_size">50 </property>   
    <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大-->   
    <property name="jdbc.batch_size">23 </property>  
    <!-- SQL 方言 -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- 在控制台输出sql语句 -->
    <property name="show_sql">true</property>
    <!-- 在启动时根据配置更新数据库 -->
    <property name="hbm2ddl.auto">update</property>
    <mapping class="com.example.hibernate.User"/><!-- 注册我们的实体映射类-->
    <mapping class="com.example.hibernate.bean.Student"/><!-- 注册我们的实体映射类-->
</session-factory>
</hibernate-configuration>
4.测试类
package com.example.hibernate;

import java.util.Date;

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 com.example.hibernate.bean.Student;
import com.example.hibernate.bean.User;

public class Client {
	public static void main(String[] args) {

		// 但在5.1.0版本汇总,hibernate则采用如下新方式获取:
		// 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final
		// 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件
		final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml")
				.build();
		// 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
		SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();

		/**** 上面是配置准备,下面开始我们的数据库操作 ******/
		Session session = sessionFactory.openSession();// 从会话工厂获取一个session 
		
		try {
			
			Transaction transaction = session.beginTransaction();// 开启一个新的事务
			User user = new User();
			user.setUsername("Nick");
			user.setPassword("123");
			user.setCreateTime(new Date());
			user.setExpireTime(new Date());
			session.save(user);
			
			Student student=new Student();
			student.setName("Lily");
			student.setAge(20);
			session.save(student);
			
			transaction.commit();// 提交事务
			
		} catch (Exception e) {
			e.printStackTrace();
			// 回滚事务
			session.getTransaction().rollback();
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					// 关闭session
					session.close();
				}
			}
		}
	}
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值