Spring--RowMapper
2016-07-11 10:34
423 查看
RowMapper可以将数据中的每一行封装成用户定义的类,在数据库查询中,如果返回的类型是用户自定义的类型则需要包装,如果是Java自定义的类型,如:String则不需要,Spring最新的类SimpleJdbcTemplate使用更加简单了。
下面这个实例说明了如何使用RowMapp,从网上下载的代码,还行能说明问题。在使用过程中我们可以将内部类做成POJO的外部类,只要实现RowMapper接口即可。如果用户想让ApplicationContext进行定义还是要谨慎。毕竟实现RowMapper接口需要给一个类增加一个mapRow方法,让类承受的功能较多,不利于分析系统
实现一、在内部建立内联类实现RowMapper接口
package hysteria.contact.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import hysteria.contact.dao.ItemDAO;
import hysteria.contact.domain.Item;
public class ItemDAOImpl implements ItemDAO {
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;
}
}
}
下面这个实例说明了如何使用RowMapp,从网上下载的代码,还行能说明问题。在使用过程中我们可以将内部类做成POJO的外部类,只要实现RowMapper接口即可。如果用户想让ApplicationContext进行定义还是要谨慎。毕竟实现RowMapper接口需要给一个类增加一个mapRow方法,让类承受的功能较多,不利于分析系统
实现一、在内部建立内联类实现RowMapper接口
package hysteria.contact.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import hysteria.contact.dao.ItemDAO;
import hysteria.contact.domain.Item;
public class ItemDAOImpl implements ItemDAO {
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;
}
}
}
相关文章推荐
- Android学习笔记(二)—— Toast
- 【Android】切换Activity时使用Bundle封装Map对象
- mybatis 的mapper映射文件返回一个map
- cookie解决微信不能存储localStorage的问题
- 微信于微博的区别
- ios截屏 裁剪区域
- android ndk之常用命令
- FingerGestures手势插件的使用
- 微信与QQ的区别
- Android查询扫描SD卡里指定后缀名称的文件
- 【Android基础知识】FragmentPagerAdapter和FragmentStatePagerAdapter
- 应用跳转到AppStore
- OC常用的C数学运算函数
- Android图片上传(看了别人的博客,图片无法上传,文章解决办法)
- android笔记之自定义组件
- android 多线程断点续传下载 一
- Android中SurfaceView的使用详解
- 成长小计Swift引入三方库报错问题
- win android环境
- Android开发之WebService介绍