Installation configuration hibernate + Tomcat

xiaoxiao2021-03-06  59

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 maxwait 5000 maxactive 4 Password test <

/ value> URL jdbc: mysql: // localhost: 3306 / story driverclassname DRIVERCLASSNAME com.mysql.jdbc.driver maxidle 2 Username < / Name> Test 3 Decompress the downloaded compressed package hibernate-2.1.2.zip, will decompress the hibernate2.jar to% WebApp% / Web-INF / LIB 4 will decompress the CGLIB-Full-2.0.2.jar under the LIB directory. Commons-collections-2.1.1.jar Commons-logging-1.0.4.jar Dom4j-1.4.jar ehcache-0.9.jar jta.jar log4j-1.2.8.jar ig odmg-3.0.jar files are also copied to% webApp% / Web-INF / LIB

5 Write Hibernate profile hibernate.cfg.xml placed in% WebApp% / Web-INF / CLASSES

Java: comp / enV / jdbc / mysqltest false net.sf.hibernate. Diagect.MysqldiaAlaforct

* 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 < Class name = "net.sf.hibernate.examples.quickstart.cat" Table = "cat"> 6.3 Save the following code as HibernateUtil .java, generate corresponding hibernateutil.class, put% WebApp% / Web-INF / CLASSES, also pay attention to package package net.sf.hiber Nate.examples.quickstart; import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory; import net.sf.hibernate. *; import net.sf.hibernate.cfg. *; public class Hibernateutil {

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. * "%> Lomboz JSP </ title> </ head> <body Bgcolor = "# ffffff"> <% // Add a CAT</p> <p>SESSION SES = Hibernateutil.currentSession (); Transaction TX = SES.BEGINTRANSACTION ();</p> <p>Cat princess = new cat (); princess.setname ("chenm"); princess.setsex ('f'); princess.setweight (7.4f); ses.save (prinities); tx.commit ();</p> <p>Hibernateutil.closesis ();</p> <p>// Read all CAT in the library</p> <p>SES = HibernateUtil.currentSession (); tx = ses.begintransaction ();</p> <p>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 ());</p> <p>TX.comMit (); hibernateutil.closesis ();%> </ body> </ html></p> <p>6.5 Test with HTTP, http: // localhost: 8090 / myservice / testcat.jsp test results, see: female cat: chenm</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-111842.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="111842" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.046</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'URdSK1bf8442Fk_2B1bn8zuBM3xhZLwr_2BBeXrs9t28YBZq_2BmjEDig4XMSp_2BRZZQqLOQtVObwebAD90T4_2FRHrkYjQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>