Summary of RowMapper implementation in Spring JdbcTemplate Query method

Source: Internet
Author: User

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
  1. Implement the RowMapper interface by building an inline class internally
  2. Package Hysteria.contact.dao.impl;
  3. Import Java.sql.ResultSet;
  4. Import java.sql.SQLException;
  5. Import Java.sql.Types;
  6. Import java.util.List;
  7. Import Org.springframework.jdbc.core.JdbcTemplate;
  8. Import Org.springframework.jdbc.core.RowMapper;
  9. Import Hysteria.contact.dao.ItemDAO;
  10. Import Hysteria.contact.domain.Item;
  11. Public class Itemdaoimpl implements Itemdao {
  12. private JdbcTemplate JdbcTemplate;
  13. public void Setjdbctemplate (JdbcTemplate jdbctemplate) {
  14. this.jdbctemplate = JdbcTemplate;
  15. }
  16. Public Item Insert (item item) {
  17. String sql = "INSERT into items (user_id,name,phone,email) VALUES (?,?,?,?)";
  18. object[] params = new Object[]{item.getuserid (), Item.getname (), Item.getphone (), Item.getemail ()};
  19. int[] types = new Int[]{types.integer,types.varchar,types.char,types.varchar};
  20. Jdbctemplate.update (sql,params,types);
  21. return item;
  22. }
  23. Public Item Update (item item) {
  24. String sql = "UPDATE items SET name =?, phone =?, email =?" WHERE id =? ";
  25. object[] params = new object[] {item.getname (), Item.getphone (), Item.getemail (), Item.getid ()};
  26. int[] types = new int[] {Types.varchar,types.char,types.varchar,types.varchar,types.integer};
  27. Jdbctemplate.update (sql,params,types);
  28. return item;
  29. }
  30. public void Delete (item item) {
  31. String sql = "DELETE from items WHERE id =?";
  32. object[] params = new object[] {item.getid ()};
  33. int[] types = new Int[]{types.integer};
  34. Jdbctemplate.update (sql,params,types);
  35. }
  36. Public Item FindByID (int id) {
  37. String sql = "SELECT * from items WHERE id =?";
  38. object[] params = new object[] {ID};
  39. int[] types = new int[] {Types.integer};
  40. List items = jdbctemplate.query (sql,params,types,new Itemmapper ());
  41. if (Items.isempty ()) {
  42. return null;
  43. }
  44. return (Item) Items.get (0);
  45. }
  46. Public list<item> FindAll () {
  47. String sql = "SELECT * from Items";
  48. return jdbctemplate.query (SQL,new Itemmapper ());
  49. }
  50. Public list<item> findallbyuser (int user_id) {
  51. String sql = "SELECT * from items WHERE user_id =?";
  52. object[] params = new object[]{user_id};
  53. int[] types = new Int[]{types.integer};
  54. List items = jdbctemplate.query (sql,params,types,new Itemmapper ());
  55. return items;
  56. }
  57. protected class Itemmapper implements RowMapper {
  58. Public Object Maprow (ResultSet rs, int rowNum) throws SQLException {
  59. Item item = new Item ();
  60. Item.setid (Rs.getint ("id"));
  61. Item.setuserid (Rs.getint ("user_id"));
  62. Item.setname (rs.getstring ("name"));
  63. Item.setphone (rs.getstring ("Phone"));
  64. Item.setemail (rs.getstring ("email"));
  65. return item;
  66. }
  67. }
  68. }

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.