Develop Hibernate applications with Eclipse

xiaoxiao2021-03-06  39

Develop Hibernate applications with Eclipse

BROMON Original, please respect the copyright eclipse is currently very popular development platform. The open extension architecture allows many programmers to find their personalized working environment. Hibernate is also a framework that is currently can't have. Before the EJB 3, it is, it It is our very good choice when considering the OR mapping. This article mainly describes how to quickly develop Hibernate applications in Eclipse 3.0.1 in Eclipse 3.0.1, improve our work efficiency. For details on the Hibernate framework, this article does not do too much discussion, please refer to the relevant information, recommend the book "Hibernate in Action". First, you need to install this plugin, Eclipse 3.0.1 provides us with more convenient plug-in management, select menu "Help - Software Update - Find and Install" (I use the language to use language, please contact English interface users) , Select "New Features to Search to Install", the operation interface is as follows: Select "New Remote Site", fill in the name "Hibernate plugin", fill in the URL below: http://www.binamics.com/HibernateSync as shown below Then: then select "Hibernate Plugin" we just added in the "Install" interface, all the way, Eclipse can download the installation yourself, then Eclipse automatically starts once. The following demonstrates a simplest single-meter operation that allows us to be familiar with the development process. We chose the mysql database to do this application, first create a data table, named user_info, including the three fields of ID, Name, Password, the table statement is as follows:

# Host: localhost # database: Test # table: 'user_info' # Create Table `User_info` (` ID` int (11) Not null auto_increment, `Name` VARCHAR (100) Not null default ',` Password', `Password` VARCHAR 100) Not null default ', primary key (`ID`);

Then create a normal Java project: "New-Project-Java Project", pay attention to all lib files for Hibernate, and MySQL JDBC driver files. Below we need to join a Hibernate profile in the project, select "New-other -Hibernate-Hibernate Configuration File" in the src directory, as shown below: In the pop-up interface, you need to specify the database you want to use, and connect the database The information we need, we correspond to the database as mysql, and configure the database's URL and administrator account, as follows: In the above image, we can also specify the connection from the JNDI data source, you can click "Datasource" tab Configure. After "completion", the system will generate a file called "hibernate.cfg.xml", which contains basic configuration information, which can be added manually if you need a advanced configuration. Below we have to generate a mapping file, first create a new package "org.bromon.ziz.pojo", select "New-other -Hibernate-Hibernate mapping file" under this package, click "Refresh" in the pop-up interface, talk List all the data tables in the library, select the "User_info" table we want to use, and the package we have to generate the Pojo file: org.bromon.zizz.pojo. As shown below: Other options for HBM can be configured in the "Properties" tab of the above interface, including file extensions, aggregate column names, ID generation rules, and the like. After completing, the system will automatically generate a file called "userinfo.hbm", we can generate related stubs through this file. Select "Hibernate Synchronizer-Synchronize Files" on the userinfo.hbm file: This action will generate eight packets of eight class files, which encapsulates Hibernate's operation details, let us focus on the development of business logic, read these files carefully You can improve your understanding of Hibernate and grow application skills. Then we need to add information about Userinfo in Hibernate, select "Synchronizer-Add Mapping Reference" on UserInfo.hbm. Now we can start writing your own program logic, code can be written in org.bromon.ziz.pojo.dao. UserinFodao, this class's code will not be modified by the plugin. We first create a new record in this class: public static void main (string args []) {try {_rootdao.initialize (); userinfodao uid = new userinfodao (); org.bromon.zizz. Pojo.userinfo ui = new org.bromon.zizz.pojo.userinfo (); ui.setname ("bromon"); UI.SetPassword ("123"); uid.save (ui);} catch (Exception E) { E.PrintStackTrace ();}}

It can be seen that the plugin has been encapsulated in the SESSION operation and transaction operation, and our code work has been greatly simplified. And we can use the Hibernate Editor with the plugin to edit the HBM file, which is very convenient, we need to change the iD generation method to "Identity": Let this program run normally, you also need to make some configuration file hibernate.cfg.xml modify. It is worth noting that please modify the configuration file (such as a tool to edit the file using Dreamweaver) in an environment other than the plugin, otherwise it may cause an error that is not synchronized with resources, which may be a bug of the plugin. Use the Eclipse's "Text Editor" to open the file, which is as follows: net.sf.hibernate.Transaction.jtatransactionFactory Run .., pop up the settings window, fill in or select Org.Bromon.ziz.POJO. DAO.USERINFODAO, click the "Run" button. 2. Category Library Question: "Note All LIB files for Hibernate, as well as MySQL JDBC driver files", it can be described so that many newcomers will use "Add External JAR" in the "Library" page in the Build Setup dialog box. To join the JAR droved by Hibernate and MySQL, this is not tasteless, but I think, here can write a step, tell novice, develop a good habit, Hibernate library as an example "Add Library" -> User Library -> User Library -> New-> Hibernate-> Add JAR, join all JAR files under Hibernate (including the root directory and jar under the lib directory), which will then establish the Hibernate project in the future, simply select Hibernate in the user library. . 3. Path problem: This is a big problem. If you are a newcomer to run Eclipse, you will do it step by step, you will report an exception ... hibernate.cfg.xml not found ...; resolve: Copy this file to SRC The directory is OK. Don't worry, have not finished, we found a problem, how did you generate a class? There is no Class directory, how do you do it? One check, found that Class is in the same directory with Java. It turned out that it was not set correctly when establishing a Java project. Solve: In the "New Java Project" dialog box, "Project Layout" should choose the second "Create a separate source folder and output folder"; for future convenience, click "Default Default" button, option " Source and Output Folders Select the second "folder", the default source and output folders are SRC, BIN, and Classes will be created automatically. The application is then approved. After setting this setting, there will be no problem in accordance with the article step by art. In addition, when establishing mapping files in the article, the path in Container is / zizz/src/org/bromon.zizz.pojo, which does not match the path in the figure, need to manually select SRC.

转载请注明原文地址:https://www.9cbs.com/read-72389.html

New Post(0)