通常情况下,普通DAO的实现大概是这样的:
1、实体类:
public class Emp {
private Integer id;
private String name;
private String sex;
private Float sal;
//getter and setter methods...
}
2、DAO接口:
public interface EmpDAO {
public int insert(Emp emp)throws SQLException;
public int delete(Emp emp)throws SQLException;
public int update(Emp emp)throws SQLException;
public Emp findById(Emp emp)throws SQLException;
public String findName(Emp emp)throws SQLException;
}
3、DAO实现类:
public class EmpDAOImpl implements EmpDAO {
public int delete(Emp emp) throws SQLException {
String sql = "delete from emp where id=?";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getId());
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(null, ps, conn);
}
}
public Emp findById(Emp emp) throws SQLException {
String sql = "select id,name,sal,sex from emp where id=?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Emp currEmp = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getId());
rs = ps.executeQuery();
if (rs.next()) {
currEmp = new Emp();
currEmp.setId(rs.getInt("id"));
currEmp.setName(rs.getString("name"));
currEmp.setSal(rs.getFloat("sal"));
currEmp.setSex(rs.getString("sex"));
}
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(rs, ps, conn);
}
return currEmp;
}
public String findName(Emp emp) throws SQLException {
String sql = "select name from emp where id=?";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String name = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, emp.getId());
rs = ps.executeQuery();
if (rs.next()) {
name = rs.getString("name");
}
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(rs, ps, conn);
}
return name;
}
public int insert(Emp emp) throws SQLException {
String sql = "insert into emp(name,sal,sex) values(?,?,?)";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, emp.getName());
ps.setFloat(2, emp.getSal());
ps.setString(3, emp.getSex());
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(null, ps, conn);
}
}
public int update(Emp emp) throws SQLException {
String sql = "update emp set name=?,sal=?,sex=? where id=?";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, emp.getName());
ps.setFloat(2, emp.getSal());
ps.setString(3, emp.getSex());
ps.setInt(4, emp.getId());
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(null, ps, conn);
}
}
}
以下是改造后的实现方式:
1、实体类同上
2、接口同上
3、用于处理查询结果的接口:
public interface RowMapper {
public Object mapperRow(ResultSet rs)throws SQLException;
}
4、模拟JdbcTemplate类
public class JdbcTemplate {
public int update(String sql, Object[] args) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
return ps.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(null, ps, conn);
}
}
public Object find(String sql, Object[] args, RowMapper rowMapper)
throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Object obj = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
if (rs.next()) {
obj = rowMapper.mapperRow(rs);
}
} catch (SQLException e) {
throw e;
} finally {
DBUtil.close(rs, ps, conn);
}
return obj;
}
5、DAO实现类:
public class EmpDAOJdbcTemplateImpl implements EmpDAO {
private JdbcTemplate jdbcTemplate = new JdbcTemplate();
public int delete(Emp emp) throws SQLException {
String sql = "delete from emp where id=?";
Object[] args = new Object[] { emp.getId() };
return jdbcTemplate.update(sql, args);
}
public Emp findById(Emp emp) throws SQLException {
String sql = "select id,name,sal,sex from emp where id=?";
Object[] args = new Object[] { emp.getId() };
Object obj = jdbcTemplate.find(sql, args, new RowMapper() {
@Override
public Object mapperRow(ResultSet rs) throws SQLException {
Emp emp = new Emp();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
emp.setSal(rs.getFloat("sal"));
emp.setSex(rs.getString("sex"));
return emp;
}
});
return (Emp) obj;
}
public String findName(Emp emp) throws SQLException {
String sql = "select name from emp where id=?";
Object[] args = new Object[] { emp.getId() };
Object obj = jdbcTemplate.find(sql, args, new RowMapper() {
@Override
public Object mapperRow(ResultSet rs) throws SQLException {
return rs.getString("name");
}
});
return (String) obj;
}
public int insert(Emp emp) throws SQLException {
String sql = "insert into emp(name,sal,sex) values(?,?,?)";
Object[] args = new Object[] { emp.getName(), emp.getSal(),
emp.getSex() };
return jdbcTemplate.update(sql, args);
}
public int update(Emp emp) throws SQLException {
String sql = "update emp set name=?,sal=?,sex=? where id=?";
Object[] args = new Object[] { emp.getName(), emp.getSal(),
emp.getSex(), emp.getId() };
return jdbcTemplate.update(sql, args);
}
}
分享到:
相关推荐
使用Spring的JdbcTemplate实现分页功能
strut2+spring+springjdbctemplate做的简易登录系统
模仿spring jdbcTemplate的粗略实现,只有很小的参考价值,如果是java初学者可以使用这个封装好的工具进行数据库操作,只需要在db.properties里配置好driver,url等信息
Spring JdbcTemplate调用Oracle存储过程输出游标结果集实现增删改查
一个非常简单基于注解的Spring JdbcTemplate,供初学者参考用。
Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,Druid数据库连接池的SpringJDBCTemplate所需的jar包,...
Spring JDBCTemplate连接池jar包
spring-jdbcTemplate实例工程
SSH整合JdbcTemplate_dao)_方式_总结
SpringJdbcTemplate封装工具包,包括规范model格式接口,封装SpringJdbcTemplate,实现分页,自适应多种数据库
Spring JdbcTemplate的操作,包括接口,BaseDao,log4J配置文件,主要为oracle数据库操作,很多特殊方法只对oracle有效
Spring中的JdbcTemplate,Spring中的的事务.。。。。。。。
1.Spring4前 spring-jdbc包是独立的,4以后spring-jdbc 就已经没有了
JdbcTemplate api 下载 Spring
spring JdbcTemplate query方法使用示例,欢迎下载借鉴
使用Spring的JdbcTemplate调用Oracle的存储过程
spring jdbctemplate组件的简单实例。可以直接运行该实例来学习spring的jdbctemplate。处于初学者,或开发互联网性能要求较高的比较有料。
SSH笔记-Spring JdbcTemplate,使用JdbcTemplate对数据库进行操作,使用具名参数和JDBC模板,简化操作
NULL 博文链接:https://rayfuxk.iteye.com/blog/2286519
运用Spring中关于JDBC的一个辅助类(JDBC Template),进行curd操作