这里写目录标题
一、什么是BaseDao?
BaseDao是一种通用的数据访问对象,用于提供对数据库的基本操作,例如插入、更新、删除和查询数据等。它通常封装了对数据库的底层操作,使得其他DAO对象可以继承BaseDao以获得对数据库的访问能力,同时减少了重复的代码编写。BaseDao可以提供基本的CRUD功能,同时也可以实现一些通用的查询方法,例如按条件查询、分页查询等。通过使用BaseDao,开发人员可以更加方便地操作数据库,提高开发效率。
BaseDao的优点:
1、代码复用
BaseDao提供了通用的数据库操作方法,可以将一些重复的数据库操作封装起来,从而减少代码冗余。
2、统一管理
通过BaseDao可以将数据库操作集中管理,可以统一维护和修改数据库操作逻辑,提高代码的可维护性。
3、提高开发效率
使用BaseDao可以快速完成数据库操作,减少开发人员编写重复代码的时间,提高开发效率。
4、提供扩展性
BaseDao可以根据具体业务需求进行扩展,例如添加新的查询方法或者自定义的数据库操作逻辑。
5、降低耦合性
通过BaseDao可以将数据库操作与业务逻辑分离,降低模块之间的耦合性,提高系统的可维护性和可测试性
二,BaseDao代码展示
1、连接:将数据库与Java进行连接
/**
* 数据库连接
*/
public class BaseDao {
Connection conn = null; // 数据连接对象
private String driver = "com.mysql.jdbc.Driver";// 数据库驱动字符串
private String url = "jdbc:mysql://localhost:3306/epet";// 连接URL字符串
private String user = "root"; // 数据库用户名
private String password = "root"; // 用户密码
/**
* 获取数据库连接对象
*/
public Connection getConnection() {
if(conn==null) {
// 获取连接并捕获异常
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();// 异常处理
}
}
return conn;/ 返回连接对象
}
2,进行增删改操作
public int update(String sql, Object... args) throws SQLException {
Connection conn=null;
PreparedStatement preparedStatement=null;
try {
conn=this.getConnection();
preparedStatement=conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i+1,args[i]);
}
int count= preparedStatement.executeUpdate();
return count;
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(preparedStatement,conn);
}
return 0;
}
3,查找操作
/**
* commodity 自己定义的封装对象
* @param sql
* @param args
* @return
* @throws SQLException
*/
public List<commodity> check( String sql, Object... args) throws SQLException {
List<commodity> show=new ArrayList<>();
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = this.getConnection();
preparedStatement=connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i+1,args[i]);
}
resultSet=preparedStatement.executeQuery();
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
while (resultSet.next()){
commodity c = new commodity();
for (int i = 0; i < columnCount; i++) {
c.setId(resultSet.getInt("id"));
c.setName(resultSet.getString("name"));
c.setCount(resultSet.getInt("count"));
c.setPrice(resultSet.getBigDecimal("price"));
c.setAddress(resultSet.getString("addRess"));
}
show.add(c);
}
return show;
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭连接
// close(resultSet,preparedStatement,connection);
}
return null;
}
4,关闭连接
//重载
public void close(ResultSet resultSet) throws SQLException {
if (resultSet !=null){
resultSet.close();
}
}
public void close(Connection conn) throws SQLException {
if (conn !=null){
conn.close();
}
} public void close(PreparedStatement preparedStatement) throws SQLException {
if (preparedStatement !=null){
preparedStatement.close();
}
}
public void close(ResultSet resultSet,PreparedStatement preparedStatement,Connection conn) throws SQLException {
close(resultSet);
close(preparedStatement);
close(conn);
}
public void close(PreparedStatement preparedStatement,Connection conn) throws SQLException {
close(preparedStatement);
close(conn);
}
以上就是全部内容…