Generally, we will create a hibernate. cfg. xml file. This is undoubtedly good. Although the method described below is not good, it is undoubtedly a method;
We can directlyCodeSet a series of parameters;
Main functions:
(1) configuration Config = new configuration ();
// Create a configuration
(2) config.Setproperties(Properties P );
// Import the configuration
(3) config.Addclass(Class C );
// Create a ing (only the class object needs to be specified and the ing file is automatically searched)
Note: When you configure parameters in the code, Hibernate must be added before the parameters, such as Hibernate. Connection. username, which cannot be written as connection. username. If no parameter is added, an error is returned.
package Org. xiazdong; import Java. util. properties; import Org. hibernate. sessionfactory; import Org. hibernate. transaction; import Org. hibernate. cfg. configuration; import Org. hibernate. classic. session; public class usertest {public static void main (string [] ARGs) {properties P = new properties (); p. put ("hibernate. connection. driver_class "," com. mySQL. JDBC. driver "); p. put ("hibernate. connection. URL "," JDBC: mysql: // hibernate "); p. put ("hibernate. connection. username "," root "); p. put ("hibernate. connection. password "," 12345 "); p. put ("hibernate. dialect "," org. hibernate. dialect. mysqlinnodbdialect "); p. put ("hibernate. hbm2ddl. auto "," Update "); configuration conf = new configuration (). setproperties (P ). addclass (user. class); sessionfactory Sf = Conf. buildsessionfactory (); Session session = SF. opensession (); transaction Tx = session. begintransaction (); User u = new user (); U. setname ("xiazdong-1"); U. setage (20); Session. save (U); Tx. commit (); Session. close (); SF. close () ;}