Prerequisites: 1. Development tool Eclipse installed plugin LOMBOZ, and specifies that Server is JBoss2, has established a DataSource connection with MySQL in JBoss, JNDI is Java: / MySQLDS3, which has been established in MySQL CATColumn | Type | Modifier -------- ---------------------- ---------- CAT_ID | Character (32) | NOT NULLNAME | CHARACTER VARYING (16) | NOT NULLSEX | Character (1) | Weight | Real | Indexes: CAT_PKEY PRIMARY Key Btree (CAT_ID) Integration Steps: 1, Generate a web application 2, WEB-INF / LIB should include the following Hibernate must package hibernate2.jarodmg-3.0.jarcommons-collections-2.0.4.jarcommons-logging-1.0.4.jardom4j-1.4.jarehcache-0.9.jarcglib-ful-2.0.2.jar3, in this Application of WEB-INF / CLASSES to generate hibernate.cfg.xml (you can also use hibernate.properties, not discussed), the content is as follows: XML Version = '1.0' encoding = 'UTF-8'? !>
Hibernate / sessionFactory provides you need to bind the JNDI name of the sessionFactory (Binding, you can use Context Lookup in the container). Java: / mysqlds is a named MySQL DataSource name that has been bound. Hibernate.Dialev Net.sf.hibernate.Dialect.mysqldiaLect is hibernate to indicate database dialects for MySQL. Cat.hbm.xml is the Hibernate mapping file, which will be mentioned below. 4. Define a persistence class (POJO) - Cat.javapublic class cat {private string id; private string name; private carat weight; public cat () {} public string getId () {returnid;} Public 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;} {this.weight = weight;}} 5, define class mapping files - cat .hbm.xml xml version = "1.0"?>
clas>
hibernate-maping> This XML file is placed in the same directory as Hibernate.cfg.xml (I don't know if you can't put it in other places. 6, define a startup servlet -inithibernate.java, start Hibernate in the servlet in init (), and bind JNDIPUBLIC CLASS INIBERNATE EXTENDS HTTPSERVLET {
public void init (ServletConfig config) throws ServletException {super.init (config); try {SessionFactory sf = new Configuration () configure () buildSessionFactory ();.. System.out.println ( "Hibernate Startup completed successfully");} Catch (Exception EX) {EX.PrintStackTrace ();} // Todo method stub generated by Lomboz}
public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException {// TODO Method stub generated by Lomboz}} In order to make the Servlet can be executed when the server starts, configure WEB.XML file:
8. Test Hibernate define a servlet, which is as follows: context ctx = new initialContext (); sf = (sessionFactory) CTX.lookup ("Hibernate / sessionFactory); session session = sf.opensession (); Transaction tx = session.begintransaction (); CAT princess = new cat (); princess.setname ("princess"); Princess.SetSex ('f'); Princess.SetWeight (7.4f); session.save (Princess); TX.comMit (); session.close (); if there is no abnormality, there should already have a data in the database! An example of JSP to do inquiry: <% @ page import = "javax.naming. *"%> <% @ Page import = "Net .sf.hibernate. * "%> <% @ Page Import =" java.util. * "%> <% @ page import =" com.catmiw.hibernate.study.po. * "%> <% try {context ctx = new InitialContext (); // initialize to give context SessionFactory sf = (SessionFactory) ctx.lookup ( "hibernate / SessionFactory"); Session sess = sf.openSession (); String temp; System.out.println ( "Success connect Hibernate! "); Query query = sess.createQuery (" Select Cat from Cat "); Query.SetCharacter (" SEX ", 'F'); for (item it = query.Iterate (); it.hasnext (); {cat CAT = (cat) it.next (); temp = cat.getname ();%>