Spring JdbcTemplate 查询方法中的RowMapper实现汇总
2015-11-24 14:54
323 查看
http://blog.chinaunix.net/uid-10018502-id-2972078.html
实现一、在内部建立内联类实现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 findAll() {
String sql = "SELECT * FROM items";
return jdbcTemplate.query(sql,new ItemMapper());
}
public List 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;
}
}
}
实现一、在内部建立内联类实现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 findAll() {
String sql = "SELECT * FROM items";
return jdbcTemplate.query(sql,new ItemMapper());
}
public List 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点滴
- iOS中Block介绍 基础
- gradle编译出错:Execution failed for task ':app:compileTestDebugJava'
- iOS开发笔记- 格式化银行卡号
- Android的一段常用动画效果代码(如何让点击的图片控件加速飞入到指定位置)
- JdbcTemplate查询数据 三种callback之间的区别(ResultSetExtractor,RowMapperRowCallbackHandler)
- [转]Android App整体架构设计的思考
- iOS开发笔记-根据银行卡号判断银行名称
- ios开发——旋屏控制
- iOS GCD中队列组的使用--下载并且为图片添加水印
- 使用COCOS2D-JS开发你的第一个游戏
- 如何解决word中出现的问题事件APPCRASH_百度知道
- 粗仿微信-个人主页
- 《swift2.0 官方教程中文版》 第2章-06函数
- android删除文件出错
- 自动生成Android不同分辨率下的图片
- 《swift2.0 官方教程中文版》 第2章-05控制流
- 《swift2.0 官方教程中文版》 第2章-04集合类型
- @RequestMapping使用
- 《swift2.0 官方教程中文版》 第2章-03字符串和字符