Hibernate introduction

zhaozj2021-02-16  145

Hibernate1. Copy the JDBC driver required to the database to the global class path (Tomcat / Common / LIB). 2. Gril the hibernate2.jar file to WEB-INF / LIB 3. Put the DOM4J (required), CGLIB (required) (required) odmg4 (required) 4. Configure the Tomcat connection pool, add a database JDBC connection pool in Tomcat / Conf / Server.xml, 5. Prepared javabeanpublic class Cat {private String id; private String name; public String getId () {return id;} public void setId (String id) {this.id = id;} public String getName () {return name;} public void SetName (String name) {this.name = name;}} 6. Configure Cat.hbm.xml7. Configure hibernate.cfg.xml, put it under WEB-INF / CLASSES 8. Writing Code 1) Modify Configuration Conf = New Configuration (). Configure (); sessionFactory = Conf .buildsessionFactory (); session s = sessionFactory.openSession (); transaction tx = s.begintransaction (); onlinenews on = new onlinenews (); on.setNewsData ("Tests"); on.SetURL ("http:"); S.save (on); s.flush (); tx.commit (); s.Close ();

2) Query transaction tx = session.begintransaction (); query query = session.createQuery ("SELECT CAT from cat as cat where cat.sex =: sex"); Query.SetCharacter ("SEX", 'F'); for (Iterator IT = query.Itemate (); it.hasnext ();) {cat cat = (cat) it.next (); out.println ("female cat:" cat.getname ());} TX. Commit ();

转载请注明原文地址:https://www.9cbs.com/read-7921.html

New Post(0)