Mybatis总结
mybatis是一个优秀的基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身。通过xml或注解的方式将要执行的各种statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句,最后由mybatis1框架执行sql并将结果映射为java对象并返回。
ORM思想(对象关系型映射)
xml方式映射与主机方式映射
<mappers>
<mapper resource="com/ali/dao/IUserDao.xml"
</mappers> (xml)
<mappers>
<mapper resource="com.ali.dao.IUserDao">
</mappers>
Mybatis原理以及运用的设计模式
Mybatis中涉及的知识点有:工厂模式(Factory),构造者模式(Bulider),代理模式,反射,自定义注解,注解的反射,xml解析
mybatis常见属性
resultMap 属性:
用于指定结果集的类型;
parameterType 属性:
用于指定传入参数的类型;
sql语句中使用#{}字符:
它代表占位符,相当于原来jdbc部分所学的?,都是用于执行语句替换实际的数据。具体的数据是由#{}里面的内容决定的。
#{}中内容的写法
由于我们保存方法参数是一个对象,此处要写对象的属性名称
Mysql 自动创建主键id
select last_insert_id();#{}与${}区别
#{}表示一个占位符号
通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,#{}可以有效防止sql注入。#{}可以接受欧简单类型值或pojo属性值。如果parmeterType传输单个简单类型值,#{}括号中可以是或其他名称。
** 表 示 拼 接 s q l 串 ∗ ∗ 通 过 {}表示拼接sql串** 通过 表示拼接sql串∗∗通过{}可以将parameterType传入的内容拼接在sql中且不进行jdbc类型转换,
如果对象属性和数据库中字段不一致该怎么办?
1.查询语句中采用别名,改成和对象属性一致
2.定义resultMap建立实体类和数据库表对应关系(type属性:指定实体类的全限定类名,id标签:用于指定主键字段 ,result:用于指定非主键字段,column属性:用于指定数据库类名;property属性:用于指定实体类属性名称)。
mappers(映射器)
使用相对于类路径的资源。如:
使用mapper接口类路径
如:
注意:此种方法要求 r mapper 接口名称和 r mapper
注册指定包下的所有 mapper 接口
如:
注意:此种方法要求 mar pper 接口名称和 r mapper 映射文件名称相同,且放在同一个目录中。
动态sql
标签
《select id=“findByUser” resultType=“user” parameterType=“user” >
select * from user where 1=1
《if test="username!=null and username != ‘’ ">
and username like #{username}
《/if>
《if test=“address != null”>
and address like #{address}
《/if>
《/select>
注意:标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法
<where 标签>*
《select id=“findByUser” resultType=“user” parameterType=“user”>
select * from user
《where>
《if test="username!=null and username != ‘’ ">
and username like #{username}
《/if>
《if test=“address != null”>
and address like #{address}
《/if>
《/where>
《/select>
《foreach标签》
《select id=“findInIds” resultType=“user” parameterType=“queryvo”>
select * from user
《where>
《if test=“ids != null and ids.size() > 0”>
《foreach collection=“ids” open=“id in ( " close=”)" item=“uid”
separator=",">
#{uid}
《/foreach>
《/if>
《/where>
《/select>
标签用于遍历集合,它的属性:
collection:代表要遍历的集合元素,注意编写时不要写#{}
open:代表语句的开始部分
close:代表结束部分
一对多
xml形式的resultMap
《resultMap type=“account” id=“accountMap”>
《id column=“aid” property=“id”/>
《result column=“uid” property=“uid”/>
《result column=“money” property=“money”/>
《!-- 它是用于指定从表方的引用实体属性的 -->
《association property=“user” javaType=“user”>
《id column=“id” property=“id”/>
《result column=“username” property=“username”/>
《result column=“sex” property=“sex”/>
《result column=“birthday” property=“birthday”/>
《result column=“address” property=“address”/>
《/association>
《/resultMap>
多对一
xml形式的resultMap
《resultMap type=“user” id=“userMap”>
《id column=“id” property=“id”>
《result column=“username” property=“username”/>
《result column=“address” property=“address”/>
《result column=“sex” property=“sex”/>
《result column="birthday"property=“birthday”/>
《!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
–>
《collection property="accounts"ofType=“account”>
《id column=“aid” property=“id”/>
《result column=“uid” property=“uid”/>
《result column=“money” property=“money”/>
《/collection>
《/resultMap>
一级缓存
一级缓存是 SqlSession 级别的缓存,只要 SqlSession 没有 flush 或 close,它就存在
二级缓存
级缓存是 mapper 映射级别的缓存,多个 SqlSession 去操作同一个 Mapper 映射的 sql 语句,多个SqlSession 可以共用二级缓存,二级缓存是跨 SqlSession 的。
注解
@Select("select * from user")
@Results(id="userMap",
value= {
@Result(id=true,column="id",property="userId"),
@Result(column="username",property="userName"),
@Result(column="sex",property="userSex"),
@Result(column="address",property="userAddress"),
@Result(column="birthday",property="userBirthday")
})
@Results 注解
代替的是标签
该注解中可以使用单个@Result 注解,也可以使用@Result 集合
@Results({@Result(),@Result()})或@Results(@Result())
@Resutl 注解
代替了 标签和 标签
@Result 中 属性介绍:
id 是否是主键字段
column 数据库的列名
property 需要装配的属性名
one 需要使用的@One 注解(@Result(one=@One)()))
many 需要使用的@Many 注解(@Result(many=@many)()))
@One 注解(一对一)
代替了 标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:
select 指定用的 来多表查询的 sqlmapper
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。。
使用格式:
@Result(column=" “,property=”",one=@One(select="")
@Many 注解(多对一)
代替了 标签, 是是多表查询的关键,在注解中用来指定子查询返回对象集合。
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType
(一般为 ArrayList)但是注解中可以不定义;
使用格式:
@Result(property="",column="",many=@Many(select=""))
一对一注解
@Select(“select * from account”)
@Results(id=“accountMap”,
value= {
@Result(id=true,column=“id”,property=“id”),
@Result(column=“uid”,property=“uid”),
@Result(column=“money”,property=“money”),
@Result(column=“uid”,
property=“user”,
one=@One(select=“com.itheima.dao.IUserDao.findById”,
fetchType=FetchType.LAZY)
)
})
一对多注解
@Select(“select * from user”)
@Results(id=“userMap”,
value= {
@Result(id=true,column=“id”,property=“userId”),
@Result(column=“username”,property=“userName”),
@Result(column=“sex”,property=“userSex”),
@Result(column=“address”,property=“userAddress”),
@Result(column=“birthday”,property=“userBirthday”),
@Result(column=“id”,property=“accounts”,
many=@Many(
select=“com.itheima.dao.IAccountDao.findByUid”,
fetchType=FetchType.LAZY
)
)
})