Develop Hibernate applications with Eclipse

xiaoxiao2021-03-06  70

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

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

New Post(0)