ORM:
- mybatis 是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身,而不需要花费精力去处理加载驱动、创建连接、创建statement等繁杂的过程。
- mybatis通过xml或注解的方式将要执行的各种 statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。
- 最后mybatis框架执行sql并将结果映射为java对象并返回。采用ORM思想解决了实体和数据库映射的问题,对jdbc 进行了封装,屏蔽了jdbc api 底层访问细节,使我们不用与jdbc api 打交道,就可以完成对数据库的持久化操作。
- MyBatis官网地址:http://www.mybatis.org/mybatis-3/
- 框架:是一款半成品软件,可以基于这个半成品软件继续开发,来完成个性化的需求,这个框架不会完成所有的事情,因为需求不同,但是可以完成很多需求相似的工作。
原始jdbc操作的分析:
ORM(Object Relational Mapping): 对象关系映射
指的是持久化数据和实体对象的映射模式,为了解决面向对象与关系型数据库存在的互不匹配的现象的技术。
- 频繁创建和销毁数据库的连接会造成系统资源浪费从而影响系统性能。
- sql 语句在代码中硬编码,如果要修改 sql 语句,就需要修改 java 代码,造成代码不易维护。
- 查询操作时,需要手动将结果集中的数据封装到实体对象中。
- 增删改查操作需要参数时,需要手动将实体对象的数据设置到 sql 语句的占位符。
原始 JDBC 的操作问题解决方案
LOG4J的配置和使用
- 使用数据库连接池初始化连接资源。
- 将 sql 语句抽取到配置文件中。
- 使用反射、内省等底层技术,将实体与表进行属性与字段的自动映射
三层架构:在日常开发过程中,排查问题时难免需要输出 MyBatis 真正执行的 SQL 语句、参数、结果等信息,我们就可以借助 LOG4J 的功能来实现执行信息的输出。
在配置好LOG4J后需要关注控制台的三个地方:
1.repairing:当前功能所执行的SQL语句
2.Parameters:当前功能所执行的SQL语句的参数及类型
3. Total:执行结果的数量
分层思想:控制层(controller)、业务层(service)、持久层(dao)
调用流程:
控制层(controller)、业务层(service)、持久层(dao)、数据库
优点:
MyBatis入门程序:专注一层实现、标准化、复用性、
MyBatis开发步骤:
- 添加MyBatis的jar包
- 创建Student数据表
- 编写Student实体类
public class Student {
private Integer id;
private String name;
private Integer age;
}
- 编写映射文件StudentMapper.xml
- 编写核心文件MyBatisConfig.xml
核心配置文件的数据类型也是可以起别名的,mybatis已经帮忙实现好了,直接用对应的别名就可以了
- log4j配置文件
在日常开发过程中,排查问题时难免需要输出 MyBatis 真正执行的 SQL 语句、参数、结果等信息,我们就可以借助 LOG4J 的功能来实现执行信息的输出。
# Global logging configuration # 这是log4j的级别,依次为:ERROR WARN INFO DEBUG log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=5 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
jdbc配置文件:
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://127.0.0.1/db4 username=root password=itzhuzhu
- 编写测试类
public class StudentTest {
// 查询全部
@Test
public void selectAll() throws IOException {
// 加载核心配置文件
InputStream rs = Resources.getResourceAsStream("MybatisConfig.xml");
// InputStream rs = StudentTest.class.getClassLoader().getResourceAsStream("MybatisConfig.xml");
// 获取SqlSession工厂对象
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
// 通过获取SqlSession工厂对象获取SqlSession对象
SqlSession sqlSession = build.openSession();
// 执行映射配置文件中的sql语句,并接收结果
List list = sqlSession.selectList("StudentMapper.selectAll");
// 处理结果
for (Student student : list) {
System.out.println(student);
}
// 释放资源
sqlSession.close();
rs.close();
}
// 根据id查询
@Test
public void selectById() throws IOException {
InputStream rs = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
SqlSession sqlSession = build.openSession();
Student student = sqlSession.selectOne("StudentMapper.selectById", 2);
System.out.println(student);
sqlSession.close();
rs.close();
}
// 新增数据
@Test
public void insert() throws IOException {
InputStream rs = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
SqlSession sqlSession = build.openSession(true);
Student s = new Student(5, "憨憨韩信", 29);
Student student = sqlSession.selectOne("StudentMapper.insert", s);
// sqlSession.commit();
System.out.println(student);
sqlSession.close();
rs.close();
}
// 修改数据
@Test
public void update() throws IOException {
InputStream rs = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
SqlSession sqlSession = build.openSession(true);
Student s = new Student(5, "憨憨李白", 39);
int update = sqlSession.update("StudentMapper.update", s);
System.out.println(update);
sqlSession.close();
rs.close();
}
// 删除数据
@Test
public void delete() throws IOException {
InputStream rs = Resources.getResourceAsStream("MybatisConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);
SqlSession sqlSession = build.openSession(true);
int delete = sqlSession.delete("StudentMapper.delete",5);
System.out.println(delete);
sqlSession.close();
rs.close();
}
}
MyBatis的相关api:
Resources:
- org.apache.ibatis.io.Resources:加载资源的工具类。
- 和.class.getClassLoader().getResourceAsStream是一样的作用
| 返回值 | 方法名 | 说明 |
|---|---|---|
| InputStream | getResourceAsSteam(String fileName) | 通过类加载器返回指定资源的字节输出流 |
- org.apache.ibatis.session.SqlSessionFactoryBuilder:获取 SqlSessionFactory 工厂对象的功能类
- 通过加载mybatis的核心文件的输入流的形式构建一个SqlSessionFactory对象
| 返回值 | 方法名 | 说明 |
|---|---|---|
| SqlSessionFactory | build(InputStream is) | 通过指定资源字节输出流获取SqlSession工厂对象 |
String resource = "org/mybatis/builder/mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(inputStream);
其中, Resources 工具类,这个类在 org.apache.ibatis.io 包中。Resources 类帮助你从类路径下、文件系统或一个 web URL 中加载资源文件。
工厂对象SqlSessionFactory:- org.apache.ibatis.session.SqlSessionFactory:获取 SqlSession 构建者对象的工厂接口。
| 返回值 | 方法名 | 说明 |
|---|---|---|
| SqlSession | openSession() | 获取SqlSession构建者对象,并开启手动提交事务 |
| SqlSession | openSession(Boolean antoCommit) | 获取SqlSession构建者对象,如果参数为true,则开启自动提交事务 |
- org.apache.ibatis.session.SqlSession:构建者对象接口。用于执行 SQL、管理事务、接口代理。
SqlSession 实例在 MyBatis 中是非常强大的一个类。在这里你会看到所有执行语句、提交或回滚事务和获取映射器实例的方法。



