hibernate注解demo

  1. 搭建hibernate环境

  2. 新建一个Demo.java文件,对应数据库的表

     

  3. package com.web.hibernate.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="demo")
    public class Demo {
    	
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	@Column(name="d_id",nullable=false,unique=true)
    	private int id;
    	@Column(name="d_name")
    	private String name;
    	
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}	
    }


  4. 在hibernate的配置的配置文件中,配置属性

  5. 和传统的xml文件有点区别的地方就是:引入指定的class路径;

<!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.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">demo</property>
        <property name="hibernate.connection.password">demo</property>
        <property name="hibernate.connection.url">
        	<![CDATA[
        		jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
        	]]>
        </property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
    
    	<!-- xml配置方式:指定映射文件的路径 -->
    	<!-- <mapping resource="com/imooc/entity/Project.hbm.xml"/> 
    	<mapping resource="com/imooc/entity/Employee.hbm.xml"/>  -->
    	
    	<!-- 注解生效方式:指定类文件的路径 -->
    	<mapping class="com.web.hibernate.entity.Demo" />
    </session-factory>
</hibernate-configuration>

5.写一个HibernateUtil.java的类;

package com.web.hibernate.uitl;

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 HibernateUitls {
	
	private static SessionFactory sessionFactory;
	private static Session session;
	
	static {
		// 创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化
		Configuration config = new Configuration().configure();
		StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
				.applySettings(config.getProperties());
		StandardServiceRegistry ssr=ssrb.build();
		sessionFactory=config.buildSessionFactory(ssr);
	}
	
	//获取SessionFactory
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	//获取Session
	public static Session getSession(){
		session=sessionFactory.openSession();
		return session;
	}
	
	//关闭Session
	public static void closeSession(Session session){
		if(session!=null){
			session.close();
		}
	}
}

6.写个测试类

package com.web.hibernate.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import com.web.hibernate.entity.Demo;
import com.web.hibernate.uitl.HibernateUitls;

public class TestDemo {
	public static void main(String[] args) {
		Session session= HibernateUitls.getSession();
		Transaction tr = session.beginTransaction();
		
		Demo demo = new Demo();
		demo.setName("幕女神");
		session.save(demo);
		tr.commit();
		session.close();
	}
}

基本实例完成,其中的各种参数,请自行查询;




你可能感兴趣的:(hibernate注解demo)