com.jsj.utils===新闻发布系统(5)

2020-05-14

刚刚我完成了对dao层的业务的定义,定义了我们需要实现的操作的名称

下面我开始介绍工具类,这个就是工具,就是通过提前写好什么类,

然后后面如果你遇到,就直接调用就好了

 

为什么我要先介绍了?

  因为,后面daoImpl需要调用方法,害怕等会看不懂

 

BeanHandler.java

 

 1 package com.jsj.utils;
 2 
 3 import java.sql.ResultSet;
 4 
 5 public class BeanHandler extends ResultSetHandler {
 6 
 7     private Class<?> clazz;
 8     //构造函数:来初始化的
 9     public BeanHandler(Class<?> clazz){
10         this.clazz = clazz;
11     }
12 
13     @Override
14     public Object handle(ResultSet resultSet) throws Exception {
15         if (resultSet.next()){
16             return buildObject(resultSet, clazz);
17         }else {
18             return null;
19         }
20     }
21 
22 }
View Cod

 

BeanListHandler.java

 

 1 package com.jsj.utils;
 2 
 3 import java.sql.ResultSet;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 public class BeanListHandler extends ResultSetHandler {
 8     //无法确定类型的泛型
 9     private Class<?> clazz;
10     //也是初始化变量
11     public BeanListHandler(Class<?> clazz){
12         this.clazz = clazz;
13     }
14 
15     @Override
16     public List handle(ResultSet resultSet) throws Exception {
17         //数组
18         List list = new ArrayList();
19         //
20         while(resultSet.next()) {
21             Object bean = buildObject(resultSet, clazz);
22             list.add(bean);
23         }
24         return list;
25     }
26 }
View Code

 

JdbcTemple.java

 

 1 package com.jsj.utils;
 2 
 3 import java.sql.*;
 4 
 5 public class JdbcTemplate {
 6 
 7     private JdbcTemplate(){
 8     }
 9 
10     /**
11      * 更新方法
12      */
13     public static int update(String sql, Object ...params) throws SQLException {
14        //创建连接
15         Connection connection = JdbcUtils.getConnection();
16         //然后是获取PreparedStatement对象
17         //try(语句)这种写法,可以自动调动close()方法
18         try(PreparedStatement preparedStatement = connection.prepareStatement(sql)){
19            //这个是赋值,一个问号对应一哥变量,数从1开始
20             for (int i = 1; i <= params.length; i++){
21                 preparedStatement.setObject(i, params [i-1]);
22             }
23             //返回的是数据库发生改变的行数目,
24             //可以用来确定是否执行成功
25             return preparedStatement.executeUpdate();
26         }finally {
27             //释放了申请的资源
28             //比如connection
29             JdbcUtils.releaseConnection(connection);
30         }
31     }
32 
33     /**
34      * 查询方法
35      */
36     public static Object query(String sql, ResultSetHandler handler, Object... params) throws Exception {
37         //创建连接对象
38         Connection connection = JdbcUtils.getConnection();
39         //执行sql语句
40         try(PreparedStatement preparedStatement = connection.prepareStatement(sql)){
41             //读出值
42             for (int i = 1; i <= params.length; i++){
43                 preparedStatement.setObject(i, params [i-1]);
44             }
45             //resultSet就是flag,就是确定是否成功
46             try(ResultSet resultSet = preparedStatement.executeQuery()){
47                 //
48                 return handler.handle(resultSet);
49             }
50         }finally {
51             //释放connection资源
52             JdbcUtils.releaseConnection(connection);
53         }
54     }
55 
56     public static int queryCount(String sql, Object... params) throws Exception{
57         //创建数据库连接
58         Connection connection = JdbcUtils.getConnection();
59         //括号里面是导入sql语句,获取对象
60         try(PreparedStatement preparedStatement = connection.prepareStatement(sql)){
61             //这个是赋值
62             for (int i = 1; i <= params.length; i++){
63                 preparedStatement.setObject(i, params [i-1]);
64             }
65             //执行sql语句
66             try(ResultSet resultSet = preparedStatement.executeQuery()){
67                 //读取数据的时候
68                 if (resultSet.next()){
69                     //还有返回1
70                     return resultSet.getInt(1);
71                 }else {
72                     //没有数据了,返回-1
73                     return -1;
74                 }
75             }
76         }finally {
77             JdbcUtils.releaseConnection(connection);
78         }
79     }
80 
81 }
View Code

 

JdbcUtils.java

 1 package com.jsj.utils;
 2 
 3 import com.mchange.v2.c3p0.ComboPooledDataSource;
 4 
 5 import java.sql.Connection;
 6 import java.sql.SQLException;
 7 
 8 /**
 9  * jdbc工具类
10  */
11 public class JdbcUtils {
12 
13     private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
14 
15     private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
16 
17     public static Connection getConnection() throws SQLException {
18         Connection transactionConnection = threadLocal.get();
19         if (transactionConnection!=null){
20             return transactionConnection;
21         }
22         return dataSource.getConnection();
23     }
24 
25     /**
26      * 开启事务
27      */
28     public static void beginTransaction() throws SQLException {
29         Connection transactionConnection = threadLocal.get();
30         if (transactionConnection!=null){
31             throw new SQLException("事务已开启");
32         }
33         transactionConnection = getConnection();
34         transactionConnection.setAutoCommit(false);
35         threadLocal.set(transactionConnection);
36     }
37 
38     /**
39      * 提交事务
40      */
41     public static void commitTransaction() throws SQLException {
42         Connection transactionConnection = threadLocal.get();
43         if (transactionConnection==null){
44             throw new SQLException("事务未开启");
45         }
46         transactionConnection.commit();
47         transactionConnection.close();
48         threadLocal.remove();
49     }
50 
51     /**
52      * 回滚事务
53      */
54     public static void rollbackTransaction() throws SQLException {
55         Connection transactionConnection = threadLocal.get();
56         if (transactionConnection==null){
57             throw new SQLException("事务未开启");
58         }
59         transactionConnection.rollback();
60         transactionConnection.close();
61         threadLocal.remove();
62     }
63 
64     /**
65      * 释放连接资源
66      */
67     public static void releaseConnection(Connection connection) throws SQLException {
68         if (threadLocal.get()==null){
69             connection.close();
70             return;
71         }
72         if (connection!=threadLocal.get()){
73             connection.close();
74         }
75     }
76 }
View Code

 

 ResultSetHandler.java

 1 package com.jsj.utils;
 2 
 3 import java.lang.reflect.Field;
 4 import java.sql.ResultSet;
 5 
 6 abstract class ResultSetHandler {
 7 
 8     abstract Object handle(ResultSet resultSet) throws Exception;
 9 
10     Object buildObject(ResultSet resultSet, Class<?> clazz) throws Exception {
11         Object bean = clazz.newInstance();
12         Field[] fields = clazz.getDeclaredFields();
13         // 封装数据
14         for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i ++) {
15             Object value = resultSet.getObject(i);
16             //匹配实体类中对应的属性
17             for (Field field : fields) {
18                 if (resultSet.getMetaData().getColumnName(i).replaceAll("_","").equals(field.getName().toLowerCase())) {
19                     boolean flag = field.isAccessible();
20                     field.setAccessible(true);
21                     field.set(bean, value);
22                     field.setAccessible(flag);
23                     break;
24                 }
25             }
26         }
27         return bean;
28     }
29 
30 }
View Code

 

posted @ 2020-05-15 17:20  赚钱买个打印机  阅读(187)  评论(0编辑  收藏  举报