栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java工具类及配置文件模板大全

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java工具类及配置文件模板大全

JDBC通用增删改查

JDBC工具类

public class Util1 {
    //加载驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获得连接
    public static Connection getConnection(){
        try {
            DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?characterEnconding=utf-8","root","root");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static boolean executeUpdate(String sql,Object... args){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            //有参数
            for(int i=0;i0;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            //关闭资源
            close(conn,ps,null);
        }
        return false;
    }
    
    public static List> executeQuery(String sql,Object... args){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            //有可能有参数
            for(int i=0;i> list = new ArrayList<>();
            //获取本次查询结果集有多少列
            int count = rs.getmetaData().getColumnCount();
            //while循环
            while(rs.next()){
                //创建Map集合   获取一个数据封装成一个Map集合
                Map map = new HashMap<>();
                //for循环  遍历所有的列
                for(int i=0;i 

自定义配置文件版

public class baseDao {
    // 配置集合
    public static Properties properties = new Properties();

    // 加载驱动
    static {
        // 集合对象加载数据
        InputStream is = baseDao.class.getResourceAsStream("data.properties");
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取配置中的数据,从集合中获取
        String driver = properties.getProperty("driver");

        // 加载jdbc驱动
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    // 获取连接对象
    public Connection getConnection() {
        // 获取配置中的数据,从集合中获取
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String pwd = properties.getProperty("pwd");

        // 获取连接对象
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 返回连接对象
        return connection;
    }

    // 关闭资源
    public void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        // 按顺序删除
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    //通用的增删改
    public int commonUpdate(String sql, Object[] objects) {
        // 获取连接对象
        Connection connection = getConnection();

        // 构建执行者
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 填入数据
        for (int i = 0; i < objects.length; i++) {
            try {
                preparedStatement.setObject(i + 1, objects[i]);
                // preparedStatement.setNull(位置, Types.INTEGER);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        // 执行语句
        int i = 0;
        try {
            i = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 返回受景响的行数
        return i;

    }

    // 通用查询
    public ResultSet commonSelect(String sql, Object[] objects) {
        // 定义一个返回值
        ResultSet resultSet = null;

        // 获取连接对象
        Connection connection = getConnection();

        // 获得安全执行者
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        for (int i = 0; i < objects.length; i++) {
            try {
                assert preparedStatement != null;
                preparedStatement.setObject(i + 1, objects[i]);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        // 安全执行者执行
        try {
            assert preparedStatement != null;
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        // 处理结果集
        return resultSet;
    }

}
Mybatis 工具类的封装

MyBatisUtils

public class MyBatisUtils {

    //创建SqlSession
    private static SqlSession sqlSession;

    //初始化创建SqlSession
    static {
        //工厂构建者
        SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
        //MyBatis配置文件名称
        String configName = "sqlMapConfig.xml";
        //定义输入流
        InputStream is = null;
        try {
            //读取配置文件
            is = Resources.getResourceAsStream(configName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //通过配置文件构建出工厂类
        SqlSessionFactory ssf = ssfb.build(is);
        //通过工厂类获取sqlSession
        sqlSession = ssf.openSession(true);
    }
    
    //获取sqlSession
    public static SqlSession getSqlsession(){
        return sqlSession;
    }
    
    //归还sqlSession
    public static void closeSqlsession(SqlSession sqlSession){
        if (sqlSession!=null){
            sqlSession.close();
        }
    }

}
MyBatis逆向生成

MyBatisUtils

public class GeneratorSqlmap {
    public void generator() throws Exception{

        List warnings = new ArrayList();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        File configFile = new File("generatorConfig.xml");
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(configFile);
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                callback, warnings);
        myBatisGenerator.generate(null);

    }
    public static void main(String[] args) throws Exception {
        try {
            GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
            generatorSqlmap.generator();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

配置文件generatorConfig.xml





    
        
            
            
        
        
        
            
        
        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        
DateUtil
public class DateUtil {

    
    public static String getDateStr(Date date) {
        // 实例化一个格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 把日期对象格式化
        // 返回这个字符串
        return sdf.format(date);
    }

    
    public static Date getDate(String dateStr) {
        // 实例化一个格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 日期格式解析
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 返回日期
        return date;
    }
}
MD5加密工具类MD5Utils
public class MD5Util {
    
    public final static String getMD5(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA");//创建具有指定算法名称的摘要
            md.update(str.getBytes());                    //使用指定的字节数组更新摘要
            byte mdBytes[] = md.digest();                 //进行哈希计算并返回一个字节数组

            String hash = "";
            for(int i= 0;i
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354154.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号