学习记录--spring中的RowMapper
来源:互联网 发布:循环遍历json对象 编辑:程序博客网 时间:2024/05/17 22:09
Sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类.
我们在数据库查询中,如果返回的类型是用户自定义的类型(其实我们在数据库查询中大部分返回的都是自定义的类)则需要包装,如果是Java自定义的类型,如:String则不需要。如果sping与hibernate 相结合了,基本上是用不到,大多数都是在spring单独使用时用到。可以通过建立内部类实现RowMapper接口,RowMapper中有一个mapRow方法,所以实现RowMapper接口一定要实现mapRow方法,而对自定义类的包装就在mapRow方法中实现。
public class ItemDAOImpl { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public Item insert(Item item) { String sql = "INSERT INTO items(user_id,name,phone,email) VALUES(?,?,?,?)"; Object[] params = new Object[] { item.getUserId(), item.getName(), item.getPhone(), item.getEmail() }; int[] types = new int[] { Types.INTEGER, Types.VARCHAR, Types.CHAR, Types.VARCHAR }; jdbcTemplate.update(sql, params, types); return item; } public Item update(Item item) { String sql = "UPDATE items SET name = ?, phone = ?, email = ? WHERE id = ?"; Object[] params = new Object[] { item.getName(), item.getPhone(), item.getEmail(), item.getId() }; int[] types = new int[] { Types.VARCHAR, Types.CHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER }; jdbcTemplate.update(sql, params, types); return item; } public void delete(Item item) { String sql = "DELETE FROM items WHERE id = ?"; Object[] params = new Object[] { item.getId() }; int[] types = new int[] { Types.INTEGER }; jdbcTemplate.update(sql, params, types); } public Item findById(int id) { String sql = "SELECT * FROM items WHERE id = ?"; Object[] params = new Object[] { id }; int[] types = new int[] { Types.INTEGER }; List items = jdbcTemplate.query(sql, params, types, new ItemMapper()); if (items.isEmpty()) { return null; } return (Item) items.get(0); } public List<Item> findAll() { String sql = "SELECT * FROM items"; return jdbcTemplate.query(sql, new ItemMapper()); } public List<Item> findAllByUser(int user_id) { String sql = "SELECT * FROM items WHERE user_id = ?"; Object[] params = new Object[] { user_id }; int[] types = new int[] { Types.INTEGER }; List items = jdbcTemplate.query(sql, params, types, new ItemMapper()); return items; } protected class ItemMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Item item = new Item(); item.setId(rs.getInt("id")); item.setUserId(rs.getInt("user_id")); item.setName(rs.getString("name")); item.setPhone(rs.getString("phone")); item.setEmail(rs.getString("email")); return item; } }}
0 0
- 学习记录--spring中的RowMapper
- 使用spring中的Rowmapper
- Spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper
- Spring 中的 RowMapper
- spring 中的 RowMapper
- spring 中的 RowMapper 类
- Spring-RowMapper
- Spring-RowMapper
- Spring-RowMapper
- Spring--RowMapper
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
- Android 手机上获取物理唯一标识码
- OSChina 网站的 web.xml 文件
- 第十六周项目2-用指针玩字符串2
- Django 定义和使用模型
- OpenGL批次渲染
- 学习记录--spring中的RowMapper
- UpdatePanel控件实现无刷新级联
- 关于域名的问题
- 第16周OJ平台
- 详解UICoordinateSpace和UIScreen在iOS 8上的坐标问题
- 五十周年纪念钞
- spring3.0中aspectjweaver.jar在哪?
- 【Android】引用第三方库引发“Android: NoClassDefFoundError”错误的解决方法
- devExpress GridView删除行的几种方式