MyBatis代码实例系列-02:MyBatis用log4j打印SQL以及MyBatis的事务控制

超级通道:MyBatis代码实例系列-绪论

本章主要记录在MyBatis框架(不是SSM)中,如何用log4j打印SQL以及MyBatis的事务控制,涉及到的技术点有:
- log4j:日志打印
- MyBatis事务控制:分为JdbcTransactionManagedTransaction
- SqlSession:数据库连接会话

1.用log4j打印SQL

1.1.pom.xml

    <log4j.version>1.2.17</log4j.version>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>

1.2.log4j.properties

log4j.rootLogger = info,console,file

log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File = himybatis.log
log4j.appender.file.datePattern = '.'yyyy-MM-dd
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.console.file.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

1.3.Mybatis的log4j说明

将log4j的日志级别调节至debug,则会自动打印日志:

log4j.rootLogger = debug,console,file

1.4.result

2018-01-28 13:56:18 DEBUG insertStudent:54 - ==>  Preparing: INSERT INTO `student`(student_id,name,number) VALUES (?,?,?) 
2018-01-28 13:56:18 DEBUG insertStudent:54 - ==> Parameters: 4(Integer), 陈六(String), 006(String)
2018-01-28 13:56:18 DEBUG insertStudent:54 - <==    Updates: 1

2018-01-28 13:56:18 DEBUG queryStudentById:54 - ==>  Preparing: SELECT * FROM `student` WHERE student_id = ? 
2018-01-28 13:56:18 DEBUG queryStudentById:54 - ==> Parameters: 4(Integer)
2018-01-28 13:56:18 DEBUG queryCourseByStudentId:54 - ====>  Preparing: SELECT cos.*,sc.course_id FROM `course` cos,`student_course` sc WHERE cos.course_id = sc.course_id AND sc.student_id = ? 
2018-01-28 13:56:18 DEBUG queryCourseByStudentId:54 - ====> Parameters: 4(Integer)
2018-01-28 13:56:18 DEBUG queryCourseByStudentId:54 - <====      Total: 1
2018-01-28 13:56:18 DEBUG queryStudentById:54 - <==      Total: 1

2.MyBatis的事务控制

2.1.两种方式

MyBatis的事务控制有两种方式:JdbcTransactionManagedTransaction。参考MyBatis的配置文件

    <!--mybatis的数据库连接-->
    <environments default="dev">
        <environment id="dev">
            <!--事务管理-->
            <transactionManager type="JDBC"></transactionManager>
            <!--数据源信息-->
            <dataSource type="POOLED">
                <!-- 数据源信息 -->
            </dataSource>
        </environment>
    </environments>

其中transactionManager可以配置成JDBCMANAGED
- JDBC:利用SqlSession完成对事务的提交commit()、回滚rollback()、关闭close()等.
- MANAGED:自身不会去实现事务管理,而是让程序的容器如(JBOSS,Weblogic)来实现对事务的管理.

本章主要记录JDBC的实例代码。

2.2.JDBC事务控制模式

JdbcTransaction的主要方法有:
- 手动事务控制:由程序猿控制事务的提交commit()、回滚rollback()、关闭close()等.
- 自动事务控制:由程序本身控制事务,执行一句,提交一次,不能保证事务原子性。
手动事务控制实例(不推荐):

//创建手动事务控制会话:执行一句,提交一次
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//增删改查

自动事务控制实例(推荐):

//创建数据库会话
SqlSession sqlSession = sqlSessionFactory.openSession();
try{
    //增删改查
    ...
    //事务提交
    sqlSession.commit();
}catch (Exception e){
    //事务回滚
    sqlSession.rollback();
}finally {
    //关闭连接
    sqlSession.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值