`
gcgmh
  • 浏览: 349333 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate的hibernate.cfg.xml的基本配置

阅读更多
hibernate.cfg.xml的基本配置


<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_basemapping</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<mapping resource="com/hibernate/pojo/Article.hbm.xml"/>
		<mapping 
	</session-factory>
</hibernate-configuration>


=======================================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.pojo">
	<class name="Article" table="article">
		<id name="id">
			<generator class="native"/>
		</id>
		
		<property name="title" />
		<property name="keywords" />
		<property name="content" />
		<property name="description" />
		
	</class>
</hibernate-mapping>




=======================================
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	
	private static SessionFactory sessionFactory = null;
	private static Session session = null;
	
	static {
		try {
			sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
		} catch (Throwable e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
	
	public static Session getSession(){
		return session;
	}
	
	public static void closeSession(Session session){
		if(null != session){
			if (session.isOpen()) {
				session.close();
			}
		}
	}
	
	
}



=========================
private Class<T> clazz;
public BaseDaoImpl(Class<T> clazz){
    this.clazz = clazz;
}

public ArticleDaoImpl(){
  super(Article.class);
}

delete(int id){
Object o = getSession().get(clazz,id);
if(o!=null){
  getSession().delete(o);
}
}


find(){
getSession().createQuery("from" + clazz.getName));

find(int first,int max)
getSession().createQuery("FROM " + clazz.getName))
.setFirstResult(first)
.setMaxResult(max)
.list();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics