Dao包含 5个方法
interface Dao<F>(){
}
1.更新数据库
Connection 为数据库链接对象,sql为数据库执行语句,args为 填充参数
void update(Connection connection ,String sql, Object ... args);
2.获取数据库结果集
List<F> getForList(Connection connection, String sql ,Object ... args);
3.获取数据库的查询的第一个对象
F getForList(Connection connection ,String sql,Object ... args);
Dao代码
package cn.lw.mydao;
import java.sql.Connection;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.util.List;
public interface DAO<T> {
/**
* 批量处理方法
* @param connection
* @param sql
* @param args
* @throws SQLException
*/
void batch(Connection connection , String sql,Object[] ...args) throws SQLException;
/**
* 返回数据库库中的的一个记录。例如 一个人的姓名,一个人的成绩。
* @param connection
* @param sql
* @param args
* @return
* @throws SQLException
* @throws Exception
*/
<F> F getForValue(Connection connection,String sql,Object ...args) throws SQLException, Exception;
/**
* 返回 T 的一个集合
* @param connection
* @param sql
* @param args
* @return
* @throws SQLException
*/
List<T> getForList(Connection connection ,String sql, Object...args) throws SQLException;
/**
* 返回一行记录所对应的那个对象
* @param connection
* @param sql
* @param args
* @return
* @throws SQLException
*/
T get(Connection connection,String sql,Object ... args) throws SQLException;
/**
* 更新操作,可以包含的sql语句有 增删改
* @param connection 数据库链接
* @param sql 要执行的sql语句
* @param args 要填充的占位符
* @throws SQLException
*/
void update(Connection connection ,String sql,Object ... args) throws SQLException;
}
2.DaoImpl代码;这个类一般用对应数据库表文件的那个类来继承此类
package cn.lw.mydao;
import java.awt.Window.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import cn.lw.jdbc.User;
public class JdbcDaoImpl <T> implements DAO<T> {
private QueryRunner queryRunner=null;
private Class<T> type=null;
public JdbcDaoImpl() {
// TODO Auto-generated constructor stub
queryRunner =new QueryRunner();
type=ReflectionUtils.getSuperGenericType(this.getClass());//通过反射的方式获取父亲类指定泛型类的泛型用于JdbcUtils的Query()ResultSetHandler构造器的参数。这样就能返回 出对应的对象了
}
@Override
public void batch(Connection connection, String sql, Object[]... args) throws SQLException {
// TODO Auto-generated method stub
queryRunner.batch(connection, sql, args);
}
@Override
public <F> F getForValue(Connection connection, String sql, Object... args) throws Exception {
// TODO Auto-generated method stub
return (F) queryRunner.query(connection, sql, new ScalarHandler(), args);
}
@Override
public List<T> getForList(Connection connection, String sql, Object... args) throws SQLException {
// TODO Auto-generated method stub
return queryRunner.query(connection, sql, new BeanListHandler<>(type),args);
}
@Override
public T get(Connection connection, String sql, Object... args) throws SQLException {
// TODO Auto-generated method stub
return queryRunner.query(connection, sql, new BeanHandler<>(type), args
);
}
@Override
public void update(Connection connection, String sql, Object... args) throws SQLException {
// TODO Auto-generated method stub
queryRunner.update(connection, sql, args);
}
}
3.UserDao代码
package cn.lw.mydao;
import cn.lw.jdbc.User;
public class UserDao extends JdbcDaoImpl<User>{
}
4测试代码
package cn.lw.mydao;
import static org.junit.jupiter.api.Assertions.*;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import cn.lw.dao.JdbcUtil;
import cn.lw.jdbc.User;
class TestUserDao {
UserDao userdao=new UserDao();
@Test
void testJdbcDaoImpl() {
fail("Not yet implemented");
}
@Test
void testBatch() {
fail("Not yet implemented");
}
@Test
void testGetForValue() {
fail("Not yet implemented");
}
@Test
void testGetForList() {
fail("Not yet implemented");
}
@Test
void testGet() {
Connection connection=JdbcUtil.getConnection();
String sql ="select uname name ,password from user";
try {
User user=userdao.get(connection, sql);
System.out.println();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
void testUpdate() {
}
}