First, we need to import the jar package of hibernate. Under the lib folder, there is a necessary folder to import the jar package inside.
The second step is to create the ORM mapping file under the bean package and put it together with the beans to be mapped. Then we import the constraints. We don't have to memorize them. We can
Under this package, find the hibernate-mapping-3.0.dtd file to open and copy its contents
Then open window--Preferences -- search the cata settings constraint file's lookup path, set it as a local file, you can get prompts in a non-networked environment.
We unzip the jar above to extract the hibernate-mapping-3.0.dtd file, and then set it. In the selection of url, set the URL above.
As shown in the figure:
The same is true of master configuration files
Attach the contents of these two configuration files and the code for the bean s
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="bean"> <class name="Customer" table="cst_customer"> <id name="cust_id" column="cust_id"> <!-- Policies for generating primary keys --> <generator class="native"></generator> </id> <property name="cust_name" column="cust_name"></property> <property name="cust_user_id" column="cust_user_id"></property> <property name="cust_create_id" column="cust_create_id"></property> <property name="cust_source" column="cust_source"></property> <property name="cust_industry" column="cust_industry"></property> <property name="cust_level" column="cust_level"></property> <property name="cust_linkman" column="cust_linkman"></property> <property name="cust_phone" column="cust_phone"></property> <property name="cust_mobile" column="cust_mobile"></property> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <! - is divided into three modules, the file name is fixed format: hibernate. cfg. XML - >. <session-factory> <! - 5 required items - > 5 <! - Find its property under the etc folder - >. <property name="hibernate.connection.url">jdbc:mysql:///study</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <! - Set up the dialect type of database, which is a general type. Different dialect forms will be generated according to different database engines - > Set up the dialect type of database. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <! - Three optional items - > <! - Print sql statements executed <property name="hibernate.show_sql">true</property> <! - Is the sql statement printed in good format? <property name="hibernate.format_sql">true</property> There are four kinds of values in the automatic export table structure: create: create tables automatically, creating new tables every time the framework runs create-drop: Create tables automatically and delete them at the end of each frame run Update: Create tables automatically. If tables exist, they will not be recreated. If the table structure changes, update the table structure automatically. Instead of deleting data, they will recreate columns. validate: No tables will be created automatically, the structure of tables will be validated every time they are started, and if there are any changes, errors will be reported - >. <property name="hibernate.hbm2ddl.auto">update</property> <! - Associated orm mapping file - > <mapping resource="bean/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
public class Customer { private long cust_id; private String cust_name; private long cust_create_id; private long cust_user_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; //The set get method is not sticky }
Then let's write a test class and run it.
Relevant explanations I have recently written are very detailed, so I am not talking, ah, sleepy, sleeping now! ___________package test; import static org.junit.Assert.*; import java.util.UUID; import javax.swing.text.DefaultEditorKit.CutAction; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import bean.Customer; public class test1 { @Test public void test() { //The first step is to create the configuration object and load the configuration Configuration configuration = new Configuration(); //Load the default configuration file configuration.configure(); //Get session factory, which has better memory resources SessionFactory buildSessionFactory = configuration.buildSessionFactory(); //Create a new session using session factory Session session = buildSessionFactory.openSession(); Transaction transaction = session.beginTransaction(); try { //increase Customer customer = new Customer(); customer.setCust_id(2l); customer.setCust_name("Dark horse"); session.save(customer); //query customer = session.get(Customer.class, 1L); System.out.println(customer.toString()); //change session.update(customer); //Delete session.delete(customer); //Submitting transaction.commit(); }catch (Exception e) { transaction.rollback(); } //Close session session.close(); //Close session factory buildSessionFactory.close(); } }