1. Create project, project name HIBERNATEDEMO9, directory structure
650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8F/57/wKiom1jbX9aSGv0sAAAsNtP4Duc045.png-wh_500x0-wm_ 3-wmp_4-s_3682325420.png "title=" Qq20170329151753.png "alt=" Wkiom1jbx9asgv0saaasntp4duc045.png-wh_50 "/>
2. Create a Lib directory storage jar file in the project, directory structure
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/8F/55/wKioL1jbX_2hHjG-AACKtSAR6X4933.png-wh_500x0-wm_ 3-wmp_4-s_1607087323.png "title=" Qq20170329151835.png "alt=" Wkiol1jbx_2hhjg-aacktsar6x4933.png-wh_50 "/>
3. Create the entity Bean Forum, package name (Com.mycompany.demo.bean) in the SRC directory,
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8F/57/wKiom1jbYC7z_wQVAABEbKBchFQ498.png-wh_500x0-wm_ 3-wmp_4-s_2051905769.png "title=" Qq20170329151923.png "alt=" Wkiom1jbyc7z_wqvaabebkbchfq498.png-wh_50 "/>
4. Entity Bean Forum content is as follows
Package com.mycompany.demo.bean;public class forum {private int fid;private string name;private int isshow;public int getisshow () {return isshow;} Public void setisshow (Int isshow) {this.isshow = isshow;} Public forum () {super ();} Public forum (String name) {super (); this.name = name;} Public int getfid () {return fid;} Public void setfid (Int fid) {this.fid = fid;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = prime * result + fid;result = prime * result + ((Name == null) ? 0 : name.hashcode ()); Return rEsult;} @Overridepublic boolean equals (object obj) {if (this == obj) return true;if (obj == null) return false;if (GetClass () != obj.getclass ()) return false; forum other = (Forum) obj;if (Fid != other.fid) return false;if ( Name == null) {if (Other.name != null) Return false;} else if (!name.equals (other.name)) return false;return true;}
5. In the SRC directory, create the Entity Bean Forum Mapping file Forum.hbm.xml, package name (Com.mycompany.demo.bean),
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/55/wKioL1jbYHji4nqyAABDO_3iYf4599.png-wh_500x0-wm_ 3-wmp_4-s_3388242157.png "title=" Qq20170329152036.png "alt=" Wkiol1jbyhji4nqyaabdo_3iyf4599.png-wh_50 "/>
6. The contents of the mapping file Forum.hbm.xml are as follows
<?xml version= "1.0" encoding= "Utf-8"? ><! doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd//en" "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" > <!--Package: Specify <class/> packages to be located -->
7. Create the tool class Hbnutil, package name (com.mycompany.demo.util) in the SRC directory,650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/55/wKioL1jbYUKgfbOlAAA-QRvpmn8085.png-wh_500x0-wm_ 3-wmp_4-s_2140923936.png "title=" Qq20170329152358.png "alt=" Wkiol1jbyukgfbolaaa-qrvpmn8085.png-wh_50 "/>
8. The contents of the tool class Hbnutil are as followsPackage Com.mycompany.demo.util;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class Hbnutil {private static sessionfactory sessionfactory;public static Session getsession () {if (sessionfactory = = NULL | | sessionfactory.isclosed ()) {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();} return sessionfactory.getcurrentsession ();}}
9. Create the Hibernate profile Hibernate.cfg.xml in the SRC directory,650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M02/8F/55/wKioL1jbYSGzICf5AAA4dwhVKIE239.png-wh_500x0-wm_ 3-wmp_4-s_2910669729.png "title=" Qq20170329152321.png "alt=" Wkiol1jbysgzicf5aaa4dwhvkie239.png-wh_50 "/>
The contents of the 10.Hibernate configuration file Hibernate.cfg.xml are as follows<?xml version= "1.0" encoding= "Utf-8"? ><! doctype hibernate-configuration system "http://www.hibernate.org/dtd/ Hibernate-configuration-3.0.dtd ">
11. Create the test directory in the project to store testing files, file name Manageforum, package name (Com.mycompany.demo.bean), directory structure650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/57/wKiom1jbYYDxYa5cAAA1zpL-j34055.png-wh_500x0-wm_ 3-wmp_4-s_2172248738.png "title=" Qq20170329152216.png "alt=" Wkiom1jbyydxya5caaa1zpl-j34055.png-wh_50 "/>
The contents of the 12.ManageForum test class are as followspackage com.mycompany.demo.bean;import java.util.iterator;import java.util.list;import org.hibernate.hibernateexception;import org.hibernate.session;import org.hibernate.criterion.projectionlist;import org.hibernate.criterion.projections;import org.hibernate.transform.transformers;import org.junit.before;import org.junit.test;import com.mycompany.demo.util.hbnutil;public class manageforum {private session session;@ Beforepublic void init () {session = hbnutil.getsession ();} /* * projection Query-sql */@Testpublic void testprojectionsql () {try { Session.begintransaction (); string sql = "Select fid,name from hnsq_forum"; List<forum> forums = session.createsqlquery (SQL). Setresulttransformer ( Transformers.aliastobean (Forum.class)). List ();for (forum forum : forums) { System.out.println (Forum.getname ());} SessioN.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * projection Query-hql */@Testpublic void testprojectionhql () {try { Session.begintransaction (); string sql = "Select new forum (name) from forum"; List<forum> forums = session.createquery (SQL). List ();for (forum forum : forums) {system.out.println (Forum.getname ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * projection Query-qbc */@Testpublic void testprojectionqbc () {try { Session.begintransaction (); Projectionlist projections = projections.projectionlist (). Add (Projections.alias ( Projections.property ("name"), "name"). Add (Projections.alias (Projections.property ("FID"), "FID")); List<forum> forums = session.creatEcriteria (Forum.class). Setprojection (projections). Setresulttransformer (Transformers.aliastobean (Forum.class)). List ();for (forum forum : forums) {system.out.println (Forum.getfid () + "=" + forum.getname ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Group Query-sql */@Testpublic void testgrounpforsql () {try { Session.begintransaction (); string sql = "Select isshow from hnsq_forum group by isshow"; List<boolean> lists = session.createsqlquery (SQL). List ();for (Boolean isshow : lists) {system.out.println (isshow);} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Group Query-hql */@Testpublic void testgrounpforhql () {Try {session.begintRansaction (); string sql = "Select isshow from forum group by isshow"; List<object> lists = session.createquery (SQL). List ();for (object isshow : lists) {system.out.println (isshow);} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * Group Query-qbc */@Testpublic &NBSP;VOID&NBSP;TESTGROUNPFORQBC () {try { Session.begintransaction (); List<object> lists = session.createcriteria (Forum.class). SetProjection ( Projections.groupproperty ("Isshow")). List ();for (object isshow : lists) { System.out.println (isshow);} Session.gettransaction (). commit ();} catch (hibernateexception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * query list Query * 1. All eligible records are queried at once * 2. No caching mechanism is used, each query is fetched from the database */@ testpublic Void testqueryforlist () {try {session.begintransaction (); string hql = "From forum";//Output sqllist<forum> forums = Session.createquery (HQL). List ();for (forum forum : forums) {system.out.println ( Forum.getname ());} Will output Sqllist<forum> forums2 = session.createquery (HQL). List ();for (Forum forum &NBSP;:&NBSP;FORUMS2) {system.out.println (Forum.getname ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * query iterate query * 1. The ID of all eligible records is queried, and then the specific contents of the record are queried based on the ID * 2. Using caching mechanisms, first read from the cache, If no more data is read from the database * 3. N+1 problem */@Testpublic void testqueryforiterate () {try { Session.begintransaction (); string hql = "From forum";//Output sqliterator<forum> forums = Session.createquery (HQL). Iterate (); while (Forums.hasnext ()) {System.out. println (Forums.next (). GetName ());} This generates the SQL for the query ID, and the other specific record information reads the data iterator<forum> forums2 = session.createquery (HQL) from the cache based on the ID. Iterate (); while (Forums2.hasnext ()) {System.out.println (Forums2.next (). GetName ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); E.printstacktrace ();}} /* * solving n+1 problem */@Testpublic void testnplusone () {try {session.begintransaction () ; string hql = "From forum";//Output Query sqllist<forum> forums = Session.createquery (HQL). List ();for (forum forum : forums) {system.out.println ( Forum.getname ());} This generates the SQL for the query ID, and the other specific record information reads the data iterator<forum> forums2 = session.createquery (HQL) from the cache based on the ID. Iterate (); while (Forums2.hasnext ()) {System.out.println (Forums2.next (). GetName ());} Session.gettransaction (). commit ();} catch (exception e) {session.gettransaction (). rollback (); e.printStackTrace ();}}}
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M01/8F/55/wKioL1jbYbGSgGh_AABwCKMO5XE044.png-wh_500x0-wm_ 3-wmp_4-s_3768817016.png "title=" Qq20170324102905.png "alt=" Wkiol1jbybgsggh_aabwckmo5xe044.png-wh_50 "/>
This article from the "Vegetarian Yan" blog, declined to reprint!
hibernate5-projection queries, group queries, query list and iterate