Jdbctemplate details 2

Source: Internet
Author: User
1. Because of the previous jdbctemplateProgramYou need to write a bunch of rowmapper er ing files, which looks a little bloated. It is best to automatically match according to the name of the pojo class and field, so SimplejdbctemplateYou can use attributes in pojo to automatically assign values. The syntax starts. Public Class Userdaospringimpl Implements Userdao {
Private Simplejdbctemplate = New Simplejdbctemplate (
Jdbcutils. getdatasource ());

Public VoidAdduser (User user ){
String SQL ="Insert into user (name, money, birthday) values (: name,: Money,: birthday )";
Sqlparametersource Param =NewBeanpropertysqlparametersource (User );
Keyholder =NewGeneratedkeyholder ();
This. Simplejdbctemplate. getnamedparameterjdbcoperations (). Update (SQL,
Param, keyholder );
User. setid (keyholder. getkey (). intvalue ());
}

Public void Delete (User user) {
string SQL = " delete from user where id =? ";
This . simplejdbctemplate. update (SQL, user. GETID ();
}

Public User finduser (string loginname, string password) {
string SQL = "select ID, name, money, birthday from user where name =? ";
return This . simplejdbctemplate. queryforobject (SQL,
parameterizedbeanpropertyrowmapper. newinstance (user. class ),
loginname);
}

Public User getuser ( int userid) {
string SQL = " select ID, name, money, birthday from user where id =? ";
return This . simplejdbctemplate. queryforobject (SQL,
parameterizedbeanpropertyrowmapper. newinstance (user. class ),
userid),
}

Public VoidUpdate (User user ){
String SQL ="Update user set name = ?, Birthday = ?, Money =? Where id =? ";
This. Simplejdbctemplate. Update (SQL, user. getname (), user. getbirthday (),
User. getmoney (), user. GETID ());

SQL ="Update user set name =: name, Birthday =: Birthday, money =: money where id =: ID";
This. Simplejdbctemplate. Update (SQL,NewBeanpropertysqlparametersource (
User ));
}

}

The JdbcutilsCodeAs follows: Public Final Class Jdbcutils {
Private Static String url = "JDBC: mysql: // Localhost: 3306/jdbc ";
Private Static String user = "Root" ;
Private Static String Password = "";
Private Static Datasource mydatasource = Null ;

PrivateJdbcutils (){
}

static {
try {< br> class. forname ( "com. mySQL. JDBC. driver ");
// mydatasource = new myperformance2 ();
properties prop = New Properties ();
// prop. setproperty ("driverclassname", "com. mySQL. JDBC. driver ");
// prop. setproperty ("user", "user");

Inputstream is = jdbcutils.Class. Getclassloader ()
. Getresourceasstream ("Dbcpconfig. properties");
Prop. Load (is );
Mydatasource = basicdatasourcefactory. createdatasource (PROP );
}Catch(Exception e ){
Throw NewExceptionininitializererror (E );
}
}

Public StaticDatasource getdatasource (){
ReturnMydatasource;
}

Public StaticConnection getconnection ()ThrowsSqlexception {
// Return drivermanager. getconnection (URL, user, password );
ReturnMydatasource. getconnection ();
}

Public Static Void Free (resultset RS, statement St, connection conn ){
Try {
If (RS! =Null )
Rs. Close ();
} Catch (Sqlexception e ){
E. printstacktrace ();
} Finally {
Try {
If (St! = Null )
St. Close ();
} Catch (Sqlexception e ){
E. printstacktrace ();
} Finally {
If (Conn! = Null )
Try {
Conn. Close ();
// Mydatasource. Free (conn );
} Catch (Exception e ){
E. printstacktrace ();
}
}
}
}
}2. classes that complete the same ING also include namedparameterjdbctemplate,It splits the placeholder '? . However, such SQL statements cannot be directly executed in the database and must be converted using spring. Public Class Namedjdbctemplate {
Static Namedparameterjdbctemplate named = New Namedparameterjdbctemplate (
Jdbcutils. getdatasource ());

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs) {
User user = New User ();
User. setmoney (10);
User. setid (2);
system. out. println (finduser1 (User);
}

Static VoidAdduser (User user ){
String SQL ="Insert into user (name, birthday, money) values (: name,: Birthday,: Money )";
Sqlparametersource PS =NewBeanpropertysqlparametersource (User );
Keyholder =NewGeneratedkeyholder ();
Named. Update (SQL, PS, keyholder );
IntId = keyholder. getkey (). intvalue ();
User. setid (ID );

Map map = keyholder. getkeys ();
}

Static User finduser (User user ){
String SQL = "Select ID, name, money, birthday from user"
+ "Where money>: MAnd id <: ID" ;
Map Params = New Hashmap ();
// Params. Put ("N", user. getname ());
Params. Put ( "M" , User. getmoney ());
Params. Put ("ID" , User. GETID ());
Object u = named. queryforobject (SQL, Params, New Beanpropertyrowmapper (
User. Class ));
Return (User) U;
}

StaticUser finduser1 (User user ){
String SQL ="Select ID, name, money, birthday from user"
+"Where money>: Money and ID <: ID";
Sqlparametersource PS =New Beanpropertysqlparametersource(User );
Object u = named. queryforobject (SQL, PS,NewBeanpropertyrowmapper (user.Class));
Return(User) U;
}

}

[note] 1. beanpropertyrowmapper completes the ing between objects and database fields. You can no longer use rowmapper to match them one by one. If rowmapper is used only once, you can directly use internal class , instead of writing a class. 2. keyholder stores the primary keys for database operations, after obtaining the primary key of the operation, you can easily perform other actions on the Operation Records. In short: the use of reflection technology, reduce unnecessary rowmapper, improve efficiency from: http://tianya23.blog.51cto.com/1081650/375823

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.