Hibernate4学习(一)入门

阅读更多

    笔者的第一个hibernate框架应用入门例子 hibernate版本号是 4.3.11

  1. hibernate核心配置文件hibernate.cfg.xml, 笔者将配置文件放在conf.hibernate包下,配置内容如下
    
    
    
    
    
        
    
            
            com.mysql.jdbc.Driver
            
            	jdbc:mysql://127.0.0.1:3306/lian?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false
            
            root
            root
    		
            
            5
    		
    		
            
            org.hibernate.dialect.MySQL5InnoDBDialect
    
            
            thread
    
            
            org.hibernate.cache.internal.NoCacheProvider
    
            
            true
    
            
            update
    		
            
    
        
    
    
     
  2. 实体类映射文件Music.hbm.xml,该配置文件放在conf.hibernate.domain路径下
    
    
    
    
     	
     		
     			
                
            
            
           
            
            
            
            
            
            
            
        
    
     
  3. 实体类org.lian.domain.Music.java
    public class Music {
    	private String id;
    	/**
    	 * 名称
    	 */
    	private String name;
    	/**
    	 * 歌手
    	 */
    	private String singer;
    	/**
    	 * 专辑
    	 */
    	private String album;
    	/**
    	 * 歌曲时长
    	 */
    	private String duration;
    	//作词
    	private String author;
    	//作曲
    	private String composer;
    	//风格
    	private String style;
    
     
  4. hibernateUtil.java 根据配置文件Hibernate创建SessionFactory
    public class HibernateUtil {
    	private static final SessionFactory sessionFactory = buildSessionFactory();
    
    	private static SessionFactory buildSessionFactory() {
    		try {
    			// Create the SessionFactory from hibernate.cfg.xml
    			// return new
    			// Configuration().configure("/conf/hibernate/hibernate.cfg.xml")
    			// .buildSessionFactory(new StandardServiceRegistryBuilder().build());
    			Configuration configuration = new Configuration();
    			configuration.configure("/conf/hibernate/hibernate.cfg.xml");
    			ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
    					.applySettings(configuration.getProperties()).build();
    			SessionFactory sessionFactory = configuration
    					.buildSessionFactory(serviceRegistry);
    			return sessionFactory;
    
    		} catch (Throwable ex) {
    			// Make sure you log the exception, as it might be swallowed
    			System.err.println("Initial SessionFactory creation failed." + ex);
    			throw new ExceptionInInitializerError(ex);
    		}
    	}
    
    	public static SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    
    }
     
  5. 测试类Main.java
    public class Main {
    
    	public static void main(String[] args) {
    		// 获取session连接
    		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    		session.beginTransaction();
    		Music music = new Music();
    		music.setName("红色高跟鞋");
    		music.setAuthor("蔡健雅");
    		music.setSinger("蔡健雅");
    		music.setComposer("蔡健雅");
    		music.setAlbum("若你碰到他");
    		music.setDuration("03:26");
    		session.save(music);
    		session.getTransaction().commit();
    		// 关闭session工厂
    		HibernateUtil.getSessionFactory().close();
    	}
    }
     

你可能感兴趣的:(hibernate4)