mybatis-210716-02—入门—接口式编程
mybatis文档-入门,官网网址:
https://mybatis.org/mybatis-3/zh/getting-started.html
接口式编程
根据 mybatis-210716-02—入门—hello 代码进行修改
1. 接口式编程
原生: Dao -----> DaoImpl
mybatis: Mapper -----> xxxMapper.xml
2. SqlSession代表和数据库的一次会话,用完必须关闭
3. SqlSession和Connection一样,都是非线程安全,每次使用都应该去获取新的对象。
4. mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。(将接口和xml进行绑定)
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);
5. 两个重要配置文件
mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境。
sql映射文件:保存了每一条sql语句的映射信息。
EmployeeMapper.java
package com.bgy.dao;
import com.bgy.bean.Employee;
public interface EmployeeMapper {
// 这个接口就是用来查出Employee,记录数据并封装成Employee对象
public Employee getEmpById(Integer id);
}
EmployeeMapper.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">
<!--
namespace:名称空间;指定为接口的全类名
-->
<mapper namespace="com.bgy.dao.EmployeeMapper">
<!--
id:唯一标识
resultType:返回值对象
#{id}:从传递过来的参数中取出id值
-->
<!--
数据库中tbl_employee中字段是 id,last_name,gender,email
而JavaBean中的Employee.java是 id,lastName,gender,email
所以要起别名。
-->
<!--
这个接口就是用来查出Employee,记录数据并封装成Employee对象
public Employee getEmpById(Integer id);
-->
<select id="getEmpById" resultType="com.bgy.bean.Employee">
select
id,last_name as lastName,gender,email
from
tbl_employee
where
id = #{id}
</select>
</mapper>
MyBatisTest.java
package com.bgy.bean.test;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.jupiter.api.Test;
import com.bgy.bean.Employee;
import com.bgy.dao.EmployeeMapper;
class MyBatisTest {
// 给sqlSessionFactory封装成一个方法+
public SqlSessionFactory getSqlSessionFactort() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1. 根据xml位置文件(全局配置文件)创建一个SQLSessionFactory对象
* 让数据源有运行环境信息
* 2. sql映射文件;配置了每一个sql,以及sql的封装规则等
* 3. sql映射文件注册到全局配置文件中
* 4. 写代码
* 1)根据全局配置文件得到SqlSessionFactory
* 2)使用sqlSession工厂,获取到sqlSession对象,使用它来执行增删改查。
* 一个sqlSession就是代表和数据库的一次会话,用完关闭。
* 3)使用sql的唯一标识来告诉mybatis执行那个sql。sql都是保存在sql映射文件中。
* @throws IOException
*/
@Test
void test() throws IOException {
// String resource = "mybatis-config.xml";
// InputStream inputStream = Resources.getResourceAsStream(resource);
// SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 1. 获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactort();
// 2. 获取sqlSession实例,能直接执行已经映射的sql语句。
SqlSession session = sqlSessionFactory.openSession();
try {
// session.selectOne(statement, parameter)
// statement sql的唯一标识,使用 名称空间+唯一标识 的形式
// parameter 执行sql要用的参数
Employee employee =(Employee) session.selectOne("com.bgy.EmployeeMapper.selectEmployee", 1);
System.out.println(employee);
} finally {
session.close();
}
}
/**
* 接口式编程
* 推荐使用
* @throws IOException
*/
@Test
public void test01() throws IOException {
// 1. 获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactort();
// 2. 获取sqlSession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3. 获取接口实现类对象
// 会为接口自动创建一个代理对象,代理对象去执行增删改查等方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println("接口式编程");
System.out.println(employee);
} finally {
openSession.close();
}
}
}