HiBernate单表映射

ORM (Object/Relationship Mapping):对象/关系映射

Hibernate是Java领域的一款开源的ORM框架技术

Hibernate对JDBC进行了非常轻量级的对象封装

编写第一个Hibernate例子

*使用junit进行测试 常用的三个注解标签

@Test:测试方法

@Before:初始化方法

@After:释放资源   执行顺序:@Before---->@Test------>@After

Hibernate 5之后,不用 ‘创建服务注册对象’

        //创建配置对象(读取配置文档))
        Configuration config = new Configuration().configure();
        //创建会话工厂对象
        sessionFactory = config.buildSessionFactory();
        //会话对象
        session = sessionFactory.openSession();
        //开启事务
        transaction = session.beginTransaction();

//测试类
public class StudentTest {
	
	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	//初始化方法
	@Before
	public void init() {
		//创建配置对象(读取配置文档))
		Configuration config = new Configuration().configure();
		//创建会话工厂对象
		sessionFactory = config.buildSessionFactory();
		//会话对象
		session = sessionFactory.openSession();
		//开启事务
		transaction = session.beginTransaction();
	}
	
	//释放资源
	@After
	public void destory() {
		transaction.commit();    //提交事务
		session.close();         //关闭会话
		sessionFactory.close();  //关闭会话工厂
	}
	
	//测试方法
	@Test
	public void testSaveStudents() {
		Students s = new Students(1, "艾斯", "男", new Date(), "东海");
		session.save(s); //保存对象进入数据库
		
	}
	
	
}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
	<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
	
	<property name="show_sql">true</property>
	<property name="format_sql">true</property>
	<property name="hbm2ddl.auto">create</property>
	<mapping  resource="Students.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Hibernate.cfg.xml常用配置

https://blog.csdn.net/ka_ka314/article/details/78996783

把对象保存到关系数据库中需要调用session的各种方法,save()、update()、delete()、createQuery()。

hbm配置文件常用设置(hibernate-mapping)

https://blog.csdn.net/aiguo94/article/details/75570894

https://blog.csdn.net/xiu2016/article/details/52805761

 单一主键

assigned :  由java应用程序负责生成(手工赋值)

native :由底层数据库自动生成标识符,如果是MySQL就是increment

基本类型

对象类型

单表CRUD操作实例save update delete get/load(单个记录)

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值