使用hibernate

1.步骤

1.1 引入相应的jar包

1-1.png

1.2 创建hibernate配置文件

在这里插入图片描述
Snipaste_2019-10-25_16-19-02.png
如若最后一步始终不可以点击finish,可以选则低的版本,例如:5.1
注意:这里使用的是hibernate插件(Hibernate Tools//eclipse)
插件的安装可参考这个博文https://blog.csdn.net/w112736112736/article/details/78381070

1.3 创建持久化类

这个javabean需要满足的条件:

  • 有一个无参的构造器
  • 相应的getter和setter方法
  • 提供一个标识属性
  • 改类必须是非final

1.4 创建隐射文件

在这里插入图片描述

1.5 使用hibernate,具体可以参考后面的代码

2.代码

2.1创建hibernate配置文件

<?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:///hibernate1</property>
    	
    			<!-- c3p0  有关的 -->
    	<property name="hibernate.c3p0.max_size">10</property>
    	<property name="hibernate.c3p0.min_size">5</property>
    	<property name="hibernate.c3p0.acquire_increment">2</property>
    	<property name="hibernate.c3p0.max_statements">10</property>
    	
    	<property name="hibernate.c3p0.timeout">2000</property>
    	<property name="hibernate.c3p0.idle_test_period">2000</property>
    	
    	<!-- hibernate的配置-->
    	<property name="hibernate.jdbc.fetch_size">100</property>
    	<property name="hibernate.jdbc.batch_size">30</property>
    	
    	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    	<property name="show_sql">true</property>
    	<property name="format_sql">true</property>
    	
    	<property name="connection.isolation">2</property>
    	
    	<property name="hbm2ddl.auto">update</property>
    	
     <!--配置映射文件-->
      <mapping resource="com/mulin/hibernate/test/Person.hbm.xml"/>
     
     
    </session-factory>
</hibernate-configuration>

2.2创建持久化类

package com.mulin.hibernate.test;

public class Person {
	
	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;
	}
	
	@Override
	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
	

}

2.3hibernate映射文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2019-10-25 16:25:01 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping package="com.mulin.hibernate.test">
    <class name="Person" table="PERSONS">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <!--OID的生成方式-->
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="PEO_NAME" />
        </property>
        <property name="age" type="java.lang.Integer">
            <column name="PEO_AGE" />
        </property>
    </class>
</hibernate-mapping>

2.4 使用hibernate

package com.mulin.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HibernateTest {

	private  SessionFactory sessionFactory = null;
	private Session session = null;
	private Transaction transaction = null;
	
	@Before
	public void init() {
		//创建session工厂

		//加载hibernate配置文件
		Configuration configuration = new Configuration().configure();

		//hibernate需要的一个服务
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
											.applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		
		//开启session
		session = sessionFactory.openSession();

		//开启事务
		transaction= session.beginTransaction();
	}
	
	@After
	public void destory() {
		//提交事务
		transaction.commit();

		//关闭session
		session.close();

		//关闭session工厂
		sessionFactory.close();
	}
	
	@Test
	public void test() {
		
	}

}

这个junity中使用了before和after注解,它的意思就是当执行其他方法时,系统首先会调用before注解的那个方法,当方法执行完之后回调用after注解的那个方法,其实就是一个代理模式,代理模式底层原理我就不叙述了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值