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;
}
}
}
分享到:
相关推荐
strut2+spring+springjdbctemplate做的简易登录系统
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
一个非常简单基于注解的Spring JdbcTemplate,供初学者参考用。
Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,...
Spring JDBCTemplate连接池jar包
Spring JdbcTemplate调用Oracle存储过程输出游标结果集实现增删改查
模仿spring jdbcTemplate的粗略实现,只有很小的参考价值,如果是java初学者可以使用这个封装好的工具进行数据库操作,只需要在db.properties里配置好driver,url等信息
使用Spring的JdbcTemplate实现分页功能
Spring框架JdbcTemplate类中查询方法介绍
NULL 博文链接:https://humlzy.iteye.com/blog/2382685
spring-jdbcTemplate实例工程
Spring JdbcTemplate的操作,包括接口,BaseDao,log4J配置文件,主要为oracle数据库操作,很多特殊方法只对oracle有效
SpringJdbcTemplate封装工具包,包括规范model格式接口,封装SpringJdbcTemplate,实现分页,自适应多种数据库
JdbcTemplate api 下载 Spring
1.Spring4前 spring-jdbc包是独立的,4以后spring-jdbc 就已经没有了
使用Spring的JdbcTemplate调用Oracle的存储过程
JdbcTemplate主要提供以下五类方法;JdbcTemplate类支持的回调类;并附例子
spring jdbctemplate组件的简单实例。可以直接运行该实例来学习spring的jdbctemplate。处于初学者,或开发互联网性能要求较高的比较有料。
SSH笔记-Spring JdbcTemplate,使用JdbcTemplate对数据库进行操作,使用具名参数和JDBC模板,简化操作
NULL 博文链接:https://rayfuxk.iteye.com/blog/2286519