MyBatis原来做的是这些事

一、前言

    在使用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();
        }
 
    }

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡布奇诺-海晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值