Spring's jdbctemplate operations
1.Spring Frame One-stop framework for Java EE Three layer, each layer has a resolution technology on the DAO layer, using JdbcTemplate
2.Spring support for different persistence technologies Spring provides templates and callbacks for simple operations for a variety of supported persistence technologies. ORM Persistence Technology template class JDBC Org.springframework.jdbc.core.JdbcTemplate Hibernate5.0 Org.springframework.orm.hibernate5.HibernateTemplate IBatis (MyBatis) Org.springframework.orm.ibatis.SqlMapClientTemplate JPA Org.springframework.orm.jpa.JpaTemplate
JdbcTemplate to encapsulate JDBC
3.jdbcTemplate is similar to the use of dbutils in Apache, which is a crud operation on the database.
To pilot a package when using JdbcTemplate. Here must import SPRING-TX and jdbctemplate These two packages, integral, although SPRING-TX is about the transaction management package, but inside also has jdbctemplate to TX's dependence.
Download Address
Finally, don't forget to import the database-driven package
Download Address
JdbcTemplate relies on the connection pool (not the C3P0 connection pool used here) to get the database connection, so the connection pool must be constructed first.
Here are four actions: Add and delete and check
@Test public
Void Add () {
//because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first
Drivermanagerdatasource Drivermanagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source
jdbctemplate jdbctemplate=new jdbctemplate (drivermanagerdatasource);
Call the method implementation inside the JdbcTemplate object to manipulate
String sql= "insert into user values (?,?)";
int rows=jdbctemplate.update (SQL, "Firelang", "123456");
SYSTEM.OUT.PRINTLN (rows);
}
Note that the database URL in the above code is jdbc:mysql://localhost:3306/template, in fact, this can be written jdbc:mysql:///template modify
@Test public
Void Update () {
//because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first
Drivermanagerdatasource Drivermanagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source
jdbctemplate jdbctemplate=new jdbctemplate (drivermanagerdatasource);
Invokes the method implementation inside the JdbcTemplate object
String sql= "Update user set password=? where Name=?";
int rows=jdbctemplate.update (SQL, "1314520", "Firelang");
SYSTEM.OUT.PRINTLN (rows);
}
Delete
@Test public
Void Delete () {
//because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first
Drivermanagerdatasource Drivermanagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source
jdbctemplate jdbctemplate=new jdbctemplate (drivermanagerdatasource);
Invokes the method implementation inside the JdbcTemplate object
String sql= "Delete from user where name=?";
int rows=jdbctemplate.update (SQL, "Langshen");
SYSTEM.OUT.PRINTLN (rows);
}
Inquire
Using JdbcTemplate to implement query operations
JdbcTemplate implementation query, there is interface RowMapper
JdbcTemplate does not provide implementation classes for this interface, and different types of data need to be encapsulated by themselves
Query Single value:
@Test public
void Showsingletyepvalue () {
//because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first
Drivermanagerdatasource Drivermanagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source
jdbctemplate jdbctemplate=new jdbctemplate (drivermanagerdatasource);
Invokes the method implementation operation
String sql= "select COUNT (name) from user" inside the JdbcTemplate object;
int count=jdbctemplate.queryforobject (SQL, integer.class);
System.out.println (count);
}
Query object:
@Test public void Showsingleobject () {//Because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first Drivermanagerdatasource
Drivermanagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source JdbcTemplate jdbctemplate=new JdbcTemplate (Drivermanagerdatasource); Call the method implementation inside the JdbcTemplate object to manipulate String sql= "select * from user where name=?";
/The query here is a single object. User temp=jdbctemplate.queryforobject (SQL, new rowmapper<user> () {@Override Public User map
Row (ResultSet result, int num) throws SQLException {//here num represents the line number, such as I query out a few data, and NUM represents ResultSet inside the line number.
User User=new User (result.getstring ("name"), result.getstring ("password")); Return to useR
}, "Firelang");
SYSTEM.OUT.PRINTLN (temp); }
Query return list collection
@Test public void Showlist () {//Because JdbcTemplate is dependent on the database connection pool, the connection pool is constructed first Drivermanagerdatasource driverm
Anagerdatasource=new Drivermanagerdatasource ();
Drivermanagerdatasource.setdriverclassname ("Com.mysql.jdbc.Driver");
Drivermanagerdatasource.seturl ("Jdbc:mysql://localhost:3306/template");
Drivermanagerdatasource.setusername ("root");
Drivermanagerdatasource.setpassword ("");
Creates a JdbcTemplate object, sets the data source JdbcTemplate jdbctemplate=new JdbcTemplate (Drivermanagerdatasource); Invokes the method implementation within the JdbcTemplate object String sql= "select * from user";//Query All list<user> templist=jdbctemplate. Query (SQL, new rowmapper<user> () {@Override public User maprow (ResultSet result, int num) t
Hrows SQLException {//here num represents the line number, such as I query out a few data, and Num represents the line number in the resultset.
System.out.println ("Current number of lines:" +num); User User=new User (result.getstring ("name"), result.getstring ("password"));
return user;
}
});
System.out.println (templist); /* Output Result: Current number of rows: 0 Current line: 1 [user [Name=firelang, password=1314520], user [Name=langshen, password=123456]]