The RowMapper in sping can encapsulate each row of data in the data into a user-defined class.
In database queries, if the type returned is a user-defined type (in fact, most of the classes we return in a database query are custom-defined), we need to wrap it if it is a Java-custom type, such as a string.
If Sping is combined with hibernate, it is basically not available, and most of them are used when spring is used alone.
The RowMapper interface can be implemented by establishing an internal class, and there is a Maprow method in the RowMapper, so the RowMapper interface must implement the Maprow method, and the wrapper of the custom class is implemented in the Maprow method.
Here is a simple example:
public class Testdao {
Private JdbcTemplate JT;
public void Setjt (JdbcTemplate JT) {
THIS.JT = JT;
}
Public list<tnpc> GetAll () {
String sql = "SELECT * from T_NPC";
Use
List List = Jt.query (sql, New Npcrowmapper ());
return list;
}
/**
* Define inner class implementation RowMapper interface
*/
public class Npcrowmapper implements rowmapper{
Implementing the Maprow Method
Public Object Maprow (ResultSet rs, int num) throws SQLException {
To encapsulate a class
TNPC NPC = new TNpc ();
Npc.setid (Rs.getlong ("id"));
Npc.setname (rs.getstring ("name"));
return NPC;
}
}
}
(RPM) RowMapper in spring