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;
}
}
}
Java Code
- 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;
- }
- }
- }