Tag: is the obj parameter binding print parameter int span class Erb
------------I do not have him, but the hand is ripe, humble and foolish, and eager to be hungry-------------
This blog will explain the parameter binding in Hibernate , which is equivalent to the condition behind the Where in the SQL statement
One, explain the summary :
1. binding parameters by subscript
2. binding parameters by custom parameter names (more for multi-table operations)
3. bind parameters by passing in custom objects (more for single-table operations)
4. bind parameters in a way similar to smart tags (more for multi-table operations with conditions)
Two, bind the parameter by the way of subscript
@Test/*specify parameters by subscript*/ Public voidT01parameterbyunderid () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where address=?" and Sal>? "; Query Query=session.createquery (HQL); /*Assign Value*/Query.setparameter (0, Teacher.getaddress ()); Query.setparameter (1, Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/ for(Object t:list) {System.out.println (t); } }
Third, the parameter is bound by the way of the custom parameter name (more for multi-table operation)
@Test/*specifying parameters by customizing parameter names*/ Public voidT02parameterbymyselfkey () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where Address=:teaadd and Sal>:teasal"; Query Query=session.createquery (HQL); /*Assign Value*/Query.setparameter ("Teaadd", Teacher.getaddress ()); Query.setparameter ("Teasal", Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/ for(Object t:list) {System.out.println (t); } }
Four, binding parameters by passing in custom objects (more for single-table operations)
@Test/*specifying parameters by customizing the object*/ /*suitable for single-table operation*/ Public voidT03parameterbymyselfobject () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/String hql= "from Teacher where address=:address and Sal>:sal"; Query Query=session.createquery (HQL); /*Assign Value*/query.setproperties (teacher); /*Enquiry*/List List=query.list (); /*Traverse*/ for(Object t:list) {System.out.println (t); } }
Five. bind parameters in a way similar to smart tags (more for multi-table operations with conditions)
@Test/*specifying dynamic parameters by customizing parameter names*/ /*for multi-table operations*/ Public voidT04parameterbydongtai () {/*simulating foreground input parameters*/Teacher Teacher=NewTeacher ("A", 400.00); /*HQL Statements*/StringBuffer hql=NewStringBuffer ("From Teacher where 1=1"); if(Teacher.getaddress ()! =NULL) {hql.append ("And Address=:teaadd"); } if(Teacher.getsal () >0) {hql.append ("And Sal>:teasal"); } Query Query=Session.createquery (hql.tostring ()); /*Assign Value*/Query.setparameter ("Teaadd", Teacher.getaddress ()); Query.setparameter ("Teasal", Teacher.getsal ()); /*Enquiry*/List List=query.list (); /*Traverse*/ for(Object t:list) {System.out.println (t); } }
Parameter bindings in the Hibernate-orm:07.hibernate