目录
mybatis框架
1.JDBC编程及JDBC存在问题
2.mybatis框架介绍
3.mybatis的使用举例
3.1 通过maven管理依赖(pom.xml)
3.2 全局配置文件(mybatis-config.xml)
3.3 pojo中的Student类
3.4 mapper中的接口文件(studentmapper.java)
3.5 mapper的配置文件(studentmapper.xml)
3.6 修改全局配置文件,映射表映射文件
3.7 mybatis接口调用编码
总结:mybatis使用流程
mybatis框架
1.JDBC编程及JDBC存在问题
存在问题:
1、使用JDBC操作数据库前进行连接,操作完成后关闭连接,并发量大的情况下影响性能
解决方法:连接池
2、SQL语句硬编码在Java代码中,需求改变时,需要更改java代码本身
解决方法:将SQL语句放在配置文件中(xml形式),需求发生改变时,只需要修改配置文件即可
3、返回结果集存在硬编码
解决方法:将数据库中的数据集映射为Java对象
2.mybatis框架介绍
1、MyBatis 是一款优秀的持久层框架,前身是ibatis,apchche旗下的数据库持久层框架;
2、它支持自定义 SQL、存储过程以及高级映射;
3、MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
4、MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录。
1、MyBatis 是一款优秀的持久层框架,前身是ibatis,apchche旗下的数据库持久层框架;
2、它支持自定义 SQL、存储过程以及高级映射;
3、MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
4、MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录。
框架原理:
3.mybatis的使用举例
3.1 通过maven管理依赖(pom.xml)
org.mybatis
mybatis
3.4.1
3.2 全局配置文件(mybatis-config.xml)
3.3 pojo中的Student类
public class Student {
private Integer SID;
private String Sname;
private String Ssex;
private Integer Sage;
//getter和setter方法省略
}
3.4 mapper中的接口文件(studentmapper.java)
public interface StudentMapper {
//通过id来查询某一个学生信息
public Student selectStudentById(Integer id);
}
3.5 mapper的配置文件(studentmapper.xml)
3.6 修改全局配置文件,映射表映射文件
3.7 mybatis接口调用编码
//指定全局配置文件路径
String resource = "mybatis-config.xml";
//通过mybatis提供的Resource读取文件
InputStream stream = null;
stream = Resources.getResourceAsStream(resource);
//通过SQLSessionFactoryBuilder创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentById(2);
System.out.println(student);
总结:mybatis使用流程
org.mybatis
mybatis
3.4.1
3.2 全局配置文件(mybatis-config.xml)
3.3 pojo中的Student类
public class Student {
private Integer SID;
private String Sname;
private String Ssex;
private Integer Sage;
//getter和setter方法省略
}
3.4 mapper中的接口文件(studentmapper.java)
public interface StudentMapper {
//通过id来查询某一个学生信息
public Student selectStudentById(Integer id);
}
3.5 mapper的配置文件(studentmapper.xml)
3.6 修改全局配置文件,映射表映射文件
3.7 mybatis接口调用编码
//指定全局配置文件路径
String resource = "mybatis-config.xml";
//通过mybatis提供的Resource读取文件
InputStream stream = null;
stream = Resources.getResourceAsStream(resource);
//通过SQLSessionFactoryBuilder创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentById(2);
System.out.println(student);
总结:mybatis使用流程
public class Student {
private Integer SID;
private String Sname;
private String Ssex;
private Integer Sage;
//getter和setter方法省略
}
3.4 mapper中的接口文件(studentmapper.java)
public interface StudentMapper {
//通过id来查询某一个学生信息
public Student selectStudentById(Integer id);
}
3.5 mapper的配置文件(studentmapper.xml)
3.6 修改全局配置文件,映射表映射文件
3.7 mybatis接口调用编码
//指定全局配置文件路径
String resource = "mybatis-config.xml";
//通过mybatis提供的Resource读取文件
InputStream stream = null;
stream = Resources.getResourceAsStream(resource);
//通过SQLSessionFactoryBuilder创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentById(2);
System.out.println(student);
总结:mybatis使用流程
3.6 修改全局配置文件,映射表映射文件
3.7 mybatis接口调用编码
//指定全局配置文件路径
String resource = "mybatis-config.xml";
//通过mybatis提供的Resource读取文件
InputStream stream = null;
stream = Resources.getResourceAsStream(resource);
//通过SQLSessionFactoryBuilder创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectStudentById(2);
System.out.println(student);
总结:mybatis使用流程
//指定全局配置文件路径 String resource = "mybatis-config.xml"; //通过mybatis提供的Resource读取文件 InputStream stream = null; stream = Resources.getResourceAsStream(resource); //通过SQLSessionFactoryBuilder创建会话工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream); //获取会话 SqlSession sqlSession = sqlSessionFactory.openSession(); //通过接口获取对象实例 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); Student student = studentMapper.selectStudentById(2); System.out.println(student);
总结:mybatis使用流程
1、创建全局配置文件(数据源等、引入映射文件);
2、表的pojo类、mapper接口文件、mapper配置文件;
3、通过SqlSessionFactoryBuilder读取配置创建SqlSessionFactory,继而创建SQLSession,在调用方法。
编程详解:SqlSessionFactoryBuilder,用来创建sqlSessionFactory会话工厂
SqlSessionFactory:会话工厂,通过读取配置文件创建、配置参数,对文件读取一次即可,可以将SqlSessionFactory设置为单例模式,通过SqlSessionFactory创建会话
SqlSession:会话:
对数据库的增删改查操作都是通过会话操作,sqlSession是线程不安全的,将会话设置为局部变量
一级缓存机制就是sqlSession会话级别的缓存,mybatis默认是开启一级缓存
目录结构如下:



