Mybatis核心接口和类

MyBatis核心接口和类

三大对象:
1、SqISessionFactoryBuilder: 负责构建SqlSessionFactory,并且提供了多个build( )方法的重载
2、SqlSessionFactory: 创建SqlSession实例的工厂
3、SqlSession: 用于执行持久化操作的对象

三大对象获取的步骤:

(1)每个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心。

(2)首先获取SqlSessionFactoryBuilder对象,可以根据XML配置文件的实例构建该对象。

(3)然后获取SqlSessionFactory对象,该对象实例可以通过SqlSessionFactoryBuilder对象来获得。

(4)有了SqlSessionFactory对象之后,通过SqlSessionFactory对象的openSession()方法就可以获取SqlSession实例,SqlSession对象中完全包含以数据库为背景的所有执行SQL操作的方法。

三大对象的声明周期和作用域:

《1》SqlSessionFactoryBuilder的最大特点是:用过即丢。一旦创建了SqlSessionFactoryBuilder对象之后,这个类就不再需要存在了,因此SqlSessionFactoryBuilder的最佳范围是存在方法体内,也就是局部变量而已。

《2》SqlSessionFactory对象一旦创建,就会在整个应用运行过程中始终存在,因此SqlSessionFactory的最佳作用域是Application(单例模式)。

《3》SqlSession对应着一次数据库回话。在每次访问数据库时都需要创建它,每个线程都有自己的SqlSession实例,SqlSession实例不能被共享,也不是线程安全的。因此最佳的作用域范围是request作用域或者方法体作用域内。
在这里插入图片描述

MyBatis是一款开源的数据持久化框架,它可以将Java对象和关系数据库之间的映射关系配置在XML文件中,通过SQL语句实现数据库的增删改查操作MyBatis框架是基于Java的持久化框架,它为开发者提供了灵活、高效、可靠的数据访问解决方案。 MyBatis框架的优点主要包括: 1. 灵活性高:MyBatis框架可以根据实际需要,通过SQL语句来实现多种复杂的数据操作。 2. 易于学习和使用:MyBatis框架的API和配置文件非常简单易懂,开发者可以很快上手。 3. 可扩展性好:MyBatis框架允许开发者自定义插件和型处理器,以满足不同的业务需求。 4. 性能高:MyBatis框架可以通过缓存和批处理等技术,提高数据访问的性能和效率。 Mybatis核心接口主要包括: 1. SqlSession:MyBatis框架的核心接口用于执行SQL语句和管理事务。 2. SqlSessionFactory:MyBatis框架的核心,创建SqlSession对象。 3. Configuration:MyBatis框架的核心配置用于加载MyBatis的配置文件。 下面是一个简单的MyBatis Demo案例的实现步骤: 1. 创建数据库表和实体:首先需要在数据库中创建一个表,例如student表,并创建一个对应的Java实体Student。 2. 创建MyBatis的配置文件:在src目录下创建mybatis-config.xml配置文件,配置MyBatis的相关信息,例如数据库连接信息、映射器(Mapper)等。 3. 创建Mapper接口和Mapper映射文件:创建一个StudentMapper接口和一个对应的StudentMapper.xml文件,用于定义SQL语句和映射关系。 4. 测试MyBatis框架:创建一个测试,在测试中通过SqlSession对象执行SQL语句,实现数据库的增删改查操作。 下面是一个简单的MyBatis Demo案例的示例代码: 1. 创建数据库表和实体 ```sql CREATE TABLE student ( id INT(11) PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50), age INT(11) ); public class Student { private int id; private String name; private int age; // 省略getter和setter方法 } ``` 2. 创建MyBatis的配置文件 ```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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/StudentMapper.xml"/> </mappers> </configuration> ``` 3. 创建Mapper接口和Mapper映射文件 ```java public interface StudentMapper { List<Student> findAll(); Student findById(int id); void insert(Student student); void update(Student student); void deleteById(int id); } ``` ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.StudentMapper"> <select id="findAll" resultType="com.example.Student"> select * from student </select> <select id="findById" resultType="com.example.Student"> select * from student where id = #{id} </select> <insert id="insert"> insert into student(name, age) values(#{name}, #{age}) </insert> <update id="update"> update student set name = #{name}, age = #{age} where id = #{id} </update> <delete id="deleteById"> delete from student where id = #{id} </delete> </mapper> ``` 4. 测试MyBatis框架 ```java public class Main { public static void main(String[] args) { SqlSession sqlSession = null; try { // 创建SqlSessionFactory对象 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 创建SqlSession对象 sqlSession = sqlSessionFactory.openSession(); // 获取Mapper接口的代理对象 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); // 查询所有学生 List<Student> studentList = studentMapper.findAll(); for (Student student : studentList) { System.out.println(student.getId() + ", " + student.getName() + ", " + student.getAge()); } // 根据ID查询学生 Student student = studentMapper.findById(1); System.out.println(student.getId() + ", " + student.getName() + ", " + student.getAge()); // 新增学生 Student newStudent = new Student(); newStudent.setName("李四"); newStudent.setAge(20); studentMapper.insert(newStudent); sqlSession.commit(); // 修改学生信息 student.setName("王五"); student.setAge(25); studentMapper.update(student); sqlSession.commit(); // 根据ID删除学生 studentMapper.deleteById(1); sqlSession.commit(); } catch (IOException e) { e.printStackTrace(); } finally { if (sqlSession != null) { sqlSession.close(); } } } } ``` 在上述示例中,我们首先创建了一个StudentMapper接口和一个对应的StudentMapper.xml文件,用于定义SQL语句和映射关系。然后,在测试Main中,通过SqlSessionFactory对象创建SqlSession对象,并获取StudentMapper接口的代理对象,通过该对象执行SQL语句,实现数据库的增删改查操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值