RowMapper can encapsulate each row in the data into a user-defined class, in a database query, if the type returned is a user-defined type, it needs to be wrapped, if it is a Java custom type, such as: string is not required, Spring's newest class simplejdbctemplate is much simpler to use.
Here's an example of how to use Rowmapp to download code from the Web, and it's OK to explain the problem. In the use of the process we can make the inner class Pojo external class, as long as the implementation of the RowMapper interface. If the user wants to define ApplicationContext, be cautious. After all, the implementation of RowMapper interface needs to add a Maprow method to a class, so that the class can withstand more functions, not conducive to the analysis system. Please master a lot of advice.
Implement the RowMapper interface by building an inline class internally
Package Hysteria.contact.dao.impl;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.sql.Types;
Import java.util.List;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.jdbc.core.RowMapper;
Import Hysteria.contact.dao.ItemDAO;
Import Hysteria.contact.domain.Item;
public class Itemdaoimpl implements Itemdao {
Private JdbcTemplate JdbcTemplate;
public void Setjdbctemplate (JdbcTemplate jdbctemplate) {
This.jdbctemplate = JdbcTemplate;
}
Public Item Insert (item item) {
String sql = "INSERT into items (user_id,name,phone,email) VALUES (?,?,?,?)";
object[] params = new Object[]{item.getuserid (), Item.getname (), Item.getphone (), Item.getemail ()};
int[] types = new Int[]{types.integer,types.varchar,types.char,types.varchar};
Jdbctemplate.update (sql,params,types);
return item;
}
Public Item update (item item) {
String sql = "UPDATE items SET name =?, phone =?, email =?" WHERE id =? ";
object[] params = new object[] {item.getname (), Item.getphone (), Item.getemail (), Item.getid ()};
int[] types = new int[] {Types.varchar,types.char,types.varchar,types.varchar,types.integer};
Jdbctemplate.update (sql,params,types);
return item;
}
public void Delete (item item) {
String sql = "DELETE from items WHERE id =?";
object[] params = new object[] {item.getid ()};
int[] types = new Int[]{types.integer};
Jdbctemplate.update (sql,params,types);
}
Public Item FindByID (int id) {
String sql = "SELECT * from items WHERE id =?";
object[] params = new object[] {ID};
int[] types = new int[] {Types.integer};
List items = jdbctemplate.query (sql,params,types,new itemmapper ());
if (Items.isempty ()) {
return null;
}
Return (Item) items.get (0);
}
Public list<item> FindAll () {
String sql = "SELECT * from Items";
Return Jdbctemplate.query (Sql,new itemmapper ());
}
Public list<item> findallbyuser (int user_id) {
String sql = "SELECT * from items WHERE user_id =?";
object[] params = new object[]{user_id};
int[] types = new Int[]{types.integer};
List items = jdbctemplate.query (sql,params,types,new itemmapper ());
return items;
}
Protected class Itemmapper implements RowMapper {
Public Object Maprow (ResultSet rs, int rowNum) throws SQLException {
Item item = new Item ();
Item.setid (Rs.getint ("id"));
Item.setuserid (Rs.getint ("user_id"));
Item.setname (rs.getstring ("name"));
Item.setphone (rs.getstring ("Phone"));
Item.setemail (rs.getstring ("email"));
return item;
}
}
}
<span style= "FONT-SIZE:18PX;" >jdbctemplate, Namedparameterjdbctemplate, simplejdbctemplate the difference one, JdbcTemplate first set the data source in the configuration file <bean id= " DataSource "class=" Org.springframework.jdbc.datasource.DriverManagerDataSource "destroy-method=" Close ">< Property Name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver" ></property><property name= " URL "value=" Jdbc:oracle:thin: @localhost: 1521:orcl "></property><property name=" username "value=" Scott " ></property><property name= "password" value= "123" ></property></bean> Then use the configuration JdbcTemplate I mentioned in another blog to choose one of the jdbctemplate configurations, and I'll choose one of the ways to configure the following: <bean id= "jdbctemplate" class= "org. Springframework.jdbc.core.JdbcTemplate "> <property name=" dataSource "ref=" DataSource "/> </bean> to be able to use the template, the type needs to be a property of the DAO implementation class, defined as follows: public class Userdaoimpl implements userdao{private JdbcTemplate jdb ctemplatepublic void Setjdbctemplate (JdbcTemplate jdbctemplate) {this.jdbctemplate = JdbcTemplate; The DAO implementation class is configured as follows: <bean id= "Userdao" class= "Com.spring.jdbcDAOImpl.UserDAOImpl" ><property name= "JdbcTemplate "Ref=" JdbcTemplate "></property></bean> when configured as above, you can use templates in the DAO implementation class, Namedparameterjdbctemplate use The only difference from JdbcTemplate is that when assigning a question mark, it is by name. The configuration of the namedparameterjdbctemplate is similar to the configuration of the above JdbcTemplate in that the SQL statement is not used? To the most parameter substitution. Instead, use: To add variable names as parameter substitutes. The variable name is then bound to the actual value via map and passed to the Update method. The biggest benefit of using this approach is that if you have more parameters, and the parameter position or order may change, it is very convenient to use Namedparameterjdbctemplate! Here is the example I looked up from the Internet: private static final String Motorist_insert = "INSERT into motorist (ID, email, password," + "FirstName, LastName) VALUES (NULL,: Email,:p assword,: firstName,: LastName)"; public void savemotorist (motorist motorist) {MAP parameters = new HashMap (); Parameters.put ("Email", motorist.getemail ()); Parameters.put ("Password", Motorist.getpassword ()); Parameters.put ("FirstName", MotoriSt.getfirstname ()); Parameters.put ("LastName", Motorist.getlastname ()); Jdbctemplate.update (motorist_insert, parameters); Simplejdbctemplate uses Simplejdbctemplate to add support for JAVA5 features, such as variable parameters, auto-unpacking packets, generics, and more. The following is the finduserbyidsimple (int id) public User finduserbyidsimple (int id) {String sql= "select Id,us implemented with Simplejdbctemplate Er_name,password from TD where Id=? "; Parameterizedrowmapper mapper=new Parameterizedrowmapper () {@Overridepublic Object Maprow (ResultSet rs, int rowNum) Throws SQLException {User U=new User () U.setid (Rs.getint ("id")); U.setpassword (rs.getstring ("password")); U.setuser_ Name (rs.getstring ("user_name")); return u;}};/ /Here's the third parameter arguments to bind to the Queryreturn this.simpleJdbcTemplate.queryForObject (sql,mapper, id);} The biggest difference between Parameterizedrowmapper and RowMapper here is that the Parameterizedrowmapper support for generics here is different from using the Query method used in JdbcTemplate. First: The Query method in JdbcTemplate uses the object array to pass the arguments, whereas the query method in Simplejdbctemplate uses a mutable parameter, because it is a mutable parameter, so you need to place the mutable parameter in the last part of the parameter list. Second: Using the automatic encapsulation mechanism to pass ID data. Third: When working with result sets and object mappings, the Parameterizedrowmapper type is used instead of the RowMapper type, the main difference being that the Parameteri-zedrowmapper type supports generics. The Update method is also called, but the Update method uses a mutable parameter so that the object array is not required for data encapsulation, and all parameters that require a question mark are passed directly as the update parameter. The same is in index order! Iv. using the type of JDBC support class in spring I haven't done it yet, and the following is written by someone else. With the above implementations, JDBC Access can be done using a variety of different JDBC template classes, but if there are many definitions of DAO implementation classes, we need to define a lot of duplicated code parts, such as our You need to define the JdbcTemplate property in the DAO implementation class and define its get, set method. In addition, it needs to be declared in the configuration file. And so on These parts are duplicated. Spring provides a simplified implementation that defines the Jdbcdaosupport parent class (for the implementation of the JdbcTemplate approach), where duplicate code is completed, and the DAO implementation class that we define only needs to inherit from it, the relationship such as: through this implementation, The definition of a DAO implementation class can be simplified as follows: public class Jdbcrantdao extends Jdbcdaosupport implements Rantdao {public void savemotorist (M Otorist motorist) {getjdbctemplate (). Update (Motorist_insert, new object[] {motorist.getemail (), MO Torist.getpassword (), Motorist.getfirstname (), Motorist.getlastname ()}); There is no correlation definition for any of the JdbcTemplate properties in the middle. The JdbcTemplate property is obtained by calling the Getjdbctemplate () method. In addition, you need to define the following in the configuration file: <bean id= "Rantdao" class= "Com.roadrantz.dao.Jdbc. Jdbcrantdao "> <property name=" jdbctemplate "ref=" JdbcTemplate "/> </bean> from the definition can be seen, and do not use the support class are defined in the same way. But if our DAO implementation class inherits the Jdbcdaosupport parent class. Then its configuration can omit the definition of jdbctemplate content, directly do the following configuration: <bean id= "Rantdao" class= "Com.roadrantz.dao.jdbc.JdbcRantDao" > <property name= "DataSource" ref= "DataSource"/> </bean> omitted JdbcTemplate definition and declaration process! In the above introduction, Spring provides three different template classes, and if we want to use namedparameterjdbctemplate or simplejdbctemplate, we need to inherit different types of template support, namely: Namedparameterjdbcdaosupport and Simplejdbcdaosupport </span>
Spring JdbcTemplate RowMapper binding arbitrary objects