自定义mvc学习

文章目录

mvc版查询、分页

效果图
自定义mvc学习_第1张图片
代码如下

查询方法:
自定义mvc学习_第2张图片

用来做通用分页

public List executeQuery(String sql, PageBean pageBean, Callback callBack) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		if (null != pageBean && pageBean.isPagination()) {
			try {	
				con = DBAccess.getConnection();
				String countSql = this.getCountSql(sql);
				ps = con.prepareStatement(countSql);
				rs = ps.executeQuery();
				if (rs.next()) {
					int total = rs.getInt(1);// 总记录数
					pageBean.setTotal(total);// 给pagebean的总记录数赋值
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			} finally {
				DBAccess.close(null, ps, rs);
			}
		}
		// 2.查询指定页码并满足条件的总记录数
		try {
			if(con==null) {
				con=DBAccess.getConnection();
			}
			String pageSql =sql;
			if(null!=pageBean&&pageBean.isPagination()) {
				pageSql = this.getPageSql(sql, pageBean);
			}
			ps = con.prepareStatement(pageSql);
			rs = ps.executeQuery();
			// 不同内容
			return callBack.foreach(rs);
		} catch (Exception e) {
			throw new RuntimeException();
		} finally {
			DBAccess.close(con, ps, rs);
		}
	}

专门写一个方法用来拼接查询语句的统计满足条件的总行数

public String getCountSql(String sql) {
		return "select count(*) from (" + sql + ") t1";
	}

专门写一个方法用来存储拼接分页的sql

public String getPageSql(String sql, PageBean pageBean) {
		return sql + " limit " + pageBean.getStartsIndex() + "," + pageBean.getRows();

	}

回调接口 专门用来遍历

public List foreach(ResultSet rs) throws SQLException;

私有的构造方法,保护此类不能在外部实例化

	private StringUtils() {
	}

如果字符串等于null或去空格后等于"",则返回true,否则返回false

public static boolean isBlank(String s) {
		boolean b = false;
		if (null == s || s.trim().equals("")) {
			b = true;
		}
		return b;
	}

如果字符串不等于null或去空格后不等于"",则返回true,否则返回false

public static boolean isNotBlank(String s) {
		return !isBlank(s);
	}

提供一组获得或关闭数据库对象的方法

	static {// 静态块执行一次,加载 驱动一次
		try {
			InputStream is = DBAccess.class
					.getResourceAsStream("config.properties");

			Properties properties = new Properties();
			properties.load(is);

			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("pwd");
			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得数据连接对象
	 */
	public static Connection getConnection() {
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(ResultSet rs) {
		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Statement stm) {
		if (null != stm) {
			try {
				stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn, Statement stm, ResultSet rs) {
		close(rs);
		close(stm);
		close(conn);
	}

	public static boolean isOracle() {
		return "oracle.jdbc.driver.OracleDriver".equals(driver);
	}

	public static boolean isSQLServer() {
		return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
	}
	
	public static boolean isMysql() {
		return "com.mysql.jdbc.Driver".equals(driver);
	}

	public static void main(String[] args) {
		Connection conn = DBAccess.getConnection();
		DBAccess.close(conn);
		System.out.println("isOracle:" + isOracle());
		System.out.println("isSQLServer:" + isSQLServer());
		System.out.println("isMysql:" + isMysql());
		System.out.println("数据库连接(关闭)成功");
	}

完成之后的文件:
自定义mvc学习_第3张图片
所用架包:
自定义mvc学习_第4张图片

你可能感兴趣的:(自定义mvc学习)