mybatis 学习笔记 记录

来源:互联网 发布:ubuntu 安装glsl 编辑:程序博客网 时间:2024/06/17 17:16
SqlMapConfig.xml


1、properties标签
<!-- 加载  properties  文件 -->
<properties resource="db.properties">
<!-- property标签中 中可以 再配置 一些属性值-->
<property name="a" value="b"/>
</properties>


2、settings标签 全局参数配置
(1) mybatis 框架在运行时可以调整一些运行参数
例如:开启 二级缓存、开启延迟加载


<settings></settings>  


3、typeAliases(别名)*重点
(1) 在mapper.xml 中定义 很多的 statement  ,statement 需要有 parameteType 指定输入参数的类型,
需要resultType  指定输出结果的映射类型




<typeAliases>
<!-- 单个 别名定义 -->
<typeAlias type="org.xgz.mybatis.po.User" alias="UserVO"/>
<!-- 批量 别名定义 ,扫描整个包下的类,别名为类名 -->
<package name="org.xgz.mybatis.po"/>
</typeAliases>
4、typeHandles 类型处理器
(1) mybatis 中通过 typeHandles 完成jdbc 类型 和 Java 类型的转换  
typeHandles 
5、mapper ( mapper配置 )
<mappers>
    <!-- 通过 resource 单个映射文件的加载  -->
    <mapper resource="sqlmap/UserMapper.xml" />
    <mapper resource="mapper/UserMapper.xml" />

<!-- 通过 url 使用完全限定路径加载  -->    
    <!-- <mapper url="file:///D:\mapper\UserMapper.xml" /> -->    
   
   
    <!-- 通过 mapper接口路径  来加载 映射文件 -->
    <!-- 这种方式必须 遵循一种规范 mapper接口类名 和 mapper.xml文件的命名  保持一致 ,且在一个目录(包)下 -->
    <!-- 上面的规范前提是 :使用的是 mapper 代理方法  -->
    <!-- <mapper resource="org.xgz.mybatis.mapper.UserMapper" /> -->
   
   
   
    <!-- 批量加载mapper
    指定mapper接口的包名,mybaties 自动扫描 mapper接口进行加载
    这种方式必须 遵循一种规范 mapper接口类名 和 mapper.xml文件的命名  保持一致 ,且在一个目录(包)下 -->
    <package name="org.xgz.mybatis.mapper"/>
       
    </mappers>






6、输入映射
通过 parameterType 指定输入参数 的 类型 ,类型 可以是简单类型 、HashMap、pojo的包装类型



7、输出映射
resultType
实体属性值 名称 映射
resultMap
mybatis中使用 resultMap 来完成高级输出结果映射。


1、resultMap 使用方式
如果 查询出来的 列名 和POJO 属性名 不一致,还想 用POJO 来进行映射 ,可以 通过 定义一个 resultMap 对列名和属性名 之间 来做一个 映射关系

步骤
(1)定义 resultMap

(2)使用 resultMap 来作为 statement 的 输出映射类型
7.1用下面的SQL 来完成 User 输出映射 
<!--  使用 resultMap 来进行输出映射-->
<select id="findUserById" parameterType="int" resultMap="">
SELECT id,id_,username,user_name FROM USER WHERE id=#{id}
</select>


<resultMap type="UserVO" id="UserResultMap">
<!-- id 表示唯一标示
column=查询出来列名
property= type指定的 POJO类型的 属性名
最终ResultMap 对column 和 property 做一个映射关系
-->
<id column="_id" property="id"/>
<!--  result 对普通列 映射定义
column=查询出来列名
property= type指定的 POJO类型的 属性名
最终ResultMap 对column 和 property 做一个映射关系
-->
<result column="user_name" property="username"/>
</resultMap>
@Test
public void testFindUserByIdResultMap() throws Exception {
SqlSession Session = SessionFactory.openSession();
UserMapper mapper = Session.getMapper(UserMapper.class);


System.out.println(mapper.findUserByIdResultMap(1));


}

8、动态SQL
8.1 什么是 动态SQL
mybatis 核心对SQL语句进行灵活 操作,通过表达式进行灵活拼接、组装。
8.2 需求 
用户信息 的综合查询列表 和 用户信息查询列表的 总数 这两个 的statement 定义


对查询条件 进行判断 如果输入的参数不为空才进行 查询条件 拼接。




<select id="findUserList" parameterType="org.xgz.mybatis.po.UserCustom" resultType="org.xgz.mybatis.po.UserCustom">
SELECT * FROM USER   
<!-- where 可以自动的 去掉条件中的  第一个 and  -->
<where>
<if test="id!=null and id!=0">
AND USER.id=#{id} 
</if>

<if test="username!=null and username !='' ">
AND USER.username like '%${username}%' 
</if>
</where>
</select>


8.3 SQL 片段 将上边 实现的动态SQL 判断代码块 抽取出来,组成一个SQL片段,让其他 的statement中 可以引用 SQL片段 

8.3.1 定义SQL 片段
<!-- 定义 SQL片段 
id = sql片段的唯一标示
经验 : 是基于单表 来定义SQL 片段,这样的话 这个SQL 片段的可重用性 才高
在 SQL片段 中不要包括 WHERE,如果 多个 sql片段 条件 会有冲突  
-->
<sql id="query_user_where">

<if test="id!=null and id!=0">
AND USER.id=#{id} 
</if>

<if test="username!=null and username !='' ">
AND USER.username like '%${username}%' 
</if>

</sql>



8.3.2 使用SQL 片段


<select id="findUserCount" parameterType="org.xgz.mybatis.po.UserCustom" resultType="int">
SELECT COUNT(1) FROM USER  

<where>
<!-- 如果 sql片段不再 本mapper,需要加 namespace -->
<include refid="query_user_where"></include>

<!-- 可以 引入多个SQL 片段-->
</where>
</select>


8.4 foreach 标签
向sql传递数组或者List ,mybatis使用foreach 解析
<sql id="query_user_where_foreach">
<if test="idList!=null ">
<!--
使用 foreach 传入 idList
collection : 指定输入对象中 的 集合 的 属性名
item : 每个 遍历出的 对象
open : 开始遍历 是的 拼接串 例如: id in( 
close : 完成遍历 是的 拼接串 例如: )
separator :遍历 的两个对象 中间 所需的 字符串 
-->
<foreach collection="idList" item="user_id" open="id in (" close=")" separator="," >
<!-- 每个需要拼接 的字符串 -->
#{user_id}
</foreach>
 
</if>
</sql>


第二天---- mybatis 高级映射 查询缓存 和spring整合
8.5 将 关联的 列映射到一个 List<POJO>中 (一对多)




实现 一对一 、一对多  、多对多查询 。
延迟加载
查询缓存
一级缓存
二级缓存 (了解 mybatis二级缓存的使用场景)
mybatis spring 整合
逆向工程
原创粉丝点击