MyBatis框架

mybatis的环境搭建

MyBatis;(ORM框架)

在这里插入图片描述
通过Github可以进访问MyBatis的官网
MyBatis是实体类与SQL语句之间建立映射关系的
特点
基于SQL语法
SQL语句封装在配置文件中,便于统一管理与维护,降低程序的耦合度
方便程序代码调试
使用MyBatis的步骤
在这里插入图片描述
下载MyBatis的jar包(在idea中是可以不用手动下载,IDEA是可以进行自动下载的,但要在pom配置中添加下载代码)
编写MyBatis的核心配置文件(counfiguration.xml(默认名称,可以自定义,书写的是数据连接语句))
创建实体类(pojo)
DAO层—SQL映射文件(maooer.xml(默认名称,可以自定义,书写的SQL语句))
创建测试类
读取全局配置文件(或者核心配置文件)*** . xml (读取configuration.XML文件)
创建SqlSessionFactory对象(SqlSessionFactory为MyBatis的核心对象之一),读取配置文件
创建SqlSeesion对象
调用mapper文件进行数据操作
注意在书写表的列名时建议使用驼峰命名规则,好处是便于开发者工作、更好的进行内部的映射,方便数据库的字段与pojo中的属性进行自动的映射
在使用myeclipse进行开发时要导入jar包(log4j mybatus mysqlconnector)三个jar包并且还要关联对应版本的MyBatis的源码
配置文件的书写
MyBatis-config.xml文件的配置代码书写(有顺序的不能随意更改顺序)

文件头,固定书写,不能改变
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
//配置文件的主体部分
<configuration>//主体开始
//读取properties属性文件固定写法(database.properties为文件名可以改变)
    <properties resource="database.properties"/>
//设置日志属性,可以不写(要写必须写在这里)
 //<settings>
    //<setting name="logImpl" value="LOG4J"/>
//</settings>
    <typeAliases><!--实体类起别名-->
        <package name="com.kgc.pojo"/><!--name值为需要进行取别名的包名-->
    </typeAliases>
   //配置运行环境可以有多个(但id不能相同)
    <environments default="development">//development为environment的id值,default为默认,让系统默认执行此环境
        <environment id="development">
            <transactionManager type="JDBC"/><!--事务管理方式(采用JDBC的数据管理)-->
            <!--连接池-->
            <dataSource type="POOLED"><!--获取数据库的连接  POOLED数据源为MyBatis自带的数据源-->
//设置property的属性(通过${属性名}获取properties中的属性值 )
                 <property name="driver" value="${mysql.driver}"/>
                 <property name="url" value="${mysql.url}"/>
                 <property name="username" value="${mysql.username}"/>
                 <property name="password" value="${mysql.password}"/>
            </dataSource>
        </environment>
    </environments>

    <!--寻找映射文件-->
    <mappers>
        <mapper resource="com/kgc/dao/UserMapper.xml"/><!--加载映射文件  路径为全路径com/dao/UserMapper.xml-->
    </mappers>
</configuration>

映射文件的书写UserMapper.xml(pojo对象名加上mapper就是映射文件的名字)

<?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">
//映射文件主体
<mapper namespace="com.kgc.dao.UserMapper">//namespace的值为mapper配置文件的全路径
    <insert id="add" parameterType="User">/*使用别名*/
         /*SQL语句,添加指定表的数据内容*/
        insert into stu values (null,#{ename},#{esex},#{eage},#{salary})
    </insert>
    <select id="findAll" resultType="User">/*参数为泛型的类型*/
        /*数据查询*/
        select * from stu
    </select>
</mapper

框架启动(一个工具类)

/*启动框架*/
private static SqlSessionFactory factory = null;
/*静态代码块,确保其中的数据只加载一回*/
static {
    /*配置文件的路径*/
    String source = "mybatis-config.xml";
    try {
        /*读取配置文件    通过io流进行获取*/
 InputStream in = Resources.getResourceAsStream(source);
        /*获取factory对象并对其进行赋值*/
factory = new SqlSessionFactoryBuilder().build(in); /*全局单列*/
    } catch (IOException e) {
        e.printStackTrace();
    }
}
/*获取SQL会话,得到connnetion 释放资源 预编译 执行SQL 事务*/
public static SqlSession getSeession(){/*数据库操作的对象*/
    return  factory.openSession();
}

框架程序入口(一个测试类)

/*启动方法*/
@Test
public void testAdd(){
//获取session对象,通过session对象进数据库操作
    SqlSession session= SessionUtil.getSeession();
    /*通过映射关系得到接口对象*/
    UserMapper um=session.getMapper(UserMapper.class);
    User user=new User(null,"lishi","n",10,20);
    /*调用方法*/
    um.add(user);
    //提交事务 增删改必须要进行提交事务
    session.commit();
    /*关闭资源*/
    session.close();
}

在进行对数据操作的时候必须进行释放资源
在pom.xml下载导入jar包(该XML文件在创建项目时进会生成)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--生成文件时自动生成的数据-->
    <groupId>com.kgc</groupId>
    <artifactId>MyBatisDemo01</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--下载导包的固定写法-->
    <dependencies>
        <dependency><!--导入

    相应的jar包-->
                <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <!--找到指定的XML文件,代码固定,不能修改-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

MyBatis的优缺点;
mybatis是一个轻量级的半自动化框架,支持动态SQL语句的书写,但也因为SQL语句的存在,使得mybatis框架存在局限性,可移植性差,同时它也是一个功能强大且是数据持久层的框架,支持orm对象关系映射,是一个针对于dao层开发的一个微型框架

MyBatis的核心对象;核心接口和类、mybatis的核心配置文件(mybatis_config.xml)、SQL映射文件(mapper.xml)

mybatis的核心对象

MyBatis的三个基本要素
MyBatis的核心接口和类
SqlSessionFactoryBuilder
SqlSessionFactoryBuilder是用来负责构建SqlSessionFactory实例的,通过build方法来进行构建(SqlSessionFactoryBuilder提供啦多个的build方法的重载)
在这里插入图片描述
配置信息以三种形式提供给SqlSessionFactory的build方法;inputStream(字节流)、reader(字符流)、configuration(类)
在这里插入图片描述
他的生命周期只存在方法体内,用过即丢
SqlSessionFactory(整个MyBatis运用程序的中心)
在这里插入图片描述
SqlSessionFactory的创建是通过SqlSessionFactoryBuilder的build()方法来进行创建的
SqlSsession session=sqlSessionFactory.openSession(b:true)
其中b:true是事务的控制,默认为true是关闭状态,false是开启状态(不写也是默认情况为关闭事务)
他的作用是创建SqlSession实例的
作用域是Application(贯穿整个运用)
SqlSessionFactory为一个单例

package com.kgc.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SessionUtil {
    /*启动框架*/
    private static SqlSessionFactory factory = null;
    /*静态代码块,确保其中的数据只加载一回*/
    static {
        /*配置文件的路径*/
        String source = "mybatis-config.xml";
        try {
            /*读取配置文件    通过io流进行获取*/
            InputStream in = Resources.getResourceAsStream(source);
            /*获取factory对象并对其进行赋值*/
            factory = new SqlSessionFactoryBuilder().build(in); /*全局单列*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*获取SQL会话,得到connnetion 释放资源 预编译 执行SQL 事务*/
    public static SqlSession getSeession(){/*数据库操作的对象*/
        return  factory.openSession();
    }
}

SqlSession
SqlSession是通过SqlSessionFactory的openSession的方法来获得的,通过SqlSession才能对数据进行操作
MyBatis-config.xml系统核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--读取properties文件固定写法-->
    <properties resource="database.properties"/>
    <!--<settings>&lt;!&ndash;可以不写,但要写就必须写在这里&ndash;&gt;
        <setting name="logImpl" value="LOG4J"/>
    </settings>-->
   <!--设置映射的自动匹配 value值为fall为自动匹配-->
    <!--<settings>
        <setting name="autoMappingBehavior" value="NONE"/>
    </settings>-->
    <typeAliases><!--实体类起别名-->
<!--取了别名的包下的类直接可以同过类名进行使用-->
        <package name="com.kgc.pojo"/><!--name值为需要进行取别名的包名-->
    </typeAliases>
    <!--配置运行环境-->
    <!--default中的参数为运行环境的id,指定那个便执行那个-->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/><!--事务管理方式-->
            <!--连接池-->
            <dataSource type="POOLED"><!--获取数据库的连接  数据源-->
                <property name="driver" value="${mysql.driver}"/>
                <property name="url" value="${mysql.url}"/>
                <property name="username" value="${mysql.username}"/>
                <property name="password" value="${mysql.password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--寻找映射文件,可以添加多个的mappers的XML引入文件-->
    <mappers>
    <!--加载映射文件  路径为全路径com/dao/UserMapper.xml-->
        <mapper resource="com/kgc/dao/UserMapper.xml"/>
        <mapper resource="com/kgc/dao/User2Mapper.xml"/>
    </mappers>

</configuration>

mapper.xml 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的路径名必须与dao接口的完全限定名一致-->
<mapper namespace="com.kgc.dao.User2Mapper">
<!--映射文件主体-->
<!--编写resultMap文件-->
    <resultMap id="User2Map" type="User2">
       <id column="eid" property="id"/>
        <result column="ename" property="name"/>
        <!--property为要映射的属性名javaType 为要映射属性的类名-->
        <association property="sexname" javaType="Sex">
            <id column="eid" property="esex"></id>
            <result property="sexname" column="sexname"></result>
        </association>



     </resultMap>

<!--SQL查询语句-->
   <select id="findAll" resultMap="User2Map">
        select * from  stu
    </select>
    <select id="find" resultMap="User2Map">
        select ename, sexname from sex ,stu where sex.eid=stu.esex
    </select>
</mapper>

SQL映射文件

单条件查询(传入的参数数据类型为基本数据类型)
    /*@Param入参(固定写法,参数为名字传入的是SQL语句的参数)*/
    User login(@Param("name") String ename, @Param("id") String eid );
    <!--多条件查询-->
    <select id="login" resultType="User">
        /*这里的参数为传入参数的名称*/
        select * from  stu where ename=#{name} and eid=#{id}
    </select>

在这里插入图片描述

SQL映射的XML文件
mapper-namespace映射文件的根节点(namespace为命名空间是mapper的唯一的一个属性)
cache;配置给定命名空间的缓存
cache-ref;从其他命名空间引用缓存配置
resultMap;用来描述数据库结果集和对象的对应关系

<?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的路径名必须与dao接口的完全限定名一致-->
<mapper namespace="com.kgc.dao.UserMapper">
<!--映射文件主体-->

<!--数据添加-->
/*参数为数据类型这里使用的是别名*/
    <insert id = "add" parameterType = "User">
         /*SQL语句,添加指定表的数据内容*/
        insert into stu values (null,#{ename},#{esex},#{eage},#{salary})
    </insert>
<!-- 数据查询-->
    <select id = "findAll" resultType = "User">/*参数为泛型的类型*/
        select * from stu
    </select>
<!--数据删除-->
    <delete id = "del" parameterType = "User">
        DELETE  FROM stu WHERE eid = (#{eid});
    </delete>
<!--数据修改-->
    <update id="update" parameterType="User">
        update stu set esex=(#{esex}) where eid=(#{eid});
    </update>
<!--多条件查询-->
    <select id="login" resultType="User">
        /*这里的参数为传入参数的名称*/
        select * from  stu where ename=#{name} and eid=#{id}
    </select>
<!--模糊查询-->
    <select id="getuserListByUserName" resultType="User">
        select * from stu
        /*CONCAT为字符串拼接,模糊查询必须使用*/
        where ename like CONCAT('%',#{name},'%')
    </select>
</mapper>

sql;可以重用的SQL块,也可以被其他语句引用

</select>
<sql id="contents">sid,sname,sage,ssex</sql><!--加载重复的SQL语句是使用-->
<select id="findByIdMap" resultMap="stuTeaMapper">
    select
     <include refid="contents"/>/*导入SQL语句*/
     from stu
    <foreach collection="idsKey"
             item="id"
             open="where sid in ("
             separator=","
             close=")">
        #{id}
    </foreach>
</select>

insert;映射插入语句
update;映射更新语句
delete;映射删除语句
select;映射查询语句
mapper

namespace命名空间
namespace相当于包名(属性值是dao接口的完全限定名),id相当于类里的方法名(与XML文件中的id保持一致)
绑定DAO接口(绑定接口后不需要写接口实现类)
namespace的命名必须跟某个接口同名
接口中的方法与映射文件中SQL语句id一一对应

<?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的路径名必须与dao接口的完全限定名一致-->
<mapper namespace="com.kgc.dao.UserMapper">

select

id;命名空间中唯一的标识符(namespace下的唯一标识)
接口的方法与映射文件中的SQL语句id一一对应
parameterType;传入SQL语句的参数类型的完全限定名或别名(取别名用)
resultType;SQL语句返回值类型的完整类名或别名

<insert id = "add" parameterType = "User">/*参数为数据类型这里使用的是别名*/
     /*SQL语句,添加指定表的数据内容*/
    insert into stu values (null,#{ename},#{esex},#{eage},#{salary})
</insert>
<select id = "findAll" resultType = "User">/*参数为泛型的类型*/
    select * from stu
</select>

参数传递#{ 参数名 }
在这里插入图片描述
多条件查询(传入的参数数据类型为java实体类或map)

List<Student> find(@Param("name") String name,@Param("sex") String sex);

在这里插入图片描述

传入的是对象时直接传
传入的是map集合时与传入对象一样进行传入,但XML中的参数写key值(通过#{map的key获取}),不在是对象属性名(这种方法更加灵活,方便添加参数)
resultMap运用
resultMap;描述如何将结果集映射到java对象中
在这里插入图片描述
查询结果的字段名与pojo的属性一致才能进行自动映射(不一致时可以给字段去别名达到一致)
使用resultMap的方法就可以在字段名与属性名不一致时也能进行自动映射
使用方法将resultType替换成resultMap并且还需要添加一个resultMap的节点
resultMap的id值必须与引用的resultMap的属性值一样,type类型为对应结果的类型
resultMap有一个字节点为result,result有两个属性分别是property和column,property的属性值为查询出来的要赋值给实体对象的属性名称(也就是对象的属性),column的属性值为数据库的字段名(property的属性值是可以和column的属性值不一样的)
通过resultMap可以控制查询结果的显示,达到对关系的属性进行赋值填充

<?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">
<mapper namespace="com.kgc.dao.User2Mapper">
<!--编写resultMap文件-->
   <resultMap id="User2Map" type="User2">
       <id column="eid" property="id"/>
        <result column="ename" property="name"/>
   </resultMap>   
     <!--SQL查询语句-->
   <select id="findAll" resultMap="User2Map">
        select * from  stu
   </select>
</mapper>  

resultType与resultMap的区别
resultType是直接表示返回类型
基础数据类型
复杂数据类型
resultMap是对外部的resultMap的引用
应用场景
数据库字段信息与对象属性不一致
复杂的联合查询,自由控制映射结果
resultMap与resultType不能同时存在,他们本质上都是map数据结构
resultMap的自动映射级别(autoMappingBehavior)
默认映射级别为PARTIAL:自动匹配所要有属性
要实现只对关系的数据进行映射的话要进行对配置文件进行修改(将匹配设为NONE)
代码书写;

<settings>
    <setting name="autoMappingBehavior" value="NONE"/>
</settings>

MyBatis的数据增加(关键字为insert)

在这里插入图片描述
resultMap的高级映射(association)
在这里插入图片描述
association(一对一)
在这里插入图片描述
association是resultMap的一个子元素但他也可以进行引用外部的resultMap
association的property属性值为要映射的数据库列实体对象的属性(也就是javabean中的实体对象属性名)javaType的属性值为property属性的类型(也就是类名或者类型名)
查询结果存在列名相同的情况下必须进行取别名进行赋值(MySQL中的取别名)
在association中的子标签中推荐添加一个id标签(使用他来进行指定主键或联合主键)可以降低性能的开销,提高性能
在实际案例中可以进association中的子标签提取出来存放在一个新的resultMap中从而达到代码复用的一个效果

<?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">
<mapper namespace="com.kgc.dao.User2Mapper">
<!--映射文件主体-->
<!--编写resultMap文件-->
    <resultMap id="User2Map" type="User2">
       <id column="eid" property="id"/>
        <result column="ename" property="name"/>
        <!--property为要映射的属性名javaType 为要映射属性的类名-->
        <association property="sexname" javaType="Sex">
            <id column="eid" property="esex"></id>
            <result property="sexname" column="sexname"></result>
        </association>
    </resultMap>
<!--SQL查询语句-->
   <select id="findAll" resultMap="User2Map">
        select * from  stu
    </select>
    <select id="find" resultMap="User2Map">
        select ename, sexname from sex ,stu where sex.eid=stu.esex
    </select>
</mapper

collection映射(一对多)
在这里插入图片描述
映射结果为一个结果集(或者数据列表)
collection的属性
property;映射到数据库列的实体对象的属性
ofType;完整java类名或别名(集合所包括的类型)
resultMap;引用外部resultMap
值元素
id
result
使用了collection的进输出结果集就要进行添加多重for循环,进行输出指定的内容
提高映射结果的可重用性
通过collection的resultMap的可重用性

<resultMap id="TeaherMapper2" type="Teacher" >
    <id column="tid" property="tid"/>
    <result column="tname" property="tname"/>
    <!--association 的property的属性值为属性名是对象的-->
    <!--JavaType属性值为property属性的类型-->
    <association property="cource" javaType="Course">
        <id property="cid" column="cid"></id>
        <result property="cname" column="cname"></result>
    </association>
</resultMap>

collection和association都是用来进行数据对象赋值的,里面的代码为对对象的属性赋值,要写好映射关系(column是数据库的字段名,property是属性名)
缓存和resultMap的自动映射级别
在这里插入图片描述
代码书写
MyBatis的缓存
一级缓存;基于MyBatis自带的hasmap的一个本地缓存,作用范围只在session中(session刷新或者关闭缓存会清空)
二级缓存;全局缓存,能被所有的SQLsession共享
二级缓存的配置
在核心文件中进行配置

<settings>
    <setting name ="cacheEnabled" vaule="true"/>
</settings>   

在mapperXML文件中设置缓存,默认情况下是未开启的(只针对MapperXML文件)

<cache eviction = "FIFO" flushlnterval = "60000" 
    size = "521" readOnly = "true"/>

在MapperXML文件配置支持cache后可以对个别查询进行调整(添加useCache=“true”)

<select id = "selectAll" resultType = "Emp" useCache = "true">

动态SQL

动态SQL
在这里插入图片描述
动态SQL基于OGNL表达式
动态SQL的元素
if;相当于java中的if判断

select * from stu
  where 1=1
    <if test="name!=null">/*if动态SQL语句相当于if判断语句*/
        and sname
        like CONCAT ('%',#{name},'%')
    </if>
    <if test="sex!=null">/*test中的属性直接书写判断的属性名,不需要添加#{}*/
        and ssex=#{sex}
    </if>

trim;灵活的去除一些多余的关键字
trim的属性
prefix;前缀(SQL语句中的条件,如果有返回值便会添加前缀中的属性值)
suffix;后缀
prefixOverrides;剔除(前缀的)(判断是否需要and或or,如果不需要会自动的覆盖and或or)
suffixOverrides;剔除(后缀的)

select * from stu
  <!--prdfix前缀prefixOverrides删除不需要的and或or-->
 <trim prefix="where" prefixOverrides="and|or">
     <if test="name!=null">
         and sname
         like CONCAT ('%',#{name},'%')
     </if>
     <if test="sex!=null">
         and ssex=#{sex}
     </if>
 </trim>

使用他可以替换掉where和更灵活的多余的关键字
where;用来简化where的条件判断
通过添加where标签可以判断添加是否有返回值,有的话添加指定的SQLwhere条件(会自动识别要添加的关键字是否需要),没有的话就不添加
智能的处理了where条件后的and和or关键字(是否需要)

select * from stu
<where>//去掉where后的and或or
    <if test="name!=null">
        and sname
        like CONCAT ('%',#{name},'%')
    </if>
    <if test="sex!=null">
        and ssex=#{sex}
    </if>
</where>

set;用来解决动态的更新语句

update stu
<set>
    <if test="sname!=null">sname=#{sname},</if>
    <if test="ssex!=null">ssex=#{ssex},</if>
    <if test="sage!=null">sage=#{sage},</if>
</set>

choose;相当于switch语句

<select id="findName" resultMap="stuTeaMapper">
        select * from stu
            <where>
                <choose>
                    <when test="name!=null">sname
                        like CONCAT('%',#{name},'%')
                    </when>
                    <when test="sex!=null">
                        ssex=#{sex}
                    </when>
                    <otherwise></otherwise>
                </choose>
            </where>
    </select>

foreach;用来迭代一个集合
动态SQL条件实现数据修改
set;用来解决动态的更新语句(在实际的案例中不会使用set与if进行嵌套使用,一般是if与where进行嵌套)
更新修改数据时,修改指定的字段出现了其余字段为null的时候进行使用if+set方法,通过if+set方法可以对没有添加修改的字段的数据不进行刷新只更新添加了修改的字段的属性
在这里插入图片描述

choose;相当于switch语句
在这里插入图片描述

foreach;用来迭代一个集合
在这里插入图片描述
foreach通常使用在in条件中
item属性为集合中进行迭代的一个别名
index属性为集合中每次迭代的位置(相当于下标)
collection属性;必须进行指定
list;入参的参数类型为list的时候进行使用
array;入参的参数类型为数组的时候进行使用
key(map中的key值);入参的参数类型为map的时候进行使用
open属性表示SQL以什么语句开始
separator属性表示SQL以什么语句进行间隔
close属性表示SQL语句以什么语句进行结束

 <select id="findById" resultMap="stuTeaMapper">
        select * from stu
          <foreach collection="list"
                   item="id"
                        open="where sid in ("
                            separator=","
                                close=")">
              #{id}
          </foreach>
    </select>

    <select id="findById2" resultMap="stuTeaMapper" parameterType="Student">
        select * from stu
        <!--map集合的Kay键值-->
        <foreach collection="ids"
                 item="id"
                 open="where sid in ("
                 separator=","
                 close=")">
            #{id}
        </foreach>
    </select>
    <sql id="contents">sid,sname,sage,ssex</sql><!--加载重复的SQL语句是使用-->
    <select id="findByIdMap" resultMap="stuTeaMapper">
        select
         <include refid="contents"/>/*导入SQL语句*/
         from stu
        <foreach collection="idsKey"
                 item="id"
                 open="where sid in ("
                 separator=","
                 close=")">
            #{id}
        </foreach>
    </select>
    <insert id="add" parameterType="Student">
        insert into stu values (null,#{sname},#{ssex},#{sage},#{tid})
        <selectKey resultType="int" keyProperty="sid" order="AFTER">
            select LAST_INSERT_ID()
        </selectKey>
    </insert>

在MyBatis的传参中会把参数封装成Map进行入参(不管是单个的参数还是多个的参数都是如此),单参数也可以手动的封装层map集合
MyBaits接受的参数类型;接受的参数类型一共有五种,基本参数类型、数组、对象、map与list
MyBatis分页功能

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值