目录
Dao操作的通用步骤
Dao里面实现的操作
代码示例
实体类 Admin
常用方法的封装 BaseDao
AdminDao
AdminDaotest
Dao操作的通用步骤
//0.编写sql语句
String sql = "select * from dept ";//1.获取连接
Connection conn = JDBCUtil.getConnection(); //2.创建Statement preparedStatement是Statement的一个子类
pstmt = conn.prepareStatement(sql);//Statement stmt = conn.createStatement(); //3.执行sql并返回结果集//a.更新删除添加
int rs = pstmt.executeUpdate(sql); //影响的记录条数// b 查询 Resultset rs = pstmt.execute.Query(sql);//4..关闭
JDBCUtil.close(conn, pstmt,rs);
Dao里面实现的操作
1. 更新 删除 添加 类似
String sql = “select * from admin”;
String sql = “select * from admin where id=? And pwd =?”;
public void update(String sql, Object[] paramValues);2. 查询
String sql = “select * from admin”;
String sql = “select * from admin where id=? And pwd =?”;
// 传入的什么类型的对象,就封装为什么类型
// 要求: 列的名称,要与指定类型的对象的属性名称一样
Public List query (String sql , Object[] paramValues , Class clazz);
T t; // 对象赋值
代码示例
实体类 Admin
package cn.itcase.dao;
/*** 实体类* @author Administrator**/
public class Admin {private int id;
private String user;
private String pwd;public int getId() {return id;
}
public void setId(int id) {this.id = id;}
public String getUser() {return user;
}
public void setUser(String user) {this.user = user;
}
public String getPwd() {return pwd;
}
public void setPwd(String pwd) {this.pwd = pwd;
}public Admin(int id, String user, String pwd){super();this.id = id;this.user = user;this.pwd = pwd;
}
public Admin(){}
}
常用方法的封装 BaseDao
package cn.itcase.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import org.apache.commons.beanutils.BeanUtils;/*** BaseDao 封装通用的操作方法,自己写的dao都继承此类 1.更新 2.查询* * @author Administrator* */
public class BaseDao {// 初始化对象private Connection conn;private PreparedStatement pstmt;private ResultSet rs;/*** 更新的通用方法* * @param sql* 更新的sql语句(update/insert/delete)* @param paramsValue* sql语句中占位符对应的值(如果没有占位符,传入null)*/public void update(String sql, Object paramsValue[]) {try {// 1.获取连接conn = JDBCUtil.getConnection();// 2.创建执行命令的stmt对象pstmt = conn.prepareStatement(sql);// 3.参数元数据,得到占位符参数的个数int count = pstmt.getParameterMetaData().getParameterCount();// 设置占位符参数的值if (paramsValue != null && paramsValue.length > 0) {// 循环给参数赋值for (int i = 0; i List query(String sql, Object paramsValue[], Class clazz) {/* List* 第一个 告诉编译器用T作为泛型* 第二个 表示泛型* 泛型的意思是类型可以在以后指定,* */try {// 返回的集合List list = new ArrayList();// 初始化对象T t = null;// 1.获取连接conn = JDBCUtil.getConnection();// 2.创建stmt对象pstmt = conn.prepareStatement(sql);// 3.获取占位符参数个数,并设置每个参数的值int count = pstmt.getParameterMetaData().getParameterCount();if(paramsValue != null && paramsValue.length > 0){for(int i = 0; i}
AdminDao
package cn.itcase.dao;import java.util.List;public class AdminDao extends BaseDao {// 删除public void delete(int id) {String sql = "delete from admin where id = ?;";Object paramsValue[] = { id };super.update(sql, paramsValue);}// 插入public void save(Admin admin) {String sql = "insert into admin(userName,pwd) values(?,?);";Object paramsValue[] = { admin.getUser(), admin.getPwd() };super.update(sql, paramsValue);}// 查询全部public List getAll() {String sql = "select * from admin";List list = super.query(sql, null, Admin.class);return list;}// 根据条件查询public Admin findById(int id){String sql = "select * from admin where id=?";List list = super.query(sql,new Object[]{id},Admin.class);return (list != null && list.size() > 0) ? list.get(0) :null;}}
AdminDaotest
package cn.itcase.dao;import java.util.List;import org.junit.Before;
import org.junit.Test;/*** AdminDao的测试类* * @author Administrator* */
public class AdminDaotest {// 初始化对象AdminDao adminDao = null;@Beforepublic void init() {adminDao = new AdminDao();}// 插入数据@Testpublic void testsave() {adminDao = new AdminDao();Admin admin = new Admin();admin.setUserName("张三");admin.setPwd("0000");adminDao.save(admin);}// 删除数据@Testpublic void delete() {adminDao = new AdminDao();System.out.println();}// 查询全部测试@Testpublic void getAlltest() {adminDao = new AdminDao();List list = adminDao.getAll();for(Admin admin :list){System.out.println(list);}}// 根据条件查询(主键)@Testpublic void findByIdtest() {adminDao = new AdminDao();Admin admin = adminDao.findById(1);System.out.println(admin);}}