总结一下工具类备用:
MybatisUtil:
package com.macw.util;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtils {
private static SqlSessionFactory factory = null;
private static ThreadLocal<SqlSession> tl = new ThreadLocal<SqlSession>();
static {
// 1 读取配置文件 config.xml
InputStream in = null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
// 2 创建SqlSessionFactory
factory = new SqlSessionFactoryBuilder().build(in);
}
public static SqlSession openSession() {
// 3 创建SqlSession
SqlSession sqlSession = tl.get();
if (sqlSession == null) {
sqlSession = factory.openSession();
tl.set(sqlSession);
}
return sqlSession;
}
public static void close(SqlSession sqlSession) {
if (sqlSession != null) {
tl.remove();
sqlSession.close();
}
}
}
所用到的mybatis-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<properties resource="druid.properties"></properties>
<!-- <settings>
打印SQL语句
<setting name="logImpl" value="LOG4J"/>
</settings> -->
<!-- 给包起别名 -->
<typeAliases>
<typeAlias type="com.macw.entity.User" alias="user"/>
<package name="com.macw.entity"/>
</typeAliases>
<environments default="oracle">
<environment id="oracle">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${DriverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<!-- <property name="initialSize" value="${initialSize}"/>
<property name="maxActive" value="${maxActive}"/>
<property name="maxWait" value="${maxWait}"/> -->
</dataSource>
</environment>
</environments>
<!-- mapper文件的映射 -->
<mappers>
<mapper resource="com/macw/dao/mapper/UserMapper.xml"/>
</mappers>
</configuration>
上面xml所用到的druid.properties文件:
连接Oracle数据库的配置文件
DriverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:xe
username=macw
password=macw
initialSize=10
maxActive=50
maxWait=60000
Mybatis工具类在ServiceImpl类下的使用:
package com.macw.service.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.macw.dao.OrderDao;
import com.macw.entity.Order;
import com.macw.service.OrderService;
import com.macw.util.MyBatisUtils;
/**
* @author 超伟
* @2019年7月16日 上午10:41:20
* @博客:https://blog.csdn.net/MacWx
*/
public class OrderServiceImpl implements OrderService {
private OrderDao dao;
/* (non-Javadoc)
* @see com.macw.service.OrderService#insertOrder(com.macw.entity.Order)
*/
@Override
public void insertOrder(Order order) {
// TODO Auto-generated method stub
SqlSession sqlSession = MyBatisUtils.openSession();
dao = sqlSession.getMapper(OrderDao.class);
try {
dao.insertOrder(order);
sqlSession.commit();
} catch (Exception e) {
// TODO: handle exception
sqlSession.rollback();
} finally {
MyBatisUtils.close(sqlSession);
}
}
}
log4j.properties:
log4j.rootLogger=DEBUG, stdout
# SqlMap logging configuration...
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG
log4j.logger.com.ibatis.common.util.StopWatch=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n