You can generate a database based on a class's mapping file, or you can generate a persisted class based on a table in the database
<?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">className="domain. Userentity"table="User"Schema="Hibernate"> <id name="ID"column="ID"Type="int"> <!--PRIMARY key generation strategy, make as much as possible hibernate management--<generatorclass="native"/> </id> <property name="username"column="username"Type="java.lang.String"/> <property name="Password"column="Password"Type="java.lang.String"/> </class>3: Add Data
Save () and persist () both hold the data, but the Sava method caches the object, persist "stick" does not cache the object
Public classtest1 {sessionfactory sessionfactory; Session session; @Before Public voidbefore () {Configuration Configuration=NewConfiguration (); Configuration.configure (); Sessionfactory=configuration.buildsessionfactory (); Session=sessionfactory.opensession (); } /** * Test saved data save and persist are saved*/@Test Public voidTestsave () {Transaction Transaction=session.begintransaction (); Userentity userentity=Newuserentity (); Userentity.setusername ("Yu Wenhui"); Userentity.setpassword ("123456");//Session.save (userentity);session.persist (userentity); Transaction.commit (); } @After Public voidAfter () {session.close (); Sessionfactory.close (); }}
4: Load Data
Test load data get method and Load method
Get gets the SQL statement that is issued to the database immediately, and the persisted object is obtained
The load method calls for a proxy class object ( a subclass of Userentity) that only holds the ID of the persisted class (which confirms that the persisted class must have an ID as the primary key) until the non-primary key attribute of the object is used. To make angry statements to the database.
The GetClass () method does not use the object
** *Test Load data get method and Load method*Get gets the SQL statement that is issued to the database immediately, and the persisted object is obtained*The load method calls for a proxy class object that only holds the ID of the persisted class (which confirms that the persisted class must have an ID as the primary key) until the non-primary key attribute of the object is used , and the database is issued an angry statement*the GetClass () method does not use the object*/@Test Public voidTestget () {Transaction Transaction=session.begintransaction (); Userentity userentity= (userentity) session.Get(Userentity.class,1); System. out. println (Userentity.getclass ());/*class domain. Userentity here is the real userentity object*/ /*userentity user = (userentity) session.load (Userentity.class, 1); System.out.println (User.getclass ()); *//*class domain. Userentity_$$_jvst85e_0 here is a proxy object that only holds the ID of the persisted class*/ }
5: Modify Data
/* * * Test Update method * Check this object first, then change the object */ @Test public void testupdate () { = session.begintransaction (); = (userentity) session.load (userentity. Class1); User.setusername (" Yu Xiaoxi "); Session.update (user); Transaction.commit (); }
6: Delete Data
/* * * Test Delete method * First query to this object, and then delete the object */ @Test public void testdelete () { = session.begintransaction (); = (userentity) session.load (userentity. Class,3); Session.delete (user); Transaction.commit (); }
7: Basic HQL Query
Get a single object
/**
* Get a single object
*/
@Test
public void testhql () {
String hql = "from userentity WHERE id =?" and username =? ";
Query query = session.createquery (HQL);
placeholder parameter starting from 0, not starting from 1
Query.setparameter (0,1). Setparameter (1, "Yu Xiaoxi");
Gets the 0 subscript object in the collection
Userentity user = (userentity) query.list (). get (0);
SYSTEM.OUT.PRINTLN (user);
}
Get multiple Objects (collection)
/** * Query multiple objects, get a list in the collection*/@Test Public voidtesthqllist () {//the From is not a table name, but a persisted class nameString HQL ="From userentity"; Query Query=session.createquery (HQL); List<UserEntity> users =query.list (); for(userentity user:users) {System. out. println (User.tostring ()); } }
Hibernate basic configuration file and Crdu operation and basic HQL query