@Test public void testAddUser() { String newImei = "46022222222222"; int count = userDao.getUserCount(); logger.debug("user count is "+count); User user = new User(); user.setImei(newImei); user.setImsi(newImei); user.setPhoneName("Shanzhai"); logger.info("before add:"+user); user = userDao.addUser(user); assertNotSame(0, user.getUserId()); logger.info("after add:"+user); user = userDao.getUserbyID(1); assertEquals(count+1, userDao.getUserCount()); }
@Override public User addUser(final User user) { final String INSERT_SQL = "insert into t_user (imei, imsi, phone_name,createdate) values (?, ?, ?, now())"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement( Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "user_id" }); ps.setString(1, user.getImei()); ps.setString(2, user.getImsi()); ps.setString(3, user.getPhoneName()); return ps; } }, keyHolder); user.setUserId(keyHolder.getKey().intValue()); return user; }
UserDaoJDBC implements UserDao { @Override public User addUser(final User user) { final String INSERT_SQL = "insert into t_user (imei, imsi, phone_name,createdate) values (?, ?, ?, now())"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement( Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "user_id" }); ps.setString(1, user.getImei()); ps.setString(2, user.getImsi()); ps.setString(3, user.getPhoneName()); return ps; } }, keyHolder); user.setUserId(keyHolder.getKey().intValue()); return user; } }
UserDaoJDBC implements UserDao { @Override public User addUser(final User user) { final String INSERT_SQL = "insert into t_user (imei, imsi, phone_name,createdate) values (?, ?, ?, now())"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement( Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "user_id" }); ps.setString(1, user.getImei()); ps.setString(2, user.getImsi()); ps.setString(3, user.getPhoneName()); return ps; } }, keyHolder); user.setUserId(keyHolder.getKey().intValue()); return user; } @Autowired  UserDaoJDBC(DataSource datasource); }
UserDaoJDBC implements UserDao { @Override public User addUser(final User user) { final String INSERT_SQL = "insert into t_user (imei, imsi, phone_name,createdate) values (?, ?, ?, now())"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement( Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "user_id" }); ps.setString(1, user.getImei()); ps.setString(2, user.getImsi()); ps.setString(3, user.getPhoneName()); return ps; } }, keyHolder); user.setUserId(keyHolder.getKey().intValue()); return user; } @Autowired  UserDaoJDBC(DataSource datasource); @Override int getUserCount(); @Override User getUserbyID(int id); @Override List<User> getUsers(); @Override User addUser(final User user); @Override int updateUser(User user); }
UserDaoJDBC implements UserDao { @Override public User addUser(final User user) { final String INSERT_SQL = "insert into t_user (imei, imsi, phone_name,createdate) values (?, ?, ?, now())"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement( Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(INSERT_SQL, new String[] { "user_id" }); ps.setString(1, user.getImei()); ps.setString(2, user.getImsi()); ps.setString(3, user.getPhoneName()); return ps; } }, keyHolder); user.setUserId(keyHolder.getKey().intValue()); return user; } @Autowired  UserDaoJDBC(DataSource datasource); @Override int getUserCount(); @Override User getUserbyID(int id); @Override List<User> getUsers(); @Override User addUser(final User user); @Override int updateUser(User user);  }