Hibernate+MySQL简单实例

项目结构图

Hibernate+MySQL简单实例_第1张图片


一、导入jar包(java project 导包—— build path—configure build path—add external JARs),如上图所示

二、在MySQL数据库中建立相应的数据库和数据表,然后再项目中建立他的实体类User.java

package com.entity;

public class User {
	private int id;
	private String name;
	private String password;
	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;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}

 

三、创建映射文件User.hbm.xml。完成数据表到实体类的映射




  
     
        
     
     
     
  



四、创建配置文件Hibernate.cfg.xml。配置数据库,并指向映射配置文


 
 
   
     com.mysql.jdbc.Driver
     jdbc:mysql://localhost:3306/user_information
     admin
     123456
     org.hibernate.dialect.MySQLDialect
     
   
 
 


五、新建工具类,读取配置文件,创建SessionFactory和Session

package com.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public final class HibernateUtil {
	private HibernateUtil(){		
	}
	private static SessionFactory sessionFactory;
	static{
		Configuration cfg = new Configuration();
		cfg.configure();
		ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
		sessionFactory = cfg.buildSessionFactory(sr);
	}
	public static SessionFactory getSeesionFactory(){
		return sessionFactory;
	}
	public static Session getSession(){
		return sessionFactory.openSession();
	}

}


六、编写dao接口和dao的实现类

package com.dao;

import com.entity.User;

public interface UserDao {
	public void save(User user);
//	public void delete(User user);
	public User findUserById(int id);
//	public void update(User user);

}


package com.dao.impl;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dao.UserDao;
import com.entity.User;
import com.util.HibernateUtil;

public class UserDaoImpl implements UserDao {
	
	Session session = HibernateUtil.getSession();

	@Override
	public void save(User user) {
		Transaction ts = session.beginTransaction();
		try {
			session.save(user);
			ts.commit();
		} finally {
			if(session != null)
				session.close();
		}
		
	}

	@Override
	public User findUserById(int id) {
		try {
			User user = (User)session.get(User.class, id);
			return user;
		} finally {
			if(session != null)
				session.close();
		}
		
	}

}


七、编写测试类

package com.test;

import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.entity.User;

public class Test {

	public static void main(String[] args) {
		User user = new User();
		UserDao dao = new UserDaoImpl();
		
//		user.setName("yl");
//		user.setPassword("0000");
//		dao.save(user);
		
		user= dao.findUserById(1);
		System.out.println("所查询ID的姓名是"+user.getName());

	}

}


运行结果

Hibernate+MySQL简单实例_第2张图片

jar包下载地址:http://download.csdn.net/detail/forrest_ou/9630608


感想:IT方面的知识变化的很快,例如Hibernate中的buildFactory()方法已经被弃掉了,采用了新的方法,见第五步,新建工具类中








你可能感兴趣的:(javaWeb)