这里涉及到反射,不过我忘记记录了。后面学习框架应该会在提到。只是简单的记录一下。
我们在针对不同的数据表/JavaBean实例操作的时候,有一些操作是通用的,即增、删、改、查。不同的只是sql语句和参数不同而已。这样我们把相同的操作封装到BaseDao中,其他Dao的操作直接继承这些方法,并且我们将方法设置为protected,则这些方法不对外提供,只供Dao类自身调用。由于它不是具体的类的映射,所以定位虚类。
/** * */ package com.atguigu.dao; 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 com.alibaba.druid.pool.DruidPooledConnection; import com.atguigu.tools.JdbcUtils; /** * * @Title: BaseDao * @Description: * @author NianHao.X * @date 2020年3月31日 */ public abstract class BaseDao { private QueryRunner queryRunner = new QueryRunner(); /** * * @Title: update * @Description: TODO * @param sql * @param args * @return int * @date 2020-03-31 08:44:14 */ protected int update(String sql,Object...args){ DruidPooledConnection connection = JdbcUtils.getConnection(); try { return queryRunner.update(connection,sql, args); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JdbcUtils.closeConnection(connection); } return -1; } protected <T> T queryForOne(Class<T> type,String sql,Object...args) { DruidPooledConnection connection = JdbcUtils.getConnection(); try { return queryRunner.query(connection,sql,new BeanHandler<T>(type),args ); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JdbcUtils.closeConnection(connection); } return null; } protected <T> List<T> queryForList(Class<T>type,String sql,Object...args ){ DruidPooledConnection connection = JdbcUtils.getConnection(); try { return queryRunner.query(connection,sql,new BeanListHandler<T>(type),args ); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { JdbcUtils.closeConnection(connection); } return null; } protected Object queryForSingleValue(String sql, Object... args){ DruidPooledConnection connection = JdbcUtils.getConnection(); try { return queryRunner.query(connection, sql, new ScalarHandler(), args); } catch (Exception e) { e.printStackTrace(); } finally { JdbcUtils.closeConnection(connection); } return null; } }
这里我们的连接是通过数据库连接池管理的。我们使用的是阿里的druid数据库连接池。
/** * */ package com.atguigu.tools; import java.io.InputStream; import java.util.Properties; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.pool.DruidPooledConnection; import com.mysql.jdbc.Connection; /** * @author nianhao * */ public class JdbcUtils { private static DruidDataSource dataSource; static { try { Properties properties = new Properties(); InputStream inStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(inStream); dataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } public static DruidPooledConnection getConnection() { DruidPooledConnection connection = null; try { connection = dataSource.getConnection(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return connection; } public static void closeConnection(DruidPooledConnection connection) { try { if(connection!=null) { connection.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }