Hibernate updates data through SQL, and hibernatesql updates

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.