Mybatis简介
Mybatis持久层框架,对jdbc进行了封装;
Mybatis通过xml或注解的方式将要执行的statement(statement可以看成一个sql语句)配置起来,并通过java对象和statement中的sql进行映射生成最终执行的sql语句,
最后由mybatis框架执行sql并将结果映射成java对象并返回
。
Mybatis和Hibernate
1.mybatis不是一个完全的orm框架,hibernate是一个完全的orm框架
2.mybatis需要自己写sql,但是输入和输出参数是映射的
3.学mybatis比hibernate容易,但是sql要求有点高
4.更加灵活,更加适用于需求不固定项目
5.查到需要的字段数据,封装对象,返回 ; 不要向hibernate一样在业务上数据还要进行筛选
Mybatis常见名词
1.SqlMapConfig.xml(全局配置文件,数据源,事务,mapper.xml引用)
2.mapper.xml
3.SqlSessionFactory、SqlSession、Executor、输入参数、Mapped Statement、输出参数
配置文件
1.SqlMapConfig.xml :mybatis的全局配置文件
<?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>
<!-- 特性 ,读取配置文件 :value=${key} egvalue=:${jdbc.driver} -->
<properties resource="xxx.properties" />
<!-- 全局配置参数 :开启二级缓存 ,开启延迟载-->
<settings ></settings>
<!-- 别名 :parameterType和resultType设置时,定义别名代替pojo的全路径 -->
<typeAlias type="com.xxx.projec.bean.Student" alias="student">
<typeAliases>
<!-- 别名整个包定义 ,Student 首字母大小写都可以-->
<package name="com.xxx.projec.bean" />
</typeAliases>
<!-- 类型处理器用于java类型和jdbc类型映射 -->
<typeHandlers ></typeHandlers>
<!-- 配置mapper映射文件 -->
<mappers>
<!-- 加载单个映射文件 : 原始dao使用 -->
<!-- <mapper resource="sql_map/Student.xml" /> -->
<!--批量mapper扫描 遵循规则:将mapper.xml和mapper.java文件放在一个目录 且文件名相同 -->
<!-- <package name="com.xxx.projec.dao.mapper" /> -->
</mappers>
</configuration>
2.mapper.xml:编写sql
1.crud
<?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="Student">
<insert id="" parameterType="int" > </insert>
<delete id="" parameterType="int"></delete>
<update id="" parameterType="int"></update>
<select id="" parameterType="Student" resultType="Student">
select * from tb_student where id = #{id}
</select>
<select id="" parameterType="Student" resultMap="resultMapId"></select>
<resultMap type="Student" id="resultMapId">
<id column="id_sql" property="id_student"/>
<result column="name_sql" property="name_student"/>
</resultMap>
</mapper>
1.id:Statement的id标识mapper中定义的 sql,id是在同一个namespace中不允许重复(sql对应着Statement)
2.#{}:表示一个占位符,避免sql注入;${}表示替换 (ibatis#key#)
3.parameterType:表示输入参数的类型
4.resultType:表示输出 结果集单条记录映射的java对象类型(pojo),select查询的字段名(别名)和resultType中Pojo的属性名一致,才能映射成功。
5.resultMap:如果查询字段不一致,resultMap建立起映射关系,最终返回resultMap中type定义的对象类型
2.#{}:表示一个占位符,避免sql注入;${}表示替换 (ibatis#key#)
3.parameterType:表示输入参数的类型
4.resultType:表示输出 结果集单条记录映射的java对象类型(pojo),select查询的字段名(别名)和resultType中Pojo的属性名一致,才能映射成功。
5.resultMap:如果查询字段不一致,resultMap建立起映射关系,最终返回resultMap中type定义的对象类型
6.ibatis的别名typeAlias是些在mapper.xml sqlMap中的,这里不允许写
一般情况下建议使用resultType,因为简单方便。针对一对多查询或要使用延迟加载 ,必须使用resultMap。
select * from table where birthday >=#{birthday}
select * from table where birthday >=to_date(‘${birthday}’,’yyyy-MM-dd’)
select * from table where birthday >=to_date(‘${birthday}’,’yyyy-MM-dd’)
2.访问 mapper- parameterType (输入类型)
parameterType="hashmap" 引用: #{key}、${key}
parameterType="Student" 引用: #{property}、${property} eg:#{name}、${location.city}
parameterType="int" 引用: #{随便什么名字}、${随便什么名字}
parameterType="Student" 引用: #{property}、${property} eg:#{name}、${location.city}
parameterType="int" 引用: #{随便什么名字}、${随便什么名字}
3.动态sql
if、where
<!-- if -->
<!-- where 第一个and不会引入 -->
<select id="" parameterType="Student" resultType="Student">
select * from tb_student
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="property!=null">
and property=#{property}
</if>
</where>
</select>
sql片段
<sql id="sqlPart">
<if test="id!=null">
and id=#{id}
</if>
<if test="property!=null">
and property=#{property}
</if>
</sql>
<select id="" parameterType="Student" resultType="Student">
select * from tb_student
<where>
<include refid="sqlPart"></include>
</where>
</select>
foreach
<!-- foreach int[] ids={1,2,3} -->
<sql id="sqlPart">
<if test="ids!=null">
<!-- and(id=1 or id=2 or id=3) -->
<foreach collection="ids" item="id" open="and(" separator="or" close=")" >
id="#{id}"
</foreach>
<!-- and id in(1,2,3) -->
<foreach collection="ids" item="id" open="and id in" separator="," close=")" >
id="#{id}"
</foreach>
</if>
</sql>
面向接口编程——mapper(dao)动态代理
写dao接口(Mapper),而不需要写dao实现类,由mybatis根据dao接口和映射文件中statement的定义生成接口实现代理对象。可以调用代理对象方法。
步骤:
1.编写mapper.xml-> XxxMapper.xml
2.在SqlMapConfig中引入XxxMapper.xml文件
3.开发接口 dao->XxxMapper.java:方法名和XxxMapper.xml中statement的id相同,输入参数、输出参数一致
要求:
1.在XxxMapper.xml中将namespace设置为XxxMapper.java的全限定名:com.xxxproject.mapper.XxxMapper
2.将mapper.java接口的方法名和mapper.xml中statement的id保持一致。
3.将mapper.java接口的方法输入参数类型和mapper.xml中statement的parameterType保持一致
4.将mapper.java接口的方法输出 结果类型和mapper.xml中statement的resultType保持一致
缓存
一级缓存 Hashmap存储
开启:默认开启
范围:sqlSession()
在同一 个SqlSession中,两次执行相同的sql查询,第二次不再从数据库查询清除:SqlSession.commit()
如果第一次查询后,执行commit提交,mybatis会清除缓存,第二次查询从数据库查询
SqlSession.close()
SqlSession.close()
二级缓存
开启:
在核心配置文件SqlMapConfig.xml中加入,表示打开二级缓存开关
<setting name="cacheEnabled" value="true"/>
<setting name="cacheEnabled" value="true"/>
statement启用二级缓存,设置useCache=true(默认值为true)
范围:mapper的namespace
相同的namespace使用一个二级缓存结构
清除:
执行statement需要清除,在statement中设置flushCache="true" (默认值 是true,如果false update都不会更新到新数据)
要求:
pojo 实现Serializable接口
可以控制长时间不刷新(统计),也可以控制每次都刷新数据(股票)