目录
1、mybatis框架
①JDBC编程及JDBC编程存在问题
②mybatis的介绍及框架原理
③mybatis的使用demo
④配置文件的介绍
⑤mybatis的使用方式
1.1、JDBC编程及JDBC存在问题
存在问题:
1、使用JDBC操作数据库前进行连接,操作完成后关闭连接,并发量大的情况下影响性能
解决:连接池
2、SQL语句硬编码在Java代码中,需求改变需要更改java代码本身
解决:将SQL语句放在配置文件中(xml形式),需求发送给变只需要修改配置文件即可
3、返回结果集存在硬编码
将数据库中的数据集映射为Java对象
2、mybatis框架介绍
2.1、MyBatis 是一款优秀的持久层框架,前身是ibatis,apchche旗下的数据库持久层框架
2.2、它支持自定义 SQL、存储过程以及高级映射。
2.3、MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。4、MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO为数据库中的记录。
框架原理:
3、mybatis的使用demo
3.1、通过maven管理依赖
<!--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.tulun.mapper.StudentMapper213">
<!--
id:statementID,在同一个命名空间下具有唯一性,一般是对应接口中方法
parameterType:表示参数类型
resultType:指定返回类型
#{XXX}:表示占位符 ?
-->
<select id="selectStudentById" parameterType="int" resultType="com.tulun.pojo.Student213">
select * from student where SID = #{id}
</select>
</mapper>
3.6、修改全局配置文件,映射表映射文件
<!--映射文件引入-->
<mappers>
<mapper resource="mapper/studentmapper213.xml"/>
</mappers>
3.7、mybatis接口调用编码
//指定全局配置文件路径
String resource = "mybatis-config.xml";
//通过mybatis提供的Resource读取文件
InputStream stream = Resources.getResourceAsStream(resource);
//通过SQLSessionFactoryBuilder创建会话工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
//获取会话
SqlSession sqlSession = sqlSessionFactory.openSession();
//通过接口获取对象实例
StudentMapper213 studentMapper213 = sqlSession.getMapper(StudentMapper213.class);
Student213 student213 = studentMapper213.selectStudentById(1);
3.8、mybatis使用流程:
1、创建全局配置文件(数据源等、引入映射文件)
2、表的pojo类、mapper接口文件、mapper配置文件
3、通过SqlSessionFactoryBuilder读取配置创建SqlSessionFactory,继而创建SQLSession,在调用方法
目录结构如下:
4、mybatis中的配置文件讲解
4.1、全局配置文件
全部配置如下:
4.1.1、properties:读取外部资源
例如:
<properties resource="db.properties">
<property name="userName" value="root"/>
<property name="password" value="1234"/>
</properties>
读取配置位置:
<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>
4.1.2、setting:全局的参数配置
例如:
<settings>
<!--开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
4.1.3、typeAliases:类型别名
类型别名是给设定一个较短的名字,他是和XML配置有关,存在的意义是减少全限定名的冗余
<typeAliases>
<!--
给单个类取别名
type属性:给定pojo类的全路径
alias属性:给定的别名 可以随便给定,一般建议是pojo类的首字母小写的类型
-->
<typeAlias type="com.tulun.pojo.Student213" alias="student"/>
<!--批量的取别名
name属性给定的是要批量取别名的包路径
默认的别名是包下的类名首字母小写,其他不变
-->
<package name="com.tulun.pojo"/>
</typeAliases>
4.1.4、environments:环境变量
<!--配置数据源-->
<environments default="online">
<!--环境配置,可以配置多个环境-->
<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="${userName}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
4.1.5、mappers:映射器
定义的SQL语句的配置文件,需要告诉mybatis如何找到这些SQL
主要提供了三种形式
<!--映射文件引入-->
<mappers>
<!--resource方式: 指定单个映射文件,-->
<mapper resource="mapper/studentmapper213.xml"/>
<!--class方法:通过mapper的接口文件来加载单个映射文件,将mapper接口文件和mapper.xml放在同一个目录下,且名称相同-->
<mapper class="com.tulun.mapper.StudentMapper213"/>
<!--package方式:批量加载映射文件,将mapper接口文件和mapper.xml放在同一个目录下,且名称相同-->
<package name="com.tulun.mapper"/>
</mappers>
4.2、mapper.xml配置文件解析
4.2.1、查询操作:
<!--
resultMap:显性指定映射关系
id属性:取名字
type属性:映射的Java的pojo类的全限定名
id标签:主键使用id标签,一般返回结果只有一个id标签
result标签:非主属性使用result标签进行显性指定映射关系
property属性:映射java的属性名
column属性:数据库中字段名
javaType:java属性名的类型
jdbcType:数据库字段名的类型
typeHandler:指定类型转换器
-->
<resultMap id="studentMap" type="student">
<id property="id" column="SID"/>
<result property="Sname" column="Sname"/>
<result property="Ssex" column="Ssex"/>
<result property="Sage" column="Sage"/>
</resultMap>
<!--
select标签:查询操作标签
id:statementID,一般是对应接口中方法名称,在同一个命名空间下具有唯一性,
parameterType:表示参数类型
parameterMap:表示参数类型 入参是Map类型
resultType:指定返回类型
resultMap:指定返回结果类型
#{XXX}:表示占位符 ?
-->
<select id="selectStudentById" parameterType="int" resultMap="studentMap">
select * from student where SID = #{id}
</select>
resultMap和resultType的异同点:
相同点:指定返回结果的
不同点:
resultType:给定映射的java对象,完成自动映射,如果数据库字段名和java字段一致建议选用resultType
resultMap:需要显性的指定映射关系,如果数据库字段名和java字段不一致必须选用resultMap显性指定映射关系
4.2.2、删除,插入,更新操作:
<!--
delete标签:删除数据
id:statementID,一般是对应接口中方法名称,在同一个命名空间下具有唯一性,
parameterType:入参类型
-->
<delete id="deleteStudentById" parameterType="int" >
delete from student where SID = #{id}
</delete>
<!--
insert标签:插入数据
id:statementID,一般是对应接口中方法名称,在同一个命名空间下具有唯一性,
parameterType:入参类型
useGeneratedKeys:表示是否采用数据自增主键 true|false
keyProperty:主键对应的Java类的属性名 ()
keyColumn:主键对应的数据库的字段名
-->
<insert id="insertStudent" parameterType="student" keyProperty="" keyColumn="" useGeneratedKeys="true">
</insert>
<!--
update标签:变更操作
id:statementID,一般是对应接口中方法名称,在同一个命名空间下具有唯一性,
parameterType:入参类型
keyProperty:主键对应的Java类的属性名
-->
<update id="updateStudent" parameterType="student">
</update>
4.3、编程详解
SqlSessionFactoryBuilder
用来创建sqlSessionFactory会话工厂
SqlSessionFactory:会话工厂
通过读取配置文件创建、配置参数,对文件读取一次即可,可以将SqlSessionFactory设置为单例模式
通过SqlSessionFactory创建会话
sqlSession:会话:
对数据库的CRUD操作哦都是通过会话操作,sqlSession是线程不安全的,将会话设置为局部变量
一级缓存机制就是sqlSession会话级别的缓存,mybatis默认是开启一级缓存