目录
3.2 全局配置文件(mybatis-config.xml)
3.4 mapper中的接口文件(studentmapper.java)
3.5 mapper的配置文件(studentmapper.xml)
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为数据库中的记录。
框架原理:
3.mybatis的使用举例
3.1 通过maven管理依赖(pom.xml)
<!--mybatis 依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
3.2 全局配置文件(mybatis-config.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">
<!--事务管理器 使用JDBC的事务管理器-->
<transactionManager type="JDBC"/>
<!--数据源 池类型的数据源-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
</configuration>
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)
<?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.jing.mapper.StudentMapper">
<!--
id:statementID,在同一个命名空间下具有唯一性,一般是对应接口中方法
parameterType:表示参数类型
resultType:指定返回类型
#{XXX}:表示占位符 ?
-->
<select id="selectStudentById" parameterType="int" resultType="com.jing.pojo.Student">
select * from student where SID = #{id}
</select>
</mapper>
3.6 修改全局配置文件,映射表映射文件
<!--映射文件引入-->
<mappers>
<!--resource方式: 指定单个映射文件-->
<mapper resource="mapper/studentmapper.xml"/>
</mappers>
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使用流程
1、创建全局配置文件(数据源等、引入映射文件);
2、表的pojo类、mapper接口文件、mapper配置文件;
3、通过SqlSessionFactoryBuilder读取配置创建SqlSessionFactory,继而创建SQLSession,在调用方法。
编程详解:
SqlSessionFactoryBuilder,用来创建sqlSessionFactory会话工厂
SqlSessionFactory:会话工厂,通过读取配置文件创建、配置参数,对文件读取一次即可,可以将SqlSessionFactory设置为单例模式,通过SqlSessionFactory创建会话
SqlSession:会话:
对数据库的增删改查操作都是通过会话操作,sqlSession是线程不安全的,将会话设置为局部变量
一级缓存机制就是sqlSession会话级别的缓存,mybatis默认是开启一级缓存
目录结构如下: