一个Java小白的学习之路 个人博客 youngljx.top
MyBatis的开发流程:
- MyBatis核心文件配置和相关属性设置
- Mapper.xml映射文件实现SQL映射操作
- 使用SqlSessionFactoryBuilder加载配置文件创建核心对象SqlSessionFactory
- 每次开启数据库会话有SqlSessionFactory开启SqlSession
- 通过SqlSession调用执行mapper中设置的数据访问操作
MyBatis的重点基础知识点:
映射文件 表名+mapper.xml配置
1.namespace 是mapper接口的全限定名 通过mapper.xml和mapper.java进行关联
2.mapper.xml中statement的 id就是mapper.java中方法名
3.mapper.xml中statement的 parameterType和mapper.java中方法输入参数类型一致
4.mapper.xml中statement的resultType和mapper.java中方法返回值类型一致
parameterType完成输入映射
传递多个参数 ,多参数那么就不能使用parameterType
- 基于参数索引 #{index}是第几个就用第几个的索引,索引从0开始
select t.* from tableName where id = #{0} and name = #{1}
- 基于注解
public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("code")String code);
resultType和resultMap完成输出映射
-
resultType指定输出结果的类型(pojo、简单类型、hashmap…),将sql查询结果映射为java对象, sql查询的列名要和resultType指定pojo的属性名相同,指定相同 属性方可映射成功, 返回值为集合或单个对象时,resultType都等于相应的pojo类
-
resultMap 将sql查询结果映射为java对象 , sql查询列名和最终要映射的pojo的属性名不一致,resultMap将列名和pojo的属性名做一个对应关系 (列名和属性名映射配置)
配置详解:
#{}表示一个占位符,向占位符输入参数,#{}接收pojo数据,可以使用OGNL解析出pojo的属性值
${}表示sql的拼接,通过${}接收参数,将参数的内容不加任何修饰拼接在sql中,可以使用OGNL解析出pojo的属性值,缺点:不能防止sql注入。
concat()是mysql中自带的拼接字符串参数,常用在条件查询
<!-- 抽取sql片段 -->
<sql id="sqlByLike">
<where>
<!--
<if test="ntitle!=null and ntitle!=''">
and ntitle like '%${ntitle}%'
</if>
<if test="nauthor!=null and nauthor!=''">
and nauthor like '%${nauthor}%'
</if>
-->
<if test="ntitle!=null and ntitle!=''">
and ntitle like concat("%",#{ntitle},"%")
</if>
<if test="nauthor!=null and nauthor!=''">
and nauthor like concat("%",#{nauthor},"%")
</if>
</where>
</sql>
MyBatis关联映射以及动态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">
<!-- namespace的名字需要跟接口的类名一致 -->
<mapper namespace="cn.bdqn.dao.UserMapper">
<!-- 实现列名和属性名映射配置
1、resultMap属性:type为java实体类;id为此resultMap的标识
2、resultMap的子元素:
id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能.
result – 映射到JavaBean 的某个“简单类型”属性,String,int等.
association – 映射到JavaBean 的某个“复杂类型”属性,其他JavaBean类.
collection –复杂类型集合
-->
<!--根据roleId获取用户列表: 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<!-- <resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleId" column="roleId"/>
<result property="roleName" column="roleName"/>
</resultMap>
<select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select>
-->
多对一 关联映射
<!-- 根据roleId获取用户列表 association start-->
<resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="roleId" column="roleId" />
<!-- <association property="role" javaType="Role" >
<result property="id" column="id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association> -->
<association property="role" javaType="Role" resultMap="roleMap"/>
</resultMap>
<select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleCode as roleCode,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select>
<!-- association end-->
一对多 关联映射
<!-- 获取指定用户的地址列表 collection start-->
<resultMap type="User" id="userMap">
<id property="id" column="userId"/>
<collection property="addressList" ofType="Address">
<id property="id" column="a_id"/>
<result property="postCode" column="postCode"/>
<result property="addressContent" column="addressContent"/>
</collection>
</resultMap>
<select id="getAddressListByUserId" parameterType="User" resultMap="userMap">
select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id}
</select>
<!-- collection end -->
<!--
1、有些时候,sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。
2、mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:
if 语句 (简单的条件判断)
choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
set (主要用于更新时)
foreach (在实现 mybatis in 语句查询时特别有用)
-->
<!-- if(判断参数) - 将实体类不为空的属性作为where条件 -->
<select id="searchUserList" parameterType="User" resultMap="seachUser">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id
<if test="roleId!=null">
and u.roleId = #{roleId}
</if>
<if test="userCode != null">
and u.userCode like CONCAT ('%',#{userCode},'%') //使用CONCAT拼接字符串查询
</if>
<if test="userName != null">
and u.userName like CONCAT ('%',#{userName},'%')
</if>
</select>
<!-- if/set(判断参数) - 将实体类不为空的属性更新 -->
<!-- <update id="update" parameterType="User">
update user
<set>
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="roleId != null">roleId=#{roleId}</if>
</set>
where id=#{id}
</update> -->
<!-- if/trim代替set(判断参数) - 将实体类不为空的属性更新 -->
<update id="update" parameterType="User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="roleId != null">roleId=#{roleId}</if>
</trim>
where id=#{id}
</update>
<!--注意: 你可以传递一个List实例或者数组作为参数对象传给MyBatis。
当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。
List实例将会以“list”作为键,而数组实例将会以“array”作为键。
配置文件中的parameterType是可以不配置的-->
<resultMap type="User" id="userMapByDep">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
</resultMap>
<!-- foreach(循环array参数) - 作为where中in的条件 -->
<select id="getUserByDepId_foreach_array" resultMap="userMapByDep">
select * from user where depId in
<foreach collection="array" item="depIds" open="(" separator="," close=")">
#{depIds}
</foreach>
</select>
<!-- foreach(循环List<String>参数) - 作为where中in的条件 -->
<select id="getUserByDepId_foreach_list" resultMap="userMapByDep">
select * from user where depId in
<foreach collection="list" item="depIdList" open="(" separator="," close=")">
#{depIdList}
</foreach>
</select>
</mapper>
简单总结一下,未完待续