The previous array is studying o / r mapping, I feel that Hibernate and Ibatis are good, but iBatis is easier to get started, and when you deal with the database, you just need to configure the SQL statement in XML, not like HibernamTe to write composite specifications. HQL. Of course, Hibernate is now in a big way, and I have been studying hard for a while. Technology development is too fast, learning methods are important. (It seems that the opening source has indeed promoted the development of productivity. It is inferred that communist society will definitely be realized.) I think I have to abandon the learning method of the book in accordance with the class, but should be Study In Action, study in practice. Lack of supplementation, greatly refactivated. Of course, the premise is that there is a certain technology accumulation. In order to learn Ibatis, in addition to approving the official document of Ibatis, I put the main energy in the sample author's example JPETSTORE, everyone can go to download 4.0 version of the example, 3.x Don't take it. There is no 4.0, which is written by the code. Then go to download MySQL, Tomcat, and install the JetStore run up. I will not talk about this process, everyone will live a sense of accomplishment. Workers must be good, and must first make a tool. I use JBuilder9 because I have used Delphi's feelings to Borland's products. (Of course, Borland will have feelings to me, because ... ^ _ ^, Chinese characteristics). Built a new project, turn off the Project, then copy the JPETStore's code package to: Project / SRC / Under, open the project, you can see a bunch of packages under the project. Now compilation must have a problem, don't worry, load all .jar and servlet.jar in the JPETStore source lib directory in the project, in order to even Mysql, you have to load MySQL JDBC drivers. For specific practices, you can create three languages in JBuilder9 Tools / Config Libraries. .Jar, then modify the contents of JPETStore source src / proties / database.properties, for example, mm is driver = org.gjt.mm.mysql.driver URL = jdbc: mysql: // localhost / jpetstore username = jPETSTORE password = Ibatis9977 now make Project should be available. Ok, open all the .java files under the com.ibatis.jpetStore. The Domain directory is JavaBean, which is generally corresponding to the table. The Persistence directory is DAO interface and the specific implementation class, DAO is data access object (Data Access Object) is used to deal with the database. IBATIS 'o / r mapping is in this directory. The presentation directory is FormBean, followed by the JPETStore sample to create the creative use of Struts. The service directory is decentralized, and the method of calling the DAO object is invoked through the Service object. This is a common design pattern.
Common Struts programming should be an action corresponding to an action, such as writing a dating site, user login, and user queries to write two Actions, respectively. And JPETSTORE's author Clinton wrote an Action dynamic calling FormBean different ways to handle different actions.
The code is as follows, pay attention to the write method of Execute ()! public class BeanAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {String forward = "success"; try {ActionContext.initialize (request, response); if (! form = null) {// Explicit Method Mapping Method method = null; String methodName = mapping.getParameter ();. if (!!. methodName = null && "*" equals (methodName)) {try {method = form.getClass () getMethod ( methodName, null); forward = (String) method.invoke (form, null);} catch (Exception e) {throw new BeanActionException ( "Error dispatching bean action via method parameter ( '" methodName "') Cause:. " E, E);}} // path based method mapping if (Method == NULL &&!" * ". Equals (methodname)) {methodname = mapping.getpath (); if (MethodName.Length ()> 1 ) {INT SLASH = MethodName.lastIndexof ("/") 1; methodname = methodname.substring (Slash); if (MethodName.Length ()> 0) {Try {method = form.getClass (). GetMetho D (MethodName, NULL); Forward = (String) Method.invoke (Form, NULL);} catch (Exception E) {Throw new beanactionException ("ERROR DISPATCHON ACTION VIA URL PATTERN ('" methodname "). Cause: " E, E);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} catch (" beanactionException ", e); throw e;} return mapping.findforward (forward);}} is passed The reading of configuration information of Struts-Config.xml determines the call to the call, or does not call any method. E.g:
When ActionServlet receives request /Shop/viewcategory.shtml, (JPETSTORE suffix defines ".shtml") calling CatalogBean's ViewCategory method. How, have a creative, if you are willing, you don't have to write so much Action. Let's talk about iBATIs' o / r mapping. Please open the directory your project / com / ibatis / jpetstore / persistence / SQLMAPDAO / SQL, what did you see? In addition to the configuration file SQL-MAP-Config.xml, it is some XML corresponding to JavaBean, open SQL-MAP-Config.xml and ACOUNT.XML, then return to Your Project / Com / iBATIS / JPETSTORE / PERSISTENCE Directory, open DAO.XML to discover their relationship. It may be, you can't do this, don't know what to use, don't worry, Ibatis official documentation is very clear (with Chinese version). I roughly tell the role of those XML files corresponding to JavaBean, such as ACOUNT.XML, is a file that maps JavaBean and the database. You can write any SQL statement, parameters, and return values in accordance with Ibatis specification, parameters, and return values can be basic types and their packaging classes, MAP, custom classes. The most commonly used, of course, returns a custom class instance.
However, if the return value is a custom class instance, you need to define ResultMap first, such as:
select SIGNON.USERNAME as USERID, ACCOUNT.EMAIL, ACCOUNT.FIRSTNAME, ACCOUNT.LASTNAME, ACCOUNT.STATUS, ACCOUNT.ADDR1, ACCOUNT.ADDR2, ACCOUNT.CITY, ACCOUNT.STATE, ACCOUNT.ZIP, ACCOUNT.COUNTRY, ACCOUNT.PHONE , PROFILE.LANGPREF, PROFILE.FAVCATEGORY, PROFILE.MYLISTOPT, PROFILE.BANNEROPT, BANNERDATA.BANNERNAME from ACCOUNT, PROFILE, SIGNON, BANNERDATA where ACCOUNT.USERID = # value # and SIGNON.USERNAME = ACCOUNT.USERID and PROFILE.USERID = ACCOUNT .USERID and PROFILE.FAVCATEGORY = BANNERDATA.FAVCATEGORY
Open AccountService.JAVA: public Account getAccount (String username) {return accountDao.getAccount (username);} open AccountBean.java: public String newAccount () {try {accountService.insertAccount (account); account = accountService.getAccount (account. getUsername ()); myList // this call = catalogService.getProductListByCategory (account.getFavouriteCategoryId ()); authenticated = true; repeatedPassword = null; return "success";} catch (Exception e) {throw new BeanActionException ( "There Was a prosker. cause: " e, e);}} Write a half-day, write an example, try it. Package com.ibatis.jpetStore.Service; import com.ibatis.dao.client.daomanager; import com.ibatis.jpetstore.domain *;. import com.ibatis.jpetstore.persistence.DaoConfig; import com.ibatis.common.util.PaginatedList; import com.ibatis.jpetstore.persistence.iface *;. import java.util. *; public class ibatistest {private daomanager daomanager = DAOCONFIG.GETDAOMANAGER (); private accessdao accountdao; public ibatistest () {accountdao = (AccountDao) daoManager.getDao (AccountDao.class); Account account = accountDao.getAccount ( "j2ee", "j2ee"); System.out.println (account.getFirstName ());} public static void main (String [] Args) {ibatistest ibatistest = new ibatistest ();}} You can also try INSERT, UPDATE, DELETE. Ibatis can support paging, it is very cool, everyone is empty or spending a little effort to study it. About Debug: I might on the JPETStore on the first two days tried to write a dating website, basically very smooth, just feel that Debug is more troublesome, usually in order to find a mistake for a few hours. I didn't use the DEBUG with JBuilder, but it was sure that Debug on Delphi had a height. And JUnit, I just started learning, and it is useless. Here, I introduced myself to the way: 1. When you write new O / R mapping, you can write a class test containing the main method first, just like the test example above.
Second, when you want to track the variable value, you can use the IBATIS tape tool actionContext to write the variable to the session, such as you have you formbean: userbean.java, one way is to process the user login, you Track the user's username and password, you can write: public string logon () {try {user = userService.getuser (user.getusername (), user.getpassword (); if (user == null) {clear (); return "failure";} else {user.setPassword (null); authenticated = true; user.setMessageCount (messageService.getMessageNewCount (user.getUserId ()));. ActionContext.getActionContext () setSimpleMessage ( "newCount =" newCount. TOSTRING () "Touserid =" user.getuserid ()); // Write Session Return "failure";} // Import to Error.jsp} catch (Exception E) {throw new beanactionException ("There Was a quilem Logoning Your Account Information. Cause: " E, E);} Of course, you need to include the following statement import com.ibatis.struts.ArtionContext; error.jsp: <% @ Taglib URI =" struts-logic "prefix =" Logic "%> <% @ Taglib URI =" struts-bean "prefix =" bean "%> <% @ Taglib URI =" struts-html "prefix =" html "%> <% @ page import =" java.io .Printwriter "%>
Property = "class.name" />
<%
Exception E = (Exception) Request.GetaTribute ("beanactionexception");
E.PrintStackTrace (New PrintWriter (OUT);
%>
logic: present> Third, for the tracking variable, you can also use the log4j to write the variable value to the log, this method is very common, don't understand www.google.com check. Fourth, in order to avoid writing JavaBean cases or other errors, it is highly recommended to write beans to write beans to write to ordinary classes. To create a new bean with IDE, this IDE will help you find a lot of errors. I have a lot of suffering. Get start? If you have any questions, please leave a message. In order to post the XML, the old nose is hard, it is not easy. I am going to study Spring and Hibernate.