zl程序教程

您现在的位置是:首页 >  后端

当前栏目

MyBatis原来做的是这些事

mybatis 这些 原来
2023-09-14 09:14:06 时间

一、前言

    在使用MyBatis之前,在学校的编程课里,我们最先接触的就是JDBC(Java Database Connectivity)Java数据库连接。

二、JDBC流程

1、注册驱动

 // 1、注册驱动
 Class.forName("com.mysql.cj.jdbc.Driver");

2、获取连接

public class Demo1 {
 
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // 1、注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 2、数据库连接基本信息url、user、password
        String url = "jdbc:mysql://localhost:3306/seckill?useSSL=false&useUnicode=true&characterEncoding=UTF-8&useAffectedRows=true&nullCatalogMeansCurrent=true&allowMultiQueries=true&serverTimezone=GMT%2B8";
        String user = "root";
        String password = "123456";
        // 3、获取连接
        Connection conn = DriverManager.getConnection(url, user, password);
        log.info(">>>>>>>>>>>>【{}】", conn);
 
}

3、创建StatementPreparestatement

   public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        initDataSource();// 初始化连接池
        try{// 查询数据
            String sql = "select * from sec_kill";
            Statement statement = connection.createStatement();
 
        }catch (Exception e){
            log.info(">>>>>>>>>>>>", e);
        }
 
    }

注:Preparestatement有预防SQL注入的功效

4、executeQueryexecuteUpdate执行查询/更新

   public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        initDataSource();// 初始化连接池
        try{// 查询数据
            String sql = "select * from sec_kill";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                long id = rs.getLong("sec_kill_id");
                String name = rs.getString("name");
                log.info(">>>>>>id:{},name:{}", id, name);
            }
 
        }catch (Exception e){
            log.info(">>>>>>>>>>>>", e);
        }
 
    }

5、涉及事务时(增修改一致性)

5.1、setAutoCommit设置是否自动提交

5.2、commit提交事务

5.3、rollback抛异常手动回滚事务

		try {
            // 开起事务
            conn.setAutoCommit(false);
            // 积分扣减
            
            // 下单

			// 提交事务
			conn.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			// 事务回滚
			conn.rollback();
		}

:需要事务在同一个连接上

6、close释放资源

   public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        initDataSource();// 初始化连接池
        try{// 查询数据
            String sql = "select * from sec_kill";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                long id = rs.getLong("sec_kill_id");
                String name = rs.getString("name");
                log.info(">>>>>>id:{},name:{}", id, name);
            }
 
        }catch (Exception e){
            log.info(">>>>>>>>>>>>", e);
        } finally {
             connection.close();
             statement.close();
        }
 
    }