Mybatis(0)Mybatis调用mysql数据库的过程

基本步骤

1. 确定要使用的Mybatis配置文件

//1.设置resource的名称,文件名是什么,这里的名称就是什么【确定要使用的Mybatis配置文件】
String resource = "mybatisConfig.xml";

2. 将Mybatis配置文件转化为输入流

		//2. 输入流,涉及文件的读写
		//1、2是固定写法,是一种组合,定义配置文件,以流的形式读写
		Reader reader = null;
		try {
			//对于简单的只读文本数据,加载为 Reader输入流【将Mybatis配置文件转化为输入流】
			reader = Resources.getResourceAsReader(resource);//输入流,涉及文件的读写
		} catch (IOException e) {
			e.printStackTrace();
		}

3.创建数据库会话工厂类,将xml的输入流封装为一个工厂,作用是创建SqlSession

		//3. SqlSession工厂类,作用是创建SqlSession,是线程安全的【创建数据库会话工厂类,将xml的输入流封装为一个工厂,作用是创建SqlSession】
		//SqlSessionFactoryBuilder ​​有五个 ​build()​ 方法,每一种都允许你从不同的资源中创建一个 ​​SqlSessionFactory ​​实例。
		/*  SqlSessionFactory build(InputStream inputStream)
			SqlSessionFactory build(InputStream inputStream, String environment)
			SqlSessionFactory build(InputStream inputStream, Properties properties)
			SqlSessionFactory build(InputStream inputStream, String env, Properties props)
			SqlSessionFactory build(Configuration config)
		*/
		//第一种方法是最常用的,它接受一个指向 XML 文件(也就是之前讨论的 mybatis的xml配置文件)的 ​​InputStream ​​实例。
		//可选的参数是 ​​environment ​​和 ​​properties​​。
		//​​environment ​​决定加载哪种环境,包括数据源和事务管理器。
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);//返回SqlSessionFactory【sql会话的工厂类】

4. 建立数据库连接(会话),借助了数据库会话工厂类

		//4. 得到一个session,session是一个会话【建立数据库连接(会话),借助了数据库会话工厂类】
		//是线程不安全的
		//作用是执行持久层操作,即,和数据库进行交互
		//openSession(boolean b)
		//默认为false,事务处理方式,关闭自动提交,
		//设为true,主动提交,在增删改的时候不要在commit
		SqlSession sqlSession = sqlSessionFactory.openSession();//返回一个【sql连接(会话)】,解耦

5. 使用数据库连接(会话)【有2种方式】

5.1 getMapper方法,获取映射器,参数是要实现的接口【自动生成当前接口的实现类,自动映射】
		//5.1getMapper,获取映射器,参数是要实现的接口【自动生成当前接口的实现类,自动映射】
		//底层采用动态代理 ,自动生成mapper接口(StudentDao)的实现类的代理对象。
		StudentDao stuDao=sqlSession.getMapper(StudentDao.class);//【用于创建Dao的代理对象,从而实现Dao操作功能】
  • 使用getMapper方法需要写dao.java接口,且方法名一定要存在,在dao.xml文件也需要进行声明
5.2 使用selectOne等方法
		//5.2使用selectOne等方法
		//第1个参数:找到resource文件映射的mapper1.xml文件中,id是searchByAgeAndGender2,对应的SQL语句
		//第2个参数:SQL语句占位符对应的参数
		Student u=new Student();
		u.setAge(20);
		u.setGender("女");
		Student stu1 = (Student)sqlSession.selectOne("searchByAgeAndGender5",u);//找到并返回mapper1.xml文件中,id是searchByAgeAndGender2的语句,并传递参数1给SQL语句
  • 注意:
    • 使用selectOne、selectList、insert、update、delete等方法可以不写dao.java接口文件,只需要在dao.xml文件中声明对应的id就可以使用
    • 使用insert、update、delete等方法
      • 需要设置sqlSessionFactory.openSession(true);【自动提交】
      • 或者设置sqlSession.commit()方法【手动提交】
      • 不然,数据不会提交

6. 关闭会话,释放资源

sqlSession.close();//关闭数据库会话,释放资源

用法全在注释里【这里就不写了】

package test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import dao.StudentDao;
import entity.BanJi;
import entity.Student;

public class Test0 {
	public static void main(String[] args) {
		//1.设置resource的名称,文件名是什么,这里的名称就是什么【确定要使用的Mybatis配置文件】
		String resource = "mybatisConfig.xml";
		//2. 输入流,涉及文件的读写
		//1、2是固定写法,是一种组合,定义配置文件,以流的形式读写
		Reader reader = null;
		try {
			//对于简单的只读文本数据,加载为 Reader输入流【将Mybatis配置文件转化为输入流】
			reader = Resources.getResourceAsReader(resource);//输入流,涉及文件的读写
		} catch (IOException e) {
			e.printStackTrace();
		}
		//3. SqlSession工厂类,作用是创建SqlSession,是线程安全的【创建数据库会话工厂类,将xml的输入流封装为一个工厂,作用是创建SqlSession】
		//SqlSessionFactoryBuilder ​​有五个 ​build()​ 方法,每一种都允许你从不同的资源中创建一个 ​​SqlSessionFactory ​​实例。
		/*  SqlSessionFactory build(InputStream inputStream)
			SqlSessionFactory build(InputStream inputStream, String environment)
			SqlSessionFactory build(InputStream inputStream, Properties properties)
			SqlSessionFactory build(InputStream inputStream, String env, Properties props)
			SqlSessionFactory build(Configuration config)
		*/
		//第一种方法是最常用的,它接受一个指向 XML 文件(也就是之前讨论的 mybatis的xml配置文件)的 ​​InputStream ​​实例。
		//可选的参数是 ​​environment ​​和 ​​properties​​。
		//​​environment ​​决定加载哪种环境,包括数据源和事务管理器。
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);//返回SqlSessionFactory【sql会话的工厂类】
		
		//4. 得到一个session,session是一个会话【建立数据库连接(会话),借助了数据库会话工厂类】
		//是线程不安全的
		//作用是执行持久层操作,即,和数据库进行交互
		//openSession(boolean b)
		//默认为false,事务处理方式,关闭自动提交,
		//设为true,主动提交,在增删改的时候不要在commit
		SqlSession sqlSession = sqlSessionFactory.openSession();//返回一个【sql连接(会话)】,解耦
				
		
		//5.1getMapper,获取映射器,参数是要实现的接口【自动生成当前接口的实现类,自动映射】
		//底层采用动态代理 ,自动生成mapper接口(StudentDao)的实现类的代理对象。
		StudentDao stuDao=sqlSession.getMapper(StudentDao.class);//【用于创建Dao的代理对象,从而实现Dao操作功能】
		
		//stuDao对象对应的是getMapper方法自动生成的StudentDao实现类
		//searchByAgeAndGender2是stuDao接口的方法,并传递参数
		//调用方法,底层自动对应mybatisConfig.xml指定的mapper文件中,
		Student stu2=stuDao.searchByAgeAndGender2(20,"女");
		
		//5.2使用selectOne等方法
		//第1个参数:找到resource文件映射的mapper1.xml文件中,id是searchByAgeAndGender2,对应的SQL语句
		//第2个参数:SQL语句占位符对应的参数
		Student u=new Student();
		u.setAge(20);
		u.setGender("女");
		Student stu1 = (Student)sqlSession.selectOne("searchByAgeAndGender5",u);//找到并返回mapper1.xml文件中,id是searchByAgeAndGender2的语句,并传递参数1给SQL语句
		
		System.out.println(stu1);
		System.out.println(stu2);
		sqlSession.close();//关闭数据库会话,释放资源
	}
}

运行结果

在这里插入图片描述
配置文件

<?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.cj.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost/school?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>

	<mappers>
		<mapper resource="dao/StudentDao.xml" />

	</mappers>
</configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值