mybatis知识点总结

本文总结了MyBatis的重要特性,包括使用${}处理动态SQL,配置resultMap处理复合对象,利用set标签与choose标签实现动态更新,foreach标签在插入与查询中的应用,字符串拼接的多种方式,以及TypeHandler的作用。还介绍了如何通过typeAliases简化类型别名,使用<selectKey>获取自增ID,以及resultMap处理一对多关系映射的实例。
摘要由CSDN通过智能技术生成

1 mybatis 传表名和order by时必须用${},而不能用#

2 mybatis 返回类型是一个复合对象的话,用

<resultMap id="BaseResultMap" type="com.wanda.gmp.admin.dao.model.brand.Brand" >
 <id column="id" property="id" jdbcType="BIGINT" />
  <result column="zh_name" property="zhName" jdbcType="VARCHAR" />

<association property= "复合对象model中的名称" javaType="model中的对象名">

<result column="id" property="id"/>

<result column="name" property="name"/>
</association>
</resultMap>
3 mybatis resultType返回基本类型和对象,resultMap可以返回list

4 mybatis动态sql

(1)choose when otherWise 类似于if,where标签可以选用and
<where>
<choose>
<when test= "stu.id!=null">
  and id = #{stu.id}
</when>
<when></when>
<otherwise>
and gender= #{stu.gender}
</otherwise>
</choose>
<where>

(2)set标签可以选用逗号

update Student
<set>
<if test= "name != null">
name =#{name},
</if>
<if test= "brt != null">
birthday = #{brt},
</if>
</set>

(3)foreach标签

1、void insertCollection(List<RequirementPicture> list);
<insert id="insertCollection" parameterType="java.util.List">
    insert into requirement_picture (id, reference_type, reference_id,
    belongs_type, pic_id, seq_id,created_by, updated_by)
    values
    <foreach item="item" index="index" collection="list"
             separator=",">
      <trim prefix="(" suffixOverrides=", " suffix=")">
        #{item.id,jdbcType=BIGINT}, #{item.referenceType,jdbcType=TINYINT}, #

{item.referenceId,jdbcType=BIGINT},
        #{item.belongsType,jdbcType=TINYINT}, #{item.picId,jdbcType=VARCHAR}, #

{item.seqId,jdbcType=TINYINT},
        #{item.createdBy,jdbcType=VARCHAR}, #{item.updatedBy,jdbcType=VARCHAR}
      </trim>
    </foreach>
  </insert>

2、List<Brand> findByIds(List<Long> ids);
<select id="findByIds" resultMap="BaseResultMap" parameterType="java.util.List" >
    select
    <include refid="Base_Column_List" />
    from brand
    where id in
    <foreach collection="list" item="id" index="index" open="("
             close=")" separator=",">
      #{id}
    </foreach>
    and is_active=1
  </select>

(4)mybatis字符串拼接

1、<if test="name != null and name!=''" >
      and (d.full_name LIKE CONCAT('%',#{name,jdbcType=VARCHAR},'%')
    
      or e.zh_name LIKE CONCAT('%',#{name,jdbcType=VARCHAR},'%')
    
      or e.en_name LIKE CONCAT('%',#{name,jdbcType=VARCHAR},'%'))
    </if>

2、where name like '%${name}%'

3、通过bind标签避免$的危害
<select id= "queryStudent" parameterType="Student" resultMap = "studentResult">
<bind name= "pattern" value="'%'+ _parameter.getName()+'%'">
select id from Student where name like #{pattern}
</select>


5 databaseId用来连接不同类型的数据库

6  Mybatis中的TypeHandler是什么?

无论是 MyBatis 在预处理语句(PreparedStatement)中设置一个参数时,还是从结果集中取出一个值

时,都会用类型处理器将获取的值以合适的方式转换成 Java 类型。Mybatis默认为我们实现了许多

TypeHandler, 当我们没有配置指定TypeHandler时,Mybatis会根据参数或者返回结果的不同,默认为我们

选择合适的TypeHandler处理。

mybatis中用typehandles可以进行model对象(javaType)和数据库类型(jdbcType)的转换,

我们在自定义TypeHandler的时候,可以在TypeHandler通过@MappedJdbcTypes指定jdbcType, 通过

@MappedTypes 指定javaType, 如果没有使用注解指定,那么我们就需要在配置文件中配置。


7 typeAliases节点主要用来设置别名,其实这是挺好用的一个功能, 通过配置别名,我们不用再指定完整的

包名,并且还能取别名。

例如: 我们在使用 com.demo.entity. UserEntity 的时候,我们可以直接配置一个别名user, 这样以后在

配置文件中要使用到com.demo.entity. UserEntity的时候,直接使用User即可。
也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user)

8

好啦,咱们就先来看看 insert, update, delete 怎么配置, 能配置哪些元素吧:

复制代码
<?xml version="1.0" encoding="UTF-8" ?>   
<!DOCTYPE mapper   
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 

<!-- mapper 为根元素节点, 一个namespace对应一个dao -->
<mapper namespace="com.dy.dao.UserDao">

    <insert
      <!-- 1. id (必须配置)
        id是命名空间中的唯一标识符,可被用来代表这条语句。 
        一个命名空间(namespace) 对应一个dao接口, 
        这个id也应该对应dao里面的某个方法(相当于方法的实现),因此id 应该与方法名一致 -->
      
      id="insertUser"
      
      <!-- 2. parameterType (可选配置, 默认为mybatis自动选择处理)
        将要传入语句的参数的完全限定类名或别名, 如果不配置,mybatis会通过ParameterHandler 根据参数类型默认选择合适的typeHandler进行处理
        parameterType 主要指定参数类型,可以是int, short, long, string等类型,也可以是复杂类型(如对象) -->
      
      parameterType="com.demo.User"
      
      <!-- 3. flushCache (可选配置,默认配置为true)
        将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:true(对应插入、更新和删除语句) -->
      
      flushCache="true"
      
      <!-- 4. statementType (可选配置,默认配置为PREPARED)
        STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 -->
      
      statementType="PREPARED"
      
      <!-- 5. keyProperty (可选配置, 默认为unset)
        (仅对 insert 和 update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值,默认:unset。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 -->
      
      keyProperty=""
      
      <!-- 6. keyColumn     (可选配置)
        (仅对 insert 和 update 有用)通过生成的键值设置表中的列名,这个设置仅在某些数据库(像 PostgreSQL)是必须的,当主键列不是表中的第一列的时候需要设置。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 -->
      
      keyColumn=""
      
      <!-- 7. useGeneratedKeys (可选配置, 默认为false)
        (仅对 insert 和 update 有用)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段),默认值:false。  -->
      
      useGeneratedKeys="false"
      
      <!-- 8. timeout  (可选配置, 默认为unset, 依赖驱动)
        这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。 -->
      timeout="20">

    <update
      id="updateUser"
      parameterType="com.demo.User"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">

    <delete
      id="deleteUser"
      parameterType="com.demo.User"
      flushCache="true"
      statementType="PREPARED"
      timeout="20">
</mapper
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值