Sunday, February 4, 2018

SpringJdbc Insert

SimpleJdbcInsert allows you to working with insert operations with minimal or no configuration. Here you don’t need to provide the SQL query, rather you will pass the table name and the data map containing key as column name and value as Object type.

When you call execute method by passing the data map of values, it would automatically query the metadata of the table into his it has to perform insert operation and builds a query and inserts the data.

StudentDao.java

public class SimpeStudentInsertDao;
          private SimpleJdbcInsert  simpleJdbcInsert;

          public SimpleStudentInsertDao(DataSource dataSource) {
                 this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource);
          }

          public void insert(StudentBO student) {
                 simpleJdbcInsert.setTableName(STUDENT);
                 SqlParameterSource paramSource = new BeanPropertySqlParameterSource(student);
                 simpleJdbcInsert.execute(paramSource);
          }

          public void insert(int id, String name) {
                 simpleJdbcInsert.setTableName(STUDENT);
                 Map<String, Object> data = new HashMap<String, Object>();
                 data.put(STUDENT_ID, student.getStudentId());
                 data.put(NAME, student.getStudentName());
                 simpleJdbcInsert.execute(data);
          }
   }

Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: