Spring4 Learning Notes-SPRINGJDBC Basic operations (supplemental)

Source: Internet
Author: User

Xiangtan project using SPRING+CXF to write the server side, the JDBC operation is all done by spring, and one of the typical instances is documented below.

public class userinfo_lvdaoimpl implements userinfo_lvdao {         //JdbcTemplate objects through annotation injection @autowiredprivate jdbctemplate jdbctemplate;/**  *  queries users based on name and password. Returns the query to the Userinfo_lv object  */@Overridepublic  userinfo_lv userinfo_lv_get (string name, string  password)  {String sql =  "select * from userinfo_lv where  name = ? and password = ? "; Rowmapper<userinfo_lv> rowmapper = new beanpropertyrowmapper<userinfo_lv> ( Userinfo_lv.class); Try {return jdbctemplate.queryforobject (sql, rowmapper, name,  password);}  catch  (Exception e)  {return null;}} /** *  Add a user. Returns the Userinfo_lv object that was added successfully  */@Overridepublic  userinfo_lv user_add (string name, string &NBSP;PASSWORD,&NBSP;INT&NBSP;ROLE,INT&NBSP;GROUPID) &NBSP;{STRING&NBsp;sql =  "insert into userinfo_lv  (name, password, role )  values   (?,  ?, ?) "; Int update = jdbctemplate.update (sql, name, password, role);if  (update  > 0)  {//  Add Success Return userinfo_lv_get (Name, password);//  returns the object that was added successfully}return  null;} /** *  queries the user by ID. Returns the query to the Userinfo_lv object.  */@Overridepublic  userinfo_lv getuserinfo_lv (int id)  {String sql =  " select * from userinfo_lv where id = ? "; Rowmapper<userinfo_lv> rowmapper = new beanpropertyrowmapper<userinfo_lv> ( Userinfo_lv.class); Try {return jdbctemplate.queryforobject (sql, rowmapper, id);}  catch  (Exception e)  {return null;}} /** *  queries users by user name. Returns the query to the Userinfo_lv object.  */@Overridepublic  userinfo_lv getuserinfo_lv (string name)  {string sql =  "SELECT&NBSP;*&NBSP;FROM&NBSP;USERINFO_LV&NBSP;WHERE&NBSP;NAME&NBSP;=&NBSP;?"; Rowmapper<userinfo_lv> rowmapper = new beanpropertyrowmapper<userinfo_lv> ( Userinfo_lv.class); Try {return jdbctemplate.queryforobject (sql, rowmapper, name);}  catch  (Exception e)  {return null;}} /** *  queries the user through role.  */@Overridepublic  list<userinfo_lv> getuserinfo_lvsbyrole (int role)  {String  sql =  "SELECT&NBSP;*&NBSP;FROM&NBSP;USERINFO_LV&NBSP;WHERE&NBSP;ROLE&NBSP;=&NBSP;?"; List<userinfo_lv> userinfo_lvs = jdbctemplate.query (sql, new  Rowmapperresultsetextractor (New userinfo_lvrowmapper ()),  role); Return userinfo_lvs;} /** *  query user  */through GroupID @Overridepublic  list<userinfo_lv> getuserinfo_ Lvsbygroupid (int groupid)  {String sql =  "select * from userinfo_lv& nbsp; where groupid = ? "; List<userinfo_lv> userinfo_lvs = jdbctemplate.query (sql, new  Rowmapperresultsetextractor (New userinfo_lvrowmapper ()),  groupid); Return userinfo_lvs;} private class userinfo_lvrowmapper implements rowmapper{@Overridepublic  Object  Maprow (Resultset rs, int index)  throws sqlexception {userinfo_lv userinfo_ Lv = new userinfo_lv (); Userinfo_lv.setgroupid (Rs.getint ("GroupID")); Userinfo_lv.setid (rs.getInt ("id")); Userinfo_lv.setname (rs.getstring ("name")); Userinfo_lv.setpassword (rs.getstring ("password")); userinfo_ Lv.setrole (Rs.getint ("role")); return userinfo_lv;}} /** *  Delete user based on ID  *  If delete succeeds, the deleted Userinfo_lv object is returned. Otherwise return null */@Overridepublic  userinfo_lv user_delete (int id)  {string sql =   "DELETE&NBSP;FROM&NBSP;USERINFO_LV&NBSP;WHERE&NBSP;ID&NBSP;=&NBSP;?"; Int update = jdbctEmplate.update (Sql, id);if  (update > 0)  {//Delete success return getuserinfo_lv (ID);//   Return Delete successful object}return null;}}

The name of the method is to be normalized.

This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1579909

Spring4 Learning Notes-SPRINGJDBC Basic operations (supplemental)

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.