Want to use the Hibernate framework, read a hibernate learning video online, try to do a small Java connection database operation, Java beginner One, we have a lot to forgive
Development environment:
1. Install MySQL,
2. Install the eclipse+ plugin hibernatetools-update-4.1.2 plugin
3.java Package Import 1.hibernate-core 2.junit4.0 3.mysql
Ideas:
1. Configure Hibernate configuration Files
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "Http://hibern Ate.sourceforge.net/hibernate-configuration-3.0.dtd "><hibernate-configuration> <session-factory> < Propertyname= "Connection.username">Root</ Property> <!--Database user name - < Propertyname= "Connection.password"></ Property> <!--Database Password - < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property> < Propertyname= "Connection.url">Jdbc:mysql:///hibernate?useunicode=true&Charactertencoding=utf-8</ Property> < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property> < Propertyname= "Show_sql">True</ Property> <!--SQL output to console - < Propertyname= "Format_sql" >True</ Property> <!--Formatting SQL - < Propertyname= "Hbm2ddl.auto">Update</ Property> <!--create|update|create-drop|validate: Check that the data table structure is the same - <MappingResource= "Students.hbm.xml" /> <!--Student configuration Files - </session-factory></hibernate-configuration>
2. Definition of persistent words student class
//Student Class Public classStudents {//1. Common Classes//2. Provide the default construction method of the public without parameters//3. Property Private//4. Attribute Setter/getter Encapsulation Private intSID; PrivateString sname; PrivateString Gender; PrivateString address; PublicStudents () {} PublicStudents (intsid, String sname, String gender, string address) { //super (); This. Sid =SID; This. sname =sname; This. Gender =gender; This. Address =address; } Public intGetSID () {returnSID; } Public voidSetsid (intSID) { This. Sid =SID; } PublicString Getsname () {returnsname; } Public voidsetsname (String sname) { This. sname =sname; } PublicString Getgender () {returngender; } Public voidSetgender (String gender) { This. Gender =gender; } PublicString getaddress () {returnaddress; } Public voidsetaddress (String address) { This. Address =address; } @Override PublicString toString () {return"Students [sid=" + Sid + ", sname=" + sname + ", gender=" + Gender + ", address=" + address + "]"; }}
3. Configure the Students.hbm.xml file, this is done automatically by the Hibernate plugin tool, src Right-click-other-hibernate-hbm.xml configuration file
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--Generated 2015-6-25 19:45:24 by Hibernate Tools 3.4.0.CR1 -<hibernate-mapping> <classname= "Students"Table= "STUDENTS"> <IDname= "Sid"type= "int"> <columnname= "SID" /> <Generatorclass= "Assigned" /> </ID> < Propertyname= "Sname"type= "Java.lang.String"> <columnname= "SNAME" /> </ Property> < Propertyname= "Gender"type= "Java.lang.String"> <columnname= "GENDER" /> </ Property> < Propertyname= "Address"type= "Java.lang.String"> <columnname= "ADDRESS" /> </ Property> </class></hibernate-mapping>
4. Define a Studentstest test class
Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;ImportOrg.hibernate.service.ServiceRegistry;ImportOrg.hibernate.service.ServiceRegistryBuilder;ImportOrg.junit.After;ImportOrg.junit.Before;Importorg.junit.Test;//Test Class//@SuppressWarnings ("deprecation") Public classStudentstest {PrivateSessionfactory sessionfactory;//Session Factory PrivateSession session;//Session PrivateTransaction Transaction; @Before//before Public voidInit ()//Initialize { //creating a Configuration objectConfiguration config=NewConfiguration (). Configure (); //Create a Service registration objectServiceregistry serviceregistry=NewServiceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); //To create a session factory objectsessionfactory=config.buildsessionfactory (serviceregistry); //Session Objectsession=sessionfactory.opensession (); //Open Transactiontransaction=session.begintransaction (); } @After//after Public voiddestory () {transaction.commit (); //Commit a transactionSession.close ();//Close SessionSessionfactory.close ();//Close Session Factory} @Test//Test Public voidtestsavestudents () {Students s=NewStudents (3, "Zhang San Feng", "male", "Wudangshan"); Session.save (s); Students S1=NewStudents (4, "Shexunyu", "Male", "xianning"); Session.save (S1); //saving objects into the database } }
5. When this is done, the directory structure of the project is given to everyone
6. You can right-click studentstest right-runas-junit test it, OK, code I uploaded, click here to download
Http://files.cnblogs.com/files/shexunyu/Java-Hibernate_002.zip
User:shexunyu date:2015-06-26 Contact:
Test Hibernate+mysql Simple data storage