Hibernate updates data through SQL, and hibernatesql updates
1 @ Resource (name = "hibernateTemplate") 2 public HibernateTemplate hibernateTemplate; 3 4/** 5 * @ Title: updateBySQL 6 * @ Description: TODO (SQL update) 7 * @ param SQL statement 8 * @ param obj parameter 9 * @ param @ return10 * @ return int update quantity 11 * @ throws12 */13 public int updateBySQL (final String SQL, final Object [] obj) {14 return hibernateTemplate.exe cute (new HibernateCallback () {15 public Object d OInHibernate (Session session) 16 throws HibernateException, SQLException {17 SQLQuery query = session. createSQLQuery (SQL); 18 if (obj! = Null & obj. length> 0) {19 for (int I = 0; I <obj. length; I ++) {20 query. setParameter (I, obj [I]); 21} 22} 23 return query.exe cuteUpdate (); 24} 25}); 26}
1/** 2 * add test 3 */4 public void addTest () {5 String SQL = "insert test values (?, ?) "; 6 int I = this. updateBySQL (SQL, new Object [] {1, "test1"}); 7 System. out. println ("update records:" + I); 8}
Running result:
Database:
Note: I tested it directly in the project,
1. hibernateTemplate is injected through spring.
2. addTest is tested through junit.