MyBatis框架
MyBatis框架简介
MyBatis的前身是iBatis。
MyBatis是一个开源的数据持久层框架。它内部封装了通过JDBC访问数据库的操作,支持普通的SQL查询、存储过程和高级映射,几乎消除了所以的JDBC代码和参数的手工设置以及结果集的检索。
MyBatis作为持久层框架,主要是思想是把程序中的SQL取出来,放在配置文件中,可以做到SQL与程序代码分离。方便修复SQL。
什么是ORM
ORM的全称为Object / Relational Mapping 其意思的对象/关系映射,是一种数据持久化技术。它在对象模型和关系数据库之间建立对应关系,并且提供了一种机制,通过JavaBean对象去操作数据库表中的数据
MyBatis和ORM的关系
MyBatis是ORM的一种解决方案。
MyBatis在对象模型和关系库的表之间建立了一座桥梁,通过MyBatis建立SQL 关系映射,以便捷实现数据库存储、查询、更改和删除。
MyBatis环境搭建
-
下载jar包
下载网页:https://mvnrepository.com
在网页中下载最新的Releas版本的MyBatis
-
部署jar包
把下载的jar包放置pom.xml文件中的
<dependencies> </dependencies>
-
编写MySatis核心配置文件
在miam包中的resouces包创建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">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/><property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
-
configuration:配置文件的根节点
-
properties:通过resource属性从外部指定properties属性文件
-
settings:设置MyBatis运行中的一些行为
-
environments:表示配置MyBatis的多套运行环境
-
environment:配置MyBatis的以同运行环境
-
mappers:作用是去哪里映射文件
-
mapper:mappers的子节点,具体指定映射路径
-
创建实体类
在miam包中的java包创建实体类(pojo)
-
创建DAO接口
在miam包中的java包创建mapper类
-
创建SQL映射文件
<?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>
-
编写测试类
-
读取全局配置文件:mybatis-config.xml
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
-
创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
-
创建SqlSessiond对象
SqlSession sqlSession = MyBatisUtil.open(); List<Bill> list = sqlSession.getMapper(BillMapper.class).selectList();
或
List<Bill> list = sqlSession.select("完全限定名");
-
关闭SqlSession对象
sqlSession.close();
-
SQL映射文件
MyBatis真正强大之处就在于SQL映射语句,也是它的魅力所在。对于它强大的功能,SQL映射文件的配置却非常简单。
SQL顶级元素配置
-
mapper:映射文件的根节点元素,只有一个属性namespace(命名空间)
-
作用:
- 用于区分不同的mapper,全局唯一。
- 绑定DAO接口,即面向对象编辑。
-
代码:
<mapper namespace="全映射路径"></mapper>
-
-
cache:配置给定命名空间的缓存
-
cache-ref:从其他命名空间引用缓存配置
-
resultMap:用来描述数据库结果集和对象的对应关系
-
association
属性
- id:和映射查询语句的resultMap的值一致
- javaType:完整Java的类名或别名
- property:映射数据列数据库实体对象属性
- result:
- property:映射数据库列的实体对象的属性
- result:数据库列明和别名
-
collection:和association的属性一样。association映射单个,collection映射多个
-
-
sql:可以重用的sql块,也可以被其他语句应用
-
insert:映射插入语句
-
代码:
<insert></insert>
-
同下
-
-
update:映射更新语句
-
代码:
<update></update>
-
同下
-
-
delete:映射删除语句
-
代码:
<delete></delete>
-
同下
-
-
select:映射查询语句
-
代码:
<select></select>
-
属性:
- id:命名空间中唯一的标识符。
- prarameterType:表示查询语句的参数的类型。可以是完全限定名或别名,它支持记错数据类型和复杂数据类型。
- resultType:放回类型
- resultMap:是对外部resultMap定义的引用
-
Mapper接口
-
接口的路径和mapper的属性namespace的值对应
-
接口中的方法和insert:映射插入语句、update:映射更新语句、delete:映射删除语句、select:映射查询语句,大id一致
-
接口的参数可以使用:@Param注解实现参数的传递,参数id,和name的传递:
List<Bill> selectListLike(@Param("id")int id,@Param("name")String name );
-
测试调用接口的方法代码:
List<Bill> list = sqlSession.getMapper(BillMapper.class).selectList();
活动SQL
-
if:利用if实现简单的条件选择
-
属性:test:判断条件,多个条件用and或者or链接
-
代码:
<if test="name!= null and name!= ''">and name like concat ('%',#{name},'%')</if>
-
-
where:简化SQL语句中的where的条件判断,sql后加上where,会把第一个条件的and去掉
-
代码:
<where><if test="name!= null and name!= ''">and name like concat ('%',#{name},'%')</if ></where>
-
-
choose:类似于Java的switch语句的,when第一个条件成立后面都不执行,otherwise是when条件都都不成立才执行
-
配合使用:
-
when:
<when test="name!= null and name!= ''">and name like concat ('%',#{name},'%')</when>
-
otherwise:
<otherwise></otherwise>
-
-
代码:
<choose> <when test="name!= null and name!= ''">and name like concat ('%',#{name},'%')</when><when test="name!= null and name!= ''">and name like concat ('%',#{name},'%')</when><otherwise></otherwise> </choose>
-
-
set:解决动态更新语句,会自动加上set,和把最后一sql的,自动去掉
-
foreach:迭代一个集合
-
item:表示集合中每一个元素进行迭代的别名
-
index:指定一个名称,每次迭代的位置
-
open:表示以什么开始
-
separator:以什么隔开
-
close:以什么结尾
<select id="selectPostIn" resultType="Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
-
-
trim:可以灵活的去掉多余的关键字
-
prefix:在trim内容上加上的前缀
-
suffix:在trim内容上加上的后缀
-
prefixOverrides:对trim内容首部值定内容的忽略
-
suffixOverrides:对trim内容未部值定内容的忽略
-
代码:
`
-
<trim prefix="where" prefixOverrides="and | or">
<if test="id != null and id > 0"> and p.id = #{id}</if>
</trim>`
<trim prefix="ser" suffixOverrides=",">
<if test="id != null and id > 0"> p.id = #{id},</if>
</trim>
```