(The Hibernate learning Note series comes from "Crazy Java" video learning)
Hibernate overview
免费开源Java包,可以使程序和数据库的交互变得容易,并更加符合面向对象的设计。持久化:将数据保存到数据库中
Hibernate Persistence class Steps
- Environment construction
- Import the jar package, in the \lib\required folder in the Hibernate package
- Importing the JDBC driver for MySQL
- Add the above package to the path
Writing persisted classes
Requirements:
- In a persistent class, you typically need an ID, usually with an integer type, so that the operation uses NULL instead of 0, so that when you add null to the database, the database is automatically added
- Provide getter and setter for attributes
- This class cannot be final.
- You need to provide an argument-free constructor for this class (reflection technology)
- If there is an operation that involves collection data, the collection type uses the interface type, such as list, rather than ArrayList
Writing a mapping file
Write a mapping file that is the same as the persisted class name, and you can use the project folder in the Hibernate package to search for the *.hbm.xml file, randomly select one, and copy it as a template.
The same name as the persisted class class is for subsequent maintenance and is placed in the package where the persistence class resides.
<?xml version= "1.0"?><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernat E-mapping-3.0.dtd "><!--mapping configuration file --<!--The relationship between persisted classes and tables--<hibernate-mapping package ="Packages where class is located"> <!--Object-oriented thinking-- <class name="persisted class" table ="table name"> <!--properties and column names -- <ID name="id attribute in persisted class"> <!--ID Generation policy (refer to dialect settings in the Cfg.xml file)-- <generator class="native" /> </ID> <!--If column is configured, create the corresponding columns name for the setting, otherwise create a column name with name-- <property Name="attribute" column="corresponding column name"/> < property name="attribute 1" />....</class></hibernate-mapping>
Configure the connection database configuration file
文件模板可以从hibernate包的project/etc/hibernate.cfg.xml中拷贝。
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> <!--show executed SQL statements, development phase -- < property name="Show_sql">True</Property > <!--project/etc/hibernate.properties can find the database-driven configuration and then refer to the write- <!--database-driven < property name="Hibernate.connection.driver_class">Com.mysql.jdbc.Driver</Property > <!--database URL, three slash (/) to connect this machine- - < property name="Hibernate.connection.url">Jdbc:mysql:///test</Property > <!--user name -- < property name="Hibernate.connection.username">Gavin</Property > <!--password -- < property name="Hibernate.connection.password">Root</Property > <!--the database table corresponding to the automatic mapping file Create-drop creates the corresponding database table structure when the program starts, and when Sessionfactory is closed, the table structure is deleted, and the test uses Create deletes the last created table structure each time the program starts, and then creates the corresponding database table structure, and the test uses update to append and modify the table structure each time it is started, but does not affect the original data (most commonly used is this) Validate verifies and modifies the table structure each time it is started the delete operation for these configurations does not necessarily remove the < property name="Hibernate.hbm2ddl.auto">Update</Property > <!--dialect, this dialect supports transactions if you are using a MySQL database version after 5.5, it is recommended to use Org.hibernate.dialect.MySQL5InnoDBDialect Mysqldi Alect does not support transaction Mysqlinnodbdialect support transactions -- < property name="Hibernate.dialect">Org.hibernate.dialect.MySQLInnoDBDialect</Property > <!--The path to the object/relationship map file-- <mapping Resource="Path/xxx.hbm.xml"/> </session-factory></hibernate-configuration>
Persisting to a database
//Create a class with the main method content in the class as follows //Read config file, create configuration instance / * Public Configuration Configure () throws hibernateexception{Configure ("/hibernate.cfg.xml"); return this; } */Configuration config =NewConfiguration (). Configure ();//This method is hibernate4 prior to the acquisition method //Get factory based on configuration file //sessionfactory is consistent with the application life cycle and is shared across multiple threadsSessionfactory factory = Config.buildsessionfactory ();//One-time interaction with the databaseSession session = Factory.opensession ();//When using Hibernate to add, delete, modify, require a transaction //query does not require transactionsTransaction tx = Session.begintransaction ();//Create a class person object that needs to be persistedPerson person =NewPerson (); Person.setname ("Li"); Person.setpassword ("123456"); Person.setbirthday (NewJava.util.Date ());//Save objects to the databaseSession.save (person);//Commit a transactionTx.commit ();//Close session, end this connectionSession.close ();
- Make sure the database exists and run the above project
Hibernate Learning Note II (initial hibernate)