1、19 this.colorSelected = colorSelected;20 21 22 publicString getMessage() 23 returnMessage;24 25 26 publicvoidsetMessage(String message) 27 Message = message;28 29 30 publicString getUserName() 31 returnuserName;32 33 34 publicvoidsetUserName(String userName) 35 this.userName = userName;36 37 38 publ
2、icLong getId() 39 returnId;40 41 42 publicvoidsetId(Long id) 43 Id = id;44 45 46 publicString getMsgTime() 47 DateFormat dateFormat =newSimpleDateFormat(yyyy/MM/dd HH:mm:ss);48 Date date =newDate();49 returndateFormat.format(date);50 51 52 publicvoidsetMsgTime(String msgTime) 53 MsgTime = msgTime;54
3、 55 After creating the POJO class, create the hibernate configuration file, from which the application will come to know that how to map the tables column from database to the class members, database name and so on. The hibernate configuration file name must be “hibernate.cfg.xml” and it should resi
4、de at the root of source code.Code of hibernate.cfg.xml:1 2 !DOCTYPE hibernate-configuration PUBLIC3 -/Hibernate/Hibernate Configuration DTD/EN4 5 6 7 session-factoryname=java:hibernate/SessionFactory8 propertyname=hibernate.connection.driver_class com.mysql.jdbc.Driver9 hibernate.connection.url jdb
5、c:mysql:/localhost/test10 hibernate.connection.username username11 hibernate.connection.password pwd12 hibernate.connection.pool_size 1013 show_sql true14 dialect org.hibernate.dialect.MySQLDialect15 hibernate.hbm2ddl.auto update16 17 18 19 To keep the code simple, normally I add different configura
6、tions for different classes in hibernate.What is the need of dialect in hibernate:Databases implement subtle differences in the SQL they use. Things such as data types for example vary across databases Or database specific functionality selecting the top n rows is different depending on the database
7、. The dialect abstracts this so you dont have to worry about it. In short the dialect property internally creates the highly optimized query for underlying database.Here also, as you can see, I have included file ChatMessage.hbm.xml file.Code of ChatMessage.hbm.xml:xmlversion=1.0DOCTYPE hibernate-ma
8、pping PUBLIC-/Hibernate/Hibernate Mapping DTD 3.0/ENhibernate-mappingdefault-lazy=falseclassname=com.G2.pojo.ChatMessagetable=chatmessageidname=idtype=longcolumn=Idgeneratorclass=identity/idMessagecolumnname=userNameMsgTime20 colorSelected21 22 23 24 UI Part of the application:For the UI, I have wri
9、tten lots of css classes, this is not possible to write complete code here. Please check the code from download link provided at the bottom of the article.UI of the chat window is created to give the feel of the Gmail chat application as shown in below image:Gmail like chat window with emoticonsHere
10、, find the javascript code, which is responsible to send the AJAX request to the servlet, and getting back the response code:script type=text/javascript2 varuEnteredName = prompt(Please Enter Your Name3 $(#chat_Header).html(uEnteredName);4 $(#msg).focus();6 functionopenEmot()7 8 var$ele = $(#emotico
11、ns9 varvisibility = $ele.css(display10 11 if(visibility =none)12 13 $ele.show();14 15 else16 $ele.hide();17 18 19 20 functionsmileyCode(iconCode)21 22 var$msgEle = $(23 $msgEle.val($msgEle.val() + iconCode);24 var$ele = $(25 $ele.hide();26 27 $msgEle.focus();30 functionsaveChats() 31 varuName = $().
12、html();32 if(uName =) 33 alert(Please enter your name 34 returnfalse;35 36 varmsg = $().val();37 /var oldMsg = $(#chat-area38 varcolorCode = $(inputname=nameColor:checked,#send-message-area39 .val();40 41 $.ajax( 42 type :POST,43 data :uName=+ uName +&msg=+ msg +colorCode=44 + colorCode,45 url :Chat
13、process.do46 error :function(xhr, ajaxOptions, thrownError) 47 alert(xhr.status);48 alert(thrownError);49 ,50 success :function(data) 51 $().html(data);52 $(#ChatAtBigScreen53 document .getElementById(chat-area).scrollTop = document .getElementById().scrollHeight;54 document .getElementById(ChatAtBi
14、gScreen56 );57 returnfalse;58 59 $(#msg).keyup(function(e) 60 61 if(e.keyCode = 13) 62 saveChats();63 $().val(64 65 );66 Utility class, responsible to save the chat in the database using hibernate:1 packagecom.G2.Model;3 importjava.util.ArrayList;4 importjava.util.Iterator;5 importjava.util.List;7 i
15、mportorg.hibernate.Query;8 importorg.hibernate.Session;9 importorg.hibernate.SessionFactory;10 importorg.hibernate.Transaction;11 importorg.hibernate.cfg.Configuration;12 13 importcom.G2.pojo.ChatMessage;14 15 publicclassDBManager 16 / Read Hibernate.cfg.xml17 staticConfiguration cf =newConfiguratio
16、n().configure();18 staticSessionFactory factory = cf.buildSessionFactory();20 publicstaticvoidsaveChat(ChatMessage chat) 22 Session session =null;23 try24 25 session = factory.openSession();26 Transaction tx = session.beginTransaction();27 28 session.save(chat);29 mit();30 31 catch(Exception e) 32 e
17、.printStackTrace();33 finally34 if(session !=null) 35 / I faced problem here, if below line is not written then data automatically gets deleted after insertion36 session.flush();37 session.close();38 39 42 publicstaticList getMessages() 43 List MessageList =newArrayList();44 Session session =null;45
18、 try46 session = factory.openSession();47 String SQL_QUERY =from ChatMessage c;48 Query query = session.createQuery(SQL_QUERY);49 Iterator it = query.iterate();50 while(it.hasNext() 51 ChatMessage c = it.next();52 MessageList.add(c);53 54 55 catch(Exception e) 56 e.printStackTrace();57 finally58 ses
19、sion.flush();59 session.close();60 61 62 returnMessageList;63 As shown in above code, to work with hibernate, our application will need to read the configuration file of hibernate by below line of code:1 staticConfiguration cf =newConfiguration().configure();Now, we will need to create the session o
20、bject from the SessionFactory class:1 staticSessionFactory factory = cf.buildSessionFactory();2 Session session = factory.openSession();Create the object of transaction :1 Transaction tx = session.beginTransaction();To save the object in the database use save() method of the transaction.To get the l
21、ist of chat messages from the database :1 String SQL_QUERY =2 Query query = session.createQuery(SQL_QUERY);3 IteratorHere object of org.hibernate.Query is created and using method iterate(), it gives all the records from the database as a object. (That is what ORM Object Relation Mapping)Servlet Cod
22、e:1 packagecom.G2.servlets;3 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.util.HashMap;6 importjava.util.List;7 8 importjavax.servlet.ServletException;9 importjavax.servlet.http.HttpServlet;10 importjavax.servlet.http.HttpServletRequest;11 importjavax.servlet.http.HttpServletResponse;13 importcom.G2.Model.DBManager;14 importcom.G2.pojo.ChatMessage;15 16 publicclassChatProcessextendsHttpServlet 18 Override19 prot
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1