jdbcTemplate

时间:2017-07-16 05:54:53
【文件属性】:

文件名称:jdbcTemplate

文件大小:18.89MB

文件格式:ZIP

更新时间:2017-07-16 05:54:53

jdbcTemplate

package com.org.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowCallbackHandler; import org.springframework.jdbc.support.GeneratedKeyHolder; import org.springframework.stereotype.Repository; import com.org.JdbcTempBaseDao; import com.org.dao.IUserDao; import com.org.model.User; @Repository @SuppressWarnings("all") public class UserDaoImpl extends JdbcTempBaseDao implements IUserDao { @Override public List getUserList() { String sql="select * from user "; final List list= new ArrayList(); jdbcTemplate.query(sql, new RowCallbackHandler(){ @Override public void processRow(ResultSet rs) throws SQLException { User u=new User(); u.setId(rs.getInt("id")); u.setUsername(rs.getString("username")); u.setPassword(rs.getString("password")); u.setCreateDate(rs.getString("createDate")); u.setModifyDate(rs.getString("modifyDate")); u.setType(rs.getString("type")); list.add(u); } }); return list; } @Override public List getUserLists(Map map) { return null; } @Override public Integer getUserCount(Map map) { String sql = "select count(1) from User where id=? "; return getJdbcTemplate().queryForObject(sql, Integer.class,map); } @Override public User getUserById(Integer primaryKeyId) { String sql = "select id,username, password, createDate, modifyDate,type from User where id=?"; List userList = getJdbcTemplate().query(sql, new BeanPropertyRowMapper(User.class), primaryKeyId); if(userList.size() == 0) { return null; } return userList.get(0); } @Override public void delUserById(Integer primaryKeyId) { String sql = "delete from user where id=?"; getJdbcTemplate().update(sql, primaryKeyId); } @Override public User addUser(final User entity) { final String sql = "insert into User(username, password, createDate, modifyDate,type) values(?,?,?,?,?)"; GeneratedKeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement psst = connection.prepareStatement(sql, new String[]{"id"}); psst.setString(1, entity.getUsername()); psst.setString(2, entity.getPassword()); psst.setString(3, entity.getCreateDate()); psst.setString(4, entity.getModifyDate()); psst.setString(5, entity.getType()); return psst; } }, keyHolder); entity.setId(keyHolder.getKey().intValue()); return entity; } @Override public void editUser(User entity) { String sql="update user set username=?,password=?"; jdbcTemplate.update(sql, User.class,entity); } }


网友评论

  • 亲测可用,支持支持!
  • jdbc模板的包都有,挺全的,感谢!
  • 非常好!不错!
  • jdbc模板的包都有,挺全的,感谢!
  • 非常好,谢谢!
  • 很好的资料,很齐全,谢谢
  • 帮了我好大一个忙,可惜少了servlet包。不过相当棒了
  • 谢谢,帮了我好大一个忙

相关文章