mysql有几种引擎,当使用InnoDB的时候,才可以进行事务处理,在navicat中如下设置:
1:进入表设计页面,选项按钮页面
2:修改数据库引擎为InnoDB
3:mybatis进行事务处理的代码如下:
package com.better517na.LogCollection.dao.impl;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import com.better517na.LogCollection.dao.WriteACCindexDao;
import com.better517na.LogCollection.model.MLogAcc;
import com.better517na.LogCollection.service.base.LoadSpring;
import com.better517na.LogCollection.util.SqlUtil;
import com.better517na.logcompontent.model.MLogException;
import com.better517na.logcompontent.util.ExceptionLevel;
public class WriteACCindexDaoImpl implements WriteACCindexDao {
@Override
public boolean writeACClogToDB(List<MLogAcc> mLogAccs) {
int result = -1;
TransactionFactory transactionFactory = new JdbcTransactionFactory(); // 事务工厂
SqlSession session = null;
Transaction newTransaction = null;
try {
session = SqlUtil.getAccSqlSessionFactory().openSession();
newTransaction = transactionFactory.newTransaction(session
.getConnection());
Map<String, Object> map = new HashMap<String, Object>();
map.put("logAccList", mLogAccs);
map.put("tableName", mLogAccs.get(0).getTableName());
result = session.insert("writeACClogToDB", map);
} catch (Exception e) {
try {
newTransaction.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
LoadSpring.getLogBusiness().writeExceptionLog(
new MLogException(ExceptionLevel.Error, "123", e));
return false;
} finally {
try {
newTransaction.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result > 0 ? true : false;
}
}
4:mybatis的mapper.xml文件如下:
<p><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "<a target=_blank href="http://mybatis.org/dtd/mybatis-3-mapper.dtd">http://mybatis.org/dtd/mybatis-3-mapper.dtd</a>">
<!-- 这是mybatis的sql映射文件 -->
<mapper namespace="com.better517na.LogCollection.dao.WriteACCindexDao"></p><p> <insert id="writeACClogToDB" parameterType="Map">
INSERT INTO
${tableName}(
LogID,
TimeTicks,
TrackID,
TimePoint,
TimePeriod,
Sequence,
Direction,
LocalIP,
MachineName,
ServiceAddress,
AppName,
Method,
Description,
ExceptionID,
ExceptionLevel,
Paras,
ReturnValue,
Key1,
Key2,
Key3
)
values
<foreach collection="logAccList" item="item" index="index" separator=",">
(
#{item.logID},
#{item.timeTicks},
#{item.trackID},
#{item.timePoint},
#{item.timePeriod},
#{item.sequence},
#{item.direction},
#{item.localIP},
#{item.machineName},
#{item.serviceAddress},
#{item.appName},
#{item.method},
#{item.description},
#{item.exceptionID},
#{item.exceptionLevel},
#{item.paras},
#{item.returnValue},
#{item.key1},
#{item.key2},
#{item.key3})
</foreach>
</insert></p><p></mapper></p>