封装orm

package util;

import domain.Atm;
import domain.RowMapper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SqlSession {

    String className = "com.mysql.cj.jdbc.Driver";
    String url = "jdbc:mysql://192.168.1.109:3306/testmybatis?useSSL=false&useUnicode=true&characeterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    String user = "root";
    String password ="root";

    /**
     * 增删改的封装
     * @param sql 执行的sql语句
     * @param objects sql语句上的问号值
     * @return 执行的结果条数
     */
    public Integer superUpdate(String sql,Object ... objects){
        int count = -1;
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url,user,password);
            PreparedStatement pstst = conn.prepareStatement(sql);){
            for(int i=1;i<=objects.length;i++){
                pstst.setObject(i,objects[i-1]);
            }
            count = pstst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 查询的封装
     * @param sql 执行的sql语句
     * @param objects sql语句上的问号值
     * @return 查询得到的结果
     */
    public <T>T selectOne(String sql, RowMapper rowMapper, Object ... objects){
        T t = null;
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url,user,password);
            PreparedStatement pstst = conn.prepareStatement(sql);
            ResultSet rs = pstst.executeQuery();){
            for(int i=1;i<=objects.length;i++){
                pstst.setObject(i,objects[i-1]);
            }
            if (rs.next()){
                t = (T)rowMapper.mapperRow(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
    /**
     * 查询的封装
     * @param sql 执行的sql语句
     * @param objects sql语句上的问号值
     * @return 查询得到的结果
     */
    public <T> List<T> selectAll(String sql, RowMapper rowMapper, Object ... objects){
        List<T> list = null;
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try(Connection conn = DriverManager.getConnection(url,user,password);
            PreparedStatement pstst = conn.prepareStatement(sql);
            ResultSet rs = pstst.executeQuery();){
            for(int i=1;i<=objects.length;i++){
                pstst.setObject(i,objects[i-1]);
            }
            while (rs.next()){
                if(list==null){
                    list = new ArrayList<T>();
                }
                list.add ((T)rowMapper.mapperRow(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}

上一篇:高性能数据库ORM框架 GoMybatis


下一篇:.net5 仓储管理系统,可快速开发框架,orm使用的EFC ode,缓存采用的是redis,日志使用了log4net