mybatis框架搭建

1.myBatis框架搭建
导入jar包
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
mybatis-3.2.2.jar
mysql-connector-java-5.0.8-bin.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar

jar包链接
2.创建配置文件
database.properties

driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/mybatis?characterEncoding\=utf-8
user=root
password=root

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="database.properties" />
	<!-- 定义别名 -->
	<typeAliases>
		<!-- <typeAlias type="com.hfxt.entity.User"/> -->
		<package name="com.hfxt.entity"/>
	</typeAliases>
    <!--环境配置,连接的数据库,这里使用的是MySQL-->
    <environments default="mysql">
    
        <environment id="mysql">
            <!--指定事务管理的类型,这里简单使用Java的JDBC的提交和回滚设置-->
            <transactionManager type="JDBC"></transactionManager>
            <!--dataSource 指连接源配置,POOLED是JDBC连接对象的数据源连接池的实现-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"></property>
                <property name="url" value="${url}"></property>
                <property name="username" value="${user}"></property>
                <property name="password" value="${password}"></property>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!--这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名,
            如果在某包下,则要写明路径,如:com/mybatistest/config/User.xml-->
        <mapper resource="com/hfxt/dao/UserDaoMapper.xml"></mapper>
        <mapper resource="com/hfxt/dao/RoleDaoMapper.xml"></mapper>
    </mappers>
</configuration>

3.添加工具类
MyBatisUtil.java

import java.io.Reader;

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 MyBatisUtil {
	private MyBatisUtil(){
	}
	private static final String RESOURCE = "mybatis-config.xml";
	private static SqlSessionFactory sqlSessionFactory = null;
	private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
	static {
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(RESOURCE);
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			sqlSessionFactory = builder.build(reader);
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new ExceptionInInitializerError("初始化MyBatis错误,请检查配置文件或数据库");
			
		}
	}
	public static SqlSessionFactory getSqlSessionFactory(){
		return sqlSessionFactory;
	}
	public static SqlSession getSession(){
		//sessionTL的get()方法根据当前线程返回其对应的线程内部变量,
		//也就是我们需要的Session,多线程情况下共享数据库链接是不安全的。
		//ThreadLocal保证了每个线程都有自己的Session。
		SqlSession session = threadLocal.get();
		// 如果session为null,则打开一个新的session
		if (session == null){
			session = (sqlSessionFactory !=null) ?sqlSessionFactory.openSession():null;
			threadLocal.set(session); // 5
		}
		return session;
	}
	public static void closeSession(){
		SqlSession session = (SqlSession) threadLocal.get(); // 2
		threadLocal.set(null);
		if (session !=null){
			session.close();
		}
	}
}

4.开始创建实体类操作

你可能感兴趣的:(mybatis框架搭建)