About server Tomcat and database mysql installation configuration See IPAP "In Windows 2000 Server installation Tomcat mysql" built on Tomcat MyService 0.Set% WebApp% = $ Tomcat_Home $ / WebApps / MyService1 in $ / WebApps / MyService1 Tomcat_Home $ / WebApps Create a Directory Structure MyService ----- Web-INF ----- LIB ---- Class ---- Net ----- sf --- --Hibernate ----- Examples ----- QuickStart2 Configure independent context
/ value> parameter>
5 Write Hibernate profile hibernate.cfg.xml placed in% WebApp% / Web-INF / CLASSES XML Version = '1.0' encoding = 'uTF-8'?>
session-factory>
hibernate-configuration> * Connection.DataSource's name must be identical to the name of the DBCP specified by the server.xml in the [Config Tomcat DBCP for MySQL] configuration. 6 Write the first test code 6.1 Save the following code as Cat.java, and generate corresponding Cat.class, put% WebApp% / Web-INF / CLASSES, where you use Cat.class, but final Cat.class should be in% WebApp% / Web-INF / CLASSES / NET / SF / Hibernate / Examples / Net / SF / Hibernate / Examples / QuickStart directory Package Net.Sf.Hibernate.examples.quickStart; Public Class Cat {
Private string id; private city name; private char sex; private float weight;
Public cat () {}
Public string getId () {return id;}
Private void setid (string id) {this.id = id;}
Public string getname () {return name;}
Public void setname (String name) {this.name = name;}
Public char getsex () {return sex;}
Public void setsex (char sex) {this.sex = sex;}
Public float getWeight () {return weight;}
Public void setWeight (float weight) {this.weight = weight;}} 6.2 Save the following code as o / R mapping file Cat.hbm.xml, put% WebApp% / Web-INF / CLASSES
Private static log log = logfactory.getlog (HibernateUtil.class);
PRIVATE STATIC FINAL SESSIONFACTORY SESSIONFACTORY;
static {try {// Create the SessionFactory sessionFactory = new Configuration () configure () buildSessionFactory ();..} catch (Throwable ex) {log.error ( ". Initial SessionFactory creation failed", ex); throw new ExceptionInInitializerError ( EX);}} public static final threadlocal session = new threadlocal ();
Public static session currentations () throws hibernateException {session s = (session) session.get (); // Open a new session, if this thread has none yet if (s == null) {s = sessionFactory.openSession (); Session.set (s);} returnz
Public static void closesession () THROWS HibernateException {session s = (session) session.get (); session.set (null); if (s! = null) s.close ();}}
6.4 Save the following code as TestCat.jsp, put% WebApp% <% @ page language = "java" PageEncoding = "GB2312"%> <% @ page import = "net.sf.hibernate.transaction"%> <% @ Page Import = "Net.sf.hibernate.Session"%> <% @ page import = "Net.sf.hibernate.cfg. *"%> <% @ page import = "net.sf.hibernate.query"% > <% @ Page import = "Net.sf.hibernate.examples.quickstart.hibernateutil"%> <% @ page import = "Net.sf.hibernate.examples.quickstart.cat"%> <% @ page import = " Java.util. * "%>
SESSION SES = Hibernateutil.currentSession (); Transaction TX = SES.BEGINTRANSACTION ();
Cat princess = new cat (); princess.setname ("chenm"); princess.setsex ('f'); princess.setweight (7.4f); ses.save (prinities); tx.commit ();
Hibernateutil.closesis ();
// Read all CAT in the library
SES = HibernateUtil.currentSession (); tx = ses.begintransaction ();
Query Query = SES.CREATEQUERY ("SELECT C from cat as c where c.sex =: sex"); query.setcharacter ("sex", 'f'); for (Iterator it = query.Iterage (); IT. HasNext ();) {cat cat = (cat) it.next (); out.println ("female cat:" cat.getname ());
TX.comMit (); hibernateutil.closesis ();%> body> html>
6.5 Test with HTTP, http: // localhost: 8090 / myservice / testcat.jsp test results, see: female cat: chenm