湘潭项目使用Spring+CXF编写服务器端,JDBC操作全部由Spring完成,下面记录其中一个典型的实例。
public class Userinfo_lvDaoImpl implements Userinfo_lvDao { //通过注解注入获得jdbcTemplate对象 @Autowired private JdbcTemplate jdbcTemplate; /** * 根据name和password来查询用户。返回查询到的Userinfo_lv对象 */ @Override public Userinfo_lv userinfo_lv_get(String name, String password) { String sql = "select * from Userinfo_lv where name = ? and password = ?"; RowMapper<Userinfo_lv> rowMapper = new BeanPropertyRowMapper<Userinfo_lv>( Userinfo_lv.class); try { return jdbcTemplate.queryForObject(sql, rowMapper, name, password); } catch (Exception e) { return null; } } /** * 添加用户。返回被成功添加的Userinfo_lv对象 */ @Override public Userinfo_lv user_add(String name, String password, int role, int groupid) { String sql = "insert into Userinfo_lv (name, password, role ) values (?, ?, ?)"; int update = jdbcTemplate.update(sql, name, password, role); if (update > 0) {// 添加成功 return userinfo_lv_get(name, password);// 返回添加成功的对象 } return null; } /** * 通过id查询用户。返回查询到的Userinfo_lv对象。 */ @Override public Userinfo_lv getUserinfo_lv(int id) { String sql = "select * from Userinfo_lv where id = ?"; RowMapper<Userinfo_lv> rowMapper = new BeanPropertyRowMapper<Userinfo_lv>( Userinfo_lv.class); try { return jdbcTemplate.queryForObject(sql, rowMapper, id); } catch (Exception e) { return null; } } /** * 通过用户名查询用户。返回查询到的Userinfo_lv对象。 */ @Override public Userinfo_lv getUserinfo_lv(String name) { String sql = "select * from Userinfo_lv where name = ?"; RowMapper<Userinfo_lv> rowMapper = new BeanPropertyRowMapper<Userinfo_lv>( Userinfo_lv.class); try { return jdbcTemplate.queryForObject(sql, rowMapper, name); } catch (Exception e) { return null; } } /** * 通过role查询用户。 */ @Override public List<Userinfo_lv> getUserinfo_lvsByRole(int role) { String sql = "select * from Userinfo_lv where role = ?"; List<Userinfo_lv> Userinfo_lvs = jdbcTemplate.query(sql, new RowMapperResultSetExtractor(new Userinfo_lvRowMapper()), role); return Userinfo_lvs; } /** * 通过groupid查询用户 */ @Override public List<Userinfo_lv> getUserinfo_lvsByGroupid(int groupid) { String sql = "select * from Userinfo_lv where groupid = ?"; List<Userinfo_lv> Userinfo_lvs = jdbcTemplate.query(sql, new RowMapperResultSetExtractor(new Userinfo_lvRowMapper()), groupid); return Userinfo_lvs; } private class Userinfo_lvRowMapper implements RowMapper{ @Override public Object mapRow(ResultSet rs, int index) throws SQLException { Userinfo_lv userinfo_lv = new Userinfo_lv(); userinfo_lv.setGroupid(rs.getInt("groupid")); userinfo_lv.setId(rs.getInt("id")); userinfo_lv.setName(rs.getString("name")); userinfo_lv.setPassword(rs.getString("password")); userinfo_lv.setRole(rs.getInt("role")); return userinfo_lv; } } /** * 根据id删除用户 * 若删除成功,返回被删除的Userinfo_lv对象。否则返回null */ @Override public Userinfo_lv user_delete(int id) { String sql = "delete from Userinfo_lv where id = ?"; int update = jdbcTemplate.update(sql, id); if (update > 0) {//删除成功 return getUserinfo_lv(id);// 返回删除成功的对象 } return null; } }
方法的命名待规范。
时间: 2024-10-02 17:22:44