创建实体类、
1、导入依赖
mysql mysql-connector-java8.0.12 org.mybatis mybatis3.5.4 junit junit4.12 test
2、新建java目录下配置问价 mybatis-config
注意:此时idea读取不到这个文件,需要添加过滤配置 (pom
src/main/java ***.xml true src/main/resources ***.xml true
编写工具类mybatis-utils
//sqlSessionFactory --> sqlSession
public class MybatisUtils {
static SqlSessionFactory sqlSessionFactory = null;
static {
try {
//使用Mybatis第一步 :获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例.
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
注意:此时,获得sqlsession有重载方法,可以使用重载方法,
return sqlSessionFactory.openSession(true);
默认开启数据库
3、编写具体mapper映射语句
select * from Blog where id = #{id}
4、映射到xm'l文件中



