mybatis笔记

1. Mybatis介绍

MyBatis 本是apache的一个开源项目iBatis,后来改名为MyBatis ,迁移到GithubMyBatis是一个优秀的持久层框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。

Mybatis通过xml或注解的方式t配置,通过java对象和statement中的sql进行映射生成最终执行的sql语句,最后由mybatis框架执行sql并将结果映射成java对象并返回。

1.1. #{}${}

#{}表示一个占位符号,通过#{}可以实现preparedStatement向占位符中设置值,自动进行java类型和jdbc类型转换,如字符串加引号。#{}可以有效防止sql注入 #{}可以接收简单类型值或pojo属性值 如果parameterType传输单个简单类型值,#{}括号中可以是value或其它名称。${}表示拼接sql串,通过${}可以将parameterType 传入的内容拼接在sql中且不进行jdbc类型转换 ${}可以接收简单类型值或pojo属性值,如果parameterType传输单个简单类型值,${}括号中只能是value

1.2. 开发规范

Mapper接口开发方法只需要程序员编写Mapper接口,由Mybatis框架根据接口定义创建接口的动态代理对象

Mapper接口开发需要遵循以下规范:

1、 Mapper.xml文件中的namespacemapper接口的类路径相同。

2、 Mapper接口方法名和Mapper.xml中定义的每个statementid相同

3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql parameterType的类型相同

4、 Mapper接口方法的输出参数类型和mapper.xml中定义的每个sqlresultType的类型相同

1.3. mybatis支持别名

别名

映射的类型

_byte

byte

_long

long

_short

short

_int

int

_integer

int

_double

double

_float

float

_boolean

boolean

string

String

byte

Byte

long

Long

short

Short

int

Integer

integer

Integer

double

Double

float

Float

boolean

Boolean

date

Date

decimal

BigDecimal

bigdecimal

BigDecimal

map

Map

1.4. 入参包装对象

public class QueryVo {
    // 包含其他的pojo
    private User user;

    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

 #{user.phone}

1.5.出参resultMap

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,也可映射一对一查询和一对多查询

<resultMap id="UserOrderMap" type="com.summer.entity.UserInfo" extends="BaseResultMap">
    <collection property="orderBorrowList"
                ofType="com.summer.entity.OrderBorrow">
      <id column="aid" property="id" jdbcType="INTEGER"/>
      <result column="status" property="status" jdbcType="TINYINT"/>
      <result column="ob_create_time" property="applyTimeChg" jdbcType="VARCHAR"/>
      <result column="amount" property="applyAmount" jdbcType="INTEGER"/>
      <result column="loan_term" property="loanTerm" jdbcType="INTEGER"/>
    </collection>
  </resultMap>
<!-- association :配置一对一属性 -->
    <!-- property:order里面的User属性名 -->
    <!-- javaType:属性类型 -->
    <association property="user" javaType="user">
        <!-- id:声明主键,表示user_id是关联查询对象的唯一标识-->
        <id property="id" column="user_id" />
        <result property="username" column="username" />
        <result property="address" column="address" />
    </association>

1.6.动态sql

<select id="selectSimple" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from order_repayment_detail
        <where>
            <if test="repaymentId != null and repaymentId != ''">
                and repayment_id = #{repaymentId}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
        </where>
        ORDER BY id DESC
    </select>

1.7.foreach标签

入参为数组或List使用foreach解析,如果入参为集合或数组,collection值为listarray,若为包装类,collection值为成员变量属性

<!-- foreach标签,进行遍历 -->
        <!-- collection:遍历的集合,这里是QueryVo的ids属性 -->
        <!-- item:遍历的项目,可以随便写,,但是和后面的#{}里面要一致 -->
        <!-- open:在前面添加的sql片段 -->
        <!-- close:在结尾处添加的sql片段 -->
        <!-- separator:指定遍历的元素之间使用的分隔符 -->
        <foreach collection="ids" item="item" open="id IN (" close=")"
            separator=",">
            #{item}
        </foreach>
原文地址:https://www.cnblogs.com/fswhq/p/10322604.html