目录
1.3:在resources文件下创建mybatis核心配置文件
2.2通过SqlSessionFactoryBuild获取Builder对象
2.3通过sqlSessionFactory获取Factory对象
基本要求:
现有一个名为mydb的数据库,其中有表students,表结构如下所示
要求使用mabatis连接数据库,并对表students进行操作
一:Mybatis的部署
1.1:创建基本maven项目
1.2:在maven仓库中心下载并部署插件
mybatis,junit(负责Test注解),mysql(负责驱动),复制好三个插件的坐标并导入到maven中的pom.xml文件中。maven仓库链接如下:https://mvnrepository.com/?__cf_chl_tk=dtEofd0fyXRfvxLbR9wwT6R91jfOHiJrwoX6B7TiE30-1689392760-0-gaNycGzNCzs
本例使用其版本如下
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>
坐标填写完毕后点击箭头所指的重建maven项目按钮即可
1.3:在resources文件下创建mybatis核心配置文件
在如图的resources文件下创建名为“mybatis-config.xml”的配置文件
添加箭头所指方框内内容
1.4:在main文件内创建对应mapper接口
mapper接口用于实现其对数据库的操作方法,因此需要在mapper接口中定义其将要操作的方法,例如我们想要查询id等于1的学生的信息,样例如下:先创建一个Stu接口,在接口内做如下定义
观察代码可知,Stu接口负责返回一个数据库中的对应对象,因此要想转为java中的对象,我们需要在main文件中定义一个实体类student,用于返回其查询值。。。。。在student类中,将属性名和数据库中的字段名保持一致,并将其访问权限设为私有,设置getter和setter方法,和toString方法。
1.5:创建映射文件
在mybatis中,mapper接口不需要类来实现,而是通过定义的xml配置文件的映射来实现其方法。其步骤如下,首先在resources文件中创建一个mappers文件夹,用来存放后面所创建的映射文件,然后在mappers中创建xml文件。此处命名建议与其接口名一致。创建后访问mybatis官方文档,查看其映射文件的配置内容,并将其复制到你所创建的映射文件中,其内容如下
<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>
此处需要注意的三点:
尖括号namespace中的内容要提换成你的相对应接口的路径。
select中的id对应的是接口中定义的方法名称。
若接口中的返回类型是自定义类,则需要在resultType中填上其类对应的路径
演示如下:
<?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="mapper.Stu">
<select id="selectById" resultType="role.Student">
select * from students where id = #{id}
</select>
</mapper>
配置完成后,此时回想我们在创建mybatis核心配置文件时有一句
<mappers>
<mapper resource="mappers/Stu.xml"/>
</mappers>
这里mappers中所填的内容就是目前我们所使用的的映射文件,因此我们要导入我们定义的映射文件的路径。注意:由于我们是在resources文件目录中定义的子文件中的一个文件,,因此不能像在java文件中用.来访问路径,所以我们要用斜杠 ‘/’ 来写我们的文件路径。此处我们这样填写
二:创建Tese方法进行测试
2.1通过字节流加载核心配置文件
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
2.2通过SqlSessionFactoryBuild获取Builder对象
SqlSessionFactoryBuilder Builder=new SqlSessionFactoryBuilder();
2.3通过sqlSessionFactory获取Factory对象
build内填入我们上面所创建的字节流
SqlSessionFactory Factory= Builder.build(is);
2.4获取SqlSession对象
此处填为true是使其能够自动提交事务,默认为false
SqlSession sqlSession=Factory.openSession(true);
2.5获取mapper对象
Stu mapper=sqlSession.getMapper(Stu.class);
此处因为Stu是接口,我们通过sqlSession中的getMapper方法使其实例化
测试代码如下:
public class Try {
@Test
public void Trry() throws IOException {
//通过字节输入流来加载核心配置文件
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
//获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder Builder=new SqlSessionFactoryBuilder();
//获取sqlSessionFactory对象
SqlSessionFactory Factory= Builder.build(is);
//获取SqlSession对象
SqlSession sqlSession=Factory.openSession(true);
//获取mapper对象
Stu mapper=sqlSession.getMapper(Stu.class);
Student a=mapper.selectById(1);//查询id为1的学生信息
System.out.println(a);
}
}
测试结果如下:![](https://img-blog.csdnimg.cn/3473993a3e204780a07ec7cde5643eb9.png)