BaseDao

导入 DBUtils 的 jar 包:commons-dbutils-1.3.jar(这是BaseDao的)  

druid-1.1.9.jar  与  mysql-connector-java-5.1.7-bin.jar  (这是jdbcUtils的)

代码:

jdbc.propertis代码:

1 username=root
2 password=123456
3 url=jdbc:mysql://localhost:3306/book
4 driverClassName=com.mysql.jdbc.Driver
5 initialSize=5
6 maxActive=10

JdbcUtiles工具类

 1 import com.alibaba.druid.pool.DruidDataSource;
 2 import com.alibaba.druid.pool.DruidDataSourceFactory;
 3 
 4 import java.io.InputStream;
 5 import java.sql.Connection;
 6 import java.sql.SQLException;
 7 import java.util.Properties;
 8 
 9 public class JdbcUtils {
10 
11     private static DruidDataSource dataSource;
12 
13     static {
14         try {
15             Properties properties = new Properties();
16             // 读取 jdbc.properties属性配置文件
17             InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
18             // 从流中加载数据
19             properties.load(inputStream);
20             // 创建 数据库连接 池
21             dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25 
26     }
27 
28     /**
29      * 获取数据库连接池中的连接
30      *
31      * @return 如果返回null, 说明获取连接失败<br />有值就是获取连接成功
32      */
33     public static Connection getConnection() {
34 
35         Connection conn = null;
36 
37         try {
38             conn = dataSource.getConnection();
39         } catch (Exception e) {
40             e.printStackTrace();
41         }
42 
43         return conn;
44     }
45 
46     /**
47      * 关闭连接,放回数据库连接池
48      *
49      * @param conn
50      */
51     public static void close(Connection conn) {
52         if (conn != null) {
53             try {
54                 conn.close();
55             } catch (SQLException e) {
56                 e.printStackTrace();
57             }
58         }
59     }
60 }

 

BaseDao代码(抽象类)

 1 import com.atguigu.utils.JdbcUtils;
 2 import org.apache.commons.dbutils.QueryRunner;
 3 import org.apache.commons.dbutils.handlers.BeanHandler;
 4 import org.apache.commons.dbutils.handlers.BeanListHandler;
 5 import org.apache.commons.dbutils.handlers.ScalarHandler;
 6 
 7 import java.sql.Connection;
 8 import java.sql.SQLException;
 9 import java.util.List;
10 
11 public abstract class BaseDao {
12 
13     //使用DbUtils操作数据库
14     private QueryRunner queryRunner = new QueryRunner();
15 
16     /**
17      * update() 方法用来执行:Insert\Update\Delete语句
18      *
19      * @return 如果返回-1,说明执行失败<br/>返回其他表示影响的行数
20      */
21     public int update(String sql, Object... args) {
22         Connection connection = JdbcUtils.getConnection();
23         try {
24             return queryRunner.update(connection, sql, args);
25         } catch (SQLException e) {
26             e.printStackTrace();
27         } finally {
28             JdbcUtils.close(connection);
29         }
30         return -1;
31     }
32 
33     /**
34      * 查询返回一个javaBean的sql语句
35      *
36      * @param type 返回的对象类型
37      * @param sql  执行的sql语句
38      * @param args sql对应的参数值
39      * @param <T>  返回的类型的泛型
40      * @return
41      */
42     public <T> T queryForOne(Class<T> type, String sql, Object... args) {
43         Connection con = JdbcUtils.getConnection();
44         try {
45             return queryRunner.query(con, sql, new BeanHandler<T>(type), args);
46         } catch (SQLException e) {
47             e.printStackTrace();
48         } finally {
49             JdbcUtils.close(con);
50         }
51         return null;
52     }
53 
54     /**
55      * 查询返回多个javaBean的sql语句
56      *
57      * @param type 返回的对象类型
58      * @param sql  执行的sql语句
59      * @param args sql对应的参数值
60      * @param <T>  返回的类型的泛型
61      * @return
62      */
63     public <T> List<T> queryForList(Class<T> type, String sql, Object... args) {
64         Connection con = JdbcUtils.getConnection();
65         try {
66             return queryRunner.query(con, sql, new BeanListHandler<T>(type), args);
67         } catch (SQLException e) {
68             e.printStackTrace();
69         } finally {
70             JdbcUtils.close(con);
71         }
72         return null;
73     }
74 
75     /**
76      * 执行返回一行一列的sql语句
77      *
78      * @param sql  执行的sql语句
79      * @param args sql对应的参数值
80      * @return
81      */
82     public Object queryForSingleValue(String sql, Object... args) {
83 
84         Connection conn = JdbcUtils.getConnection();
85 
86         try {
87             return queryRunner.query(conn, sql, new ScalarHandler(), args);
88         } catch (Exception e) {
89             e.printStackTrace();
90         } finally {
91             JdbcUtils.close(conn);
92         }
93         return null;
94     }
95 }

 

posted @ 2020-03-27 12:35  王余阳  阅读(535)  评论(0)    收藏  举报