BaseDao的增删改查【入门级】

一.BaseDao是什么

BaseDao是为了解决数据库中增删改查而的提供的一个定义类,是一个接口代码,公共方法的接口类。

BaseDao的特性

(1).支持复杂查询
对于数据库中所对应的复杂的查询,可以在编译时自动生成Query类,而Query类可以帮助更加方便地构建查询条件。

(2).逻辑删除功能
支持逻辑删除,在逻辑删除模式下,删除操作自动切换为更新删除标记操作,新增操作自动初始化为未删除状态,更新和查询操作自动忽略已删除数据。

(3). 支持分库分表
内置分库分表功能,支持指定键分键和自定义分库分表算法。

(4).代码生成
提供工具通过数据库表结构自动生成Entity类,提高开发效率。

二.BaseDao的操作

1.第一步创建名叫BaseDao的类
2.开始实现数据库与Java的连接
public class BaseDao {
   private String driver="com.mysql.jdbc.Driver"; //数据库驱动字符串
   private String url="jdbc:mysql://localhost:3306/a?serverTimezone=GMT"; //连接URL字符串
   private String user="root";  //数据库用户名
   private String password="ww121212"; //用户密码
	/**
     * 获取数据库连接对象
     */
   public Connection getConnection(){
       Connection con=null;
        				// 获取连接并捕获异常
       try{
           Class.forName(driver);
           con=DriverManager.getConnection(url,user,password);
       }catch (Exception e){
           e.printStackTrace(); 	// 异常处理
       }
       return  con;				// 返回连接对象

   }
3. 查询的操作

    /**
     * 查询一个字段 (只会返回一条记录且只有一个字段;常用场景:查询总数量)
     *  1. 得到数据库连接
     *  2. 定义sql语句
     *  3. 预编译
     *  4. 如果有参数,则设置参数,下标从1开始 (数组或集合、循环设置参数)
     *  5. 执行查询,返回结果集
     *  6. 判断并分析结果集
     *  7. 关闭资源
     *
     *  注:需要两个参数:sql语句、所需参数的集合
     *
     * @param sql
     * @param params
     * @return
     */
    public static Object findSingleValue(String sql, List<Object> params) {
        Object object = null;
        Connection conn= null;
        PreparedStatement pstmt = null;
        ResultSet rs= null;

        try {
            // 获取数据库连接
            conn= getConnetion();
            // 预编译
            pstmt = conn.prepareStatement(sql);
            // 如果有参数,则设置参数,下标从1开始
            if (params != null && params.size() > 0) {
                // 循环设置参数,设置参数类型为Object
                for (int i = 0; i < params.size(); i++){
                    pstmt .setObject(i+1, params.get(i));
                }
            }
            // 执行查询,返回结果集
            rs= pstmt .executeQuery();
            // 判断并分析结果集
            if (rs.next()) {
                object = rs.getObject(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            this.close(rs, pstmt , conn);
        }
        return object;
    }



4. 增删改的操作
 /**
	 * 增、删、改的操作
	 * @param sql 预编译的 SQL 语句          
	 * @param param 参数的字符串数组          
	 * @return 影响的行数
	 */
	public int exceuteUpdate (String preparedSql, Object[] param) {
		PreparedStatement pstmt = null;
		int num = 0;
		conn =  getConnection(); 
		try {
			pstmt = conn.prepareStatement(preparedSql);
			if (param != null) {
				for (int i = 0; i < param.length; i++) {
                     	//为预编译sql设置参数
					pstmt.setObject(i + 1, param[i]); 
				}
			}
			num = pstmt.executeUpdate(); 
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			closeAll(conn,pstmt,null);
		}
		return num;
	}
}
5.关闭数据库
/**
     * 关闭数据库连接
     * @param conn 数据库连接
     * @param stmt Statement对象
     * @param rs 结果集
     */
    public void closeAll(Connection conn, Statement stmt, 
                   ResultSet rs) {
        // 若结果集对象不为空,则关闭
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 若Statement对象不为空,则关闭
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        // 若数据库连接对象不为空,则关闭
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

以上就是BaseDao的基本操作了。

相关推荐

  1. BaseDao增删入门

    2024-03-28 04:54:03       45 阅读
  2. BaseDao增删

    2024-03-28 04:54:03       49 阅读
  3. baseDao增删.

    2024-03-28 04:54:03       41 阅读
  4. BaseDao封装增删(超详解!)

    2024-03-28 04:54:03       44 阅读
  5. BaseDao封装增删(超详解)

    2024-03-28 04:54:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 04:54:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 04:54:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 04:54:03       82 阅读
  4. Python语言-面向对象

    2024-03-28 04:54:03       91 阅读

热门阅读

  1. vue3从精通到入门3:patch函数源码实现方式

    2024-03-28 04:54:03       31 阅读
  2. 关于分布式系统设计的个人看法和经验

    2024-03-28 04:54:03       38 阅读
  3. 大历史下的 pacing:程序员视角和大历史视角

    2024-03-28 04:54:03       48 阅读
  4. Linux的相关指令总结

    2024-03-28 04:54:03       40 阅读
  5. 前端下载超大文件的完整方案

    2024-03-28 04:54:03       39 阅读