mybatis详解

MyBatis是什么?
MyBatis 是一款一流的支持自定义SQL、存储过程和高级映射的持久化框架。 MyBatis几乎消除了所有的JDBC 代码,也基本不需要手工去设置参数和获取检索结果。 MyBatis 能够使用简单的XML格式或者注解进行来配置,能够映射基本数据元素、 Map 接口和POJOs(普通java对象)到数据库中的记录。

Select元素

<select id=” selectPerson” parameterType=” int” resultType=” hashmap” >
SELECT * FROM PERSON WHERE ID = #{id}
</select>

这条语句叫做selectPerson,以int 型(或者Integer 型)作为参数,并返回一个以数据库
列名作为键值的HashMap。

select 语句有很多的属性允许您详细配置每一条语句。
<select
id=”selectPerson”
parameterType=” int”
resultType=” hashmap”
resultMap=” personResultMap”
flushCache=” false”
useCache=” true”
timeout=” 10000”
fetchSize=” 256”
statementType=” PREPARED”
resultSetType=” FORWARD_ONLY”
>

select参数详解

Insert、 update、 delete元素
数据修改语句insert、 update和delete的配置使用都非常相似:

<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
useGeneratedKeys=""
timeout="20000">

<update
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">

<delete
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">

参数描述

如果想让插入的值返回主键(mysql、sqlserver):

<insert id="insertAuthor" parameterType="domain.blog.Author"
useGeneratedKeys=” true” keyProperty=” id” >
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>

oracle等数据库

<insert id="insert" parameterType="map">  
    insert into table1 (name) values (#{name})  
    <selectKey resultType="java.lang.Integer" keyProperty="id">  
      CALL IDENTITY()  
    </selectKey>  
  </insert>  

批量插入获取主键:
方法同上
解决办法:
1、升级Mybatis版本到3.3.1。
2、在Dao中不能使用@param注解。
3、Mapper.xml中使用list变量接受Dao中的集合。

#与$的区别:
用#{}语法会促使MyBatis 生成PreparedStatement并且安全地设置
PreparedStatement 参数( =?)值。尽量这是安全、快捷并且是经常使用的。

$不加修改地接收用户输入并应用到语句的方式,是非常不安全的。这使用户能够进行SQL注入,破坏代码。所以,要么这些字段不允许用户输入,要么用户每次输入后都进行检测
和规避

resultType与resultMap的区别:
MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。
在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。
①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性。
②当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

自定义resultMap:

<resultMap id="userResultMap" type="User">
    <id property="id" column="user_id" />
    <result property="username" column="username"/>
    <result property="password" column="password"/>
</resultMap>
<select id=” selectUsers” parameterType=” int” resultMap=”  userResultMap” >
select user_id, user_name, hashed_password
from some_table
where id = #{id}
</select>

高级映射:
这里写图片描述

动态SQL:
if

<select id=” findActiveBlogLike”
parameterType=” Blog” resultType=” Blog” >
SELECT * FROM BLOG WHERE state = ‘ ACTIVE’
<if test=” title != null” >
AND title like #{title}
</if>
<if test=” author != null and author.name != null” >
AND title like #{author.name}
</if>
</select>

choose, when, otherwise元素

<select id=” findActiveBlogLike”
parameterType=” Blog” resultType=” Blog” >
SELECT * FROM BLOG WHERE state = ‘ ACTIVE’
<choose>
<when test=” title != null” >
AND title like #{title}
</when>
<when test=” author != null and author.name != null” >
AND title like #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>

Foreach元素

<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值