Hibernate object type to ing File
1. Write the hibernate ing file to convert the object type into a table in the database.
The ing file is written based on the object type.
Entity type User. java
Package cn. wwh. www. hibernate. dd. property; import java. util. arrays; import java. util. date;/*** function of the class: *** @ author @ * @ version 1.0 * @ Creation Time: 08:05:30 */public class User {private Integer id; private String name; // name: private boolean gender; // true indicates male, false indicates female private Date birthday; // birthday private String desc; // a large description, up to 5000 words private byte [] photo; // photo/*** @ return the id */public Integer getId () {return id ;} /*** @ param id the id to set */public void setId (Integer id) {this. id = id;}/*** @ return the name */public String getName () {return name ;} /*** @ param name the name to set */public void setName (String name) {this. name = name;}/*** @ return the gender */public boolean isGender () {return gender ;} /*** @ param gender the gender to set */public void setGender (boolean gender) {this. gender = gender;}/*** @ return the birthday */public Date getBirthday () {return birthday ;} /*** @ param birthday the birthday to set */public void setBirthday (Date birthday) {this. birthday = birthday;}/*** @ return the desc */public String getDesc () {return desc ;} /*** @ param desc the desc to set */public void setDesc (String desc) {this. desc = desc;}/*** @ return the photo */public byte [] getPhoto () {return photo ;} /*** @ param photo the photo set */public void setPhoto (byte [] photo) {this. photo = photo;} @ Overridepublic String toString () {return "User [birthday =" + birthday + "\ n desc =" + desc + "\ n gender =" + gender + "\ n id =" + id + "\ n name = "+ name +" \ n photo = "+ Arrays. toString (photo) + "]" ;}}
2. ing file User. hbm. xml:
3. Main configuration file:
Hibernate. cfg. xml
Com. mysql. jdbc. Driver
Jdbc: mysql: // localhost: 3306/hibernate
Root
Wwh
Org. hibernate. dialect. MySQL5Dialect
Update
True
True
4. Test the data in the database:
Package cn. wwh. www. hibernate. dd. property; import java. io. fileInputStream; import java. util. date; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. cfg. configuration; import org. junit. test;/*** function: *** @ author a boat * @ version 1.0 * @ Creation Time: 08:10:38 */public class TestSqlByXml {private static SessionFactory sessionFactory = new Configuration (). configure (). B UildSessionFactory (); // save @ Testpublic void testSave () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // --------------------------------------- // read the image from the hard disk, and then store the database FileInputStream in = new FileInputStream ("F:/psb.jpg "); byte [] photo = new byte [in. available ()]; in. read (photo); in. close (); // prepare the Object User user = new User (); user. setName ("one leaf and one boat"); user. setGender (true); User. setBirthday (new Date (); user. setDesc ("walking boat is a positive child, striving for a dream ............ "); User. setPhoto (photo); // Save the session. save (user); // ------------------------------------------- session. getTransaction (). commit (); session. close () ;}// get @ Testpublic void testGet () throws Exception {Session session = sessionFactory. openSession (); session. beginTransaction (); // ------------------------------------------- User user = (User) session. get (User. class, 1); System. out. println (user. getName (); System. out. println (user. isGender (); System. out. println (user. getBirthday (); System. out. println (user. getDesc (); System. out. println (user. getPhoto (); // System. out. println (user); // ------------------------------------------- session. getTransaction (). commit (); session. close ();}}