mybatis学习



Mybatis 的应用程序都以一个sqlSessionFactory 对象的实例为核心。SqlSessionFactory对象的实例能够通过SqlSessionFactoryBuilder对象来获得。SqlSessionFactoryBuilder对象能够从XML配置文件或从Configuration类的习惯准备的实例中构建SqlSessionFactory对象。

 

SQL映射文件的几个顶级元素(依照它们应该被定义的顺序):

cache配置给定命名空间的引用缓存的配置

cache-ref 从其它命名空间引用缓存配置

resultMap用来描写叙述数据库结果集和对象的相应关系

sql能够重用的SQL块。也能够被其它语句引用

insert映射插入语句

update映射更新语句

delete映射删除语句

select映射查询语句

 

Select属性描写叙述

Id在命名空间中唯一的标识符。能够被用来引用的语句。

parameterType将会传入这条语句的參数类的全然限定名或别名

resultType从这条语句中返回的期望类型的类的全然限定名或别名。注意集合情形,那 应 该是集合能够包括的类型,而不能是集合本身。使用resultTyperesultMap,但不 能同一时候使用

resultMap命名引用外部的resultMap

flushCache将其设置为true,不论语句什么时候被调用,都会导致缓存被清空,默认值 为false

useCache将其设置为true,将会导致语句的结果被缓存。默认值为true

timeOut这个设置驱动程序等待数据库返回请求结果,并抛出异常事件的最大等待值

默认不设置(驱动自行处理)

fetchSize这个暗示驱动程序每次批量返回的结果数

statementTypeSTATEMENT.PREPAREDCALLABLE的一种。

mybatis 选择使用

StetamentPreparedStatementCallableStatement

默认值PREPARED.

resultSetTypeFORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE中的一 种,默认是不设置(驱动自行处理)

 

Insert特有属性

useGeneratedKeys这会告诉Mybatis使用JDBCgetGeneratedKeys方法来取出由数据 内部生成的主键。默认值false

keyProperty标记一个属性。MyBatis会通过getGeneratedKeys或者通过insert语句的selectKey子元素设置它的值。默认不设置

 

 

MyBatis的动态SQL是基于OGNL表达式的。实现动态SQL的主要元素有:ifchoosewhenotherwise),trimwheresetforeach

 

if就是简单的条件推断。利用if语句我们能够实现某些简单的条件选择

 

choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和使用方法是一样的,通常都是与when和otherwise搭配的

 

trim元素的主要功能是能够在自己包括的内容前加上某些前缀,也能够在其后加上某些后缀,与之相应的属性是prefix和suffix。能够把包括内容的首部某些内容覆盖,即忽略,也能够把尾部的某些内容覆盖。相应的属性是prefixOverrides和suffixOverrides;正由于trim有这种功能,所以我们也能够很easy的利用trim来取代where元素的功能

set元素主要是用在更新操作的时候。它的主要功能和where元素事实上是几乎相同的,主要是在包括的语句前输出一个set,然后假设包括的语句是以逗号结束的话将会把该逗号忽略。假设set包括的内容为空的话则会出错。

有了set元素我们就能够动态的更新那些改动了的字段

foreach的主要用在构建in条件中,它能够在SQL语句中进行迭代一个集合。foreach元素的属性主要有item。index,collection。open,separator,close。

item表示集合中每个元素进行迭代时的别名。index指定一个名字。用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么開始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最easy出错的就是collection属性,该属性是必须指定的,可是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

1. 假设传入的是单參数且參数类型是一个List的时候。collection属性值为list

2. 假设传入的是单參数且參数类型是一个array数组的时候。collection的属性值为array

3. 假设传入的參数是多个的时候,我们就须要把它们封装成一个Map了,当然单參数也能够封装成map,实际上假设你在传入參数的时候。在MyBatis里面也是会把它封装成一个Map的。map的key就是參数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key

 

association级联查询

联合元素用来处理“一对一”的关系。须要指定映射的Java实体类的属性,属性的javaType(通常MyBatis 自己会识别)。相应的数据库表的列名称。假设想覆写的话返回结果的值,须要指定typeHandler。
不同情况须要告诉MyBatis 怎样载入一个联合。

MyBatis 能够用两种方式载入:

1. select: 运行一个其他映射的SQL 语句返回一个Java实体类型。较灵活;
2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

collection聚集

聚集元素用来处理“一对多”的关系。须要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);相应的数据库表的列名称;
不同情况须要告诉MyBatis 怎样载入一个聚集。MyBatis 能够用两种方式载入:

1. select: 运行一个其他映射的SQL 语句返回一个Java实体类型。较灵活;
2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。

 

 

 

resultMap的元素

·constructor实例化的时候通过构造器将结果集注入到类中

oidArg– ID 參数; 将结果集标记为ID。以方便全局调用

oarg注入构造器的结果集

·id结果集ID,将结果集标记为ID,以方便全局调用

·result注入一个字段或者javabean属性的结果

·association复杂类型联合;很多查询结果合成这个类型

o嵌套结果映射– associations能引用自身,或者从其他地方引用

·collection复杂类型集合

o嵌套结果映射– collections能引用自身,或者从其他地方引用

·discriminator–使用一个结果值以决定使用哪个resultMap

ocase基于不同值的结果映射

§嵌套结果映射–case也能引用它自身, 所以也能包括这些相同的元素。它也能够从外部引用resultMap

 

Idresult属性例如以下:

 

Attribute

Description

property

映射数据库列的字段或属性。假设JavaBean 的属性与给定的名称匹配,就会使用匹配的名字。否则。MyBatis 将搜索给定名称的字段。

两种情况下您都能够使用逗点的属性形式。

比方,您能够映射到“username”,也能够映射到“address.street.number”

column

数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的參数名称同样。

javaType

完整java类名或别名(參考上面的内置别名列表)。假设映射到一个JavaBean。那MyBatis 一般会自行检測到。然而,假设映射到一个HashMap,那您应该明白指定javaType 来确保所需行为。

jdbcType

这张表以下支持的JDBC类型列表列出的JDBC类型。这个属性仅仅在insertupdatedelete 的时候针对同意空的列实用。

JDBC 须要这项。但MyBatis 不须要。假设您直接编写JDBC代码,在同意为空值的情况下须要指定这个类型。

typeHandler

我们已经在文档中讨论过默认类型处理器。使用这个属性能够重写默认类型处理器。

它的值能够是一个TypeHandler实现的完整类名,也能够是一个类型别名。

 

<?

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.study.mybatis.StudyMybatis"> <!-- 取别名 com.study.mybatis.model.StudyMybatis为表的模型类 column 表字段名 property 映射表字段的名称 --> <typeAlias type="com.study.mybatis.model.StudyMybatis" alias="StudyMybatisMap"/> <resultMap type="StudyMybatisMap" id="selectStudyMybatis"> <id property="id" column="studymybatis_id"/> <result property="name" column="studymybatis_name"/> <result property="password" column="studymybatis_password"/> <result property="phone" column="studymybatis_phone"/> <result property="classid" column="studymybatis_classid"/> <result/> </resultMap> <!-- Id在命名空间中唯一的标识符,能够被用来引用的语句 parameterType将会传入这条语句的參数类的全然限定名或别名 resultMap命名引用外部的resultMap --> <select id="selectByid" resultMap="selectStudyMybatis" parameterType="int"> select studymybatis_id,studymybatis_name,studymybatis_password,studymybatis_phone from studymybatis where id=#{id} </select> <!-- trim样例 --> <select id="selectByExample" resultMap="selectStudyMybatis" parameterType="java.util.Map"> select studymybatis_id,studymybatis_name,studymybatis_password,studymybatis_phone from studymybatis sm <trim prefix="where" prefixOverrides="AND | OR"> <if test="studymybatis_name !=null and studymybatis_name=''"> and sm.studymybatis_name = #{name} </if> <if test="classid != null and classid != ''"> and sm.studymybatis_classid=#{classid} </if> </trim> </select> <resultMap type="StudyMybatisMap" id="StudyMybatisAndStudyClass" extends="com.study.mybatis.StudyClass.studyClassMap"> <result property="name" column="studymybatis_name"/> <result property="password" column="studymybatis_password"/> <result property="phone" column="studymybatis_phone"/> <result property="classid" column="studymybatis_classid"/> </resultMap> <select id="selectStudentClass" parameterType="java.math.BigDecimal" resultMap="StudyMybatisMap"> select studymybatis_name,studymybatis_password,<include refid="studyclass_list"/> from studymybatis st left outer join studyclass sc on st.studymybatis_classid=sc.classid where sc.studymybatis_id=#{id} </select> <!-- 动态语句 if样例 --> <select id="ifSelect" parameterType="java.util.Map" resultType="StudyMybatisMap"> select * from studymybatis where 1=1 <if test="name !=null and name!=''"> and studymybatis_name=#{name} </if> <if test="phone !=null and phone !=''"> and studymybatis_phone=#{phone} </if> </select> <!-- choose样例 --> <select id="chooseSelect" parameterType="java.util.Map" resultType="StudyMybatisMap"> select * from studymybatis where 1=1 <choose> <when test="name !=null and name!=''"> and studymybatis_name=#{name} </when> <when test="phone !=null and phone !=''"> and studymybatis_phone=#{phone} </when> <otherwise> and studymybatis_classid="1" </otherwise> </choose> </select> <!-- where样例 --> <select id="whereSelect" parameterType="java.util.Map" resultType="StudyMybatisMap"> select * from studymybatis <where> <if test="name !=null and name!=''"> and studymybatis_name=#{name} </if> <if test="phone !=null and phone !=''"> and studymybatis_phone=#{phone} </if> </where> </select> <!-- set样例 --> <select id="setSelect" parameterType="java.util.Map" resultType="StudyMybatisMap"> update studymybatis <set> <if test="name !=null and name!=''"> and studymybatis_name=#{name} </if> <if test="phone !=null and phone !=''"> and studymybatis_phone=#{phone} </if> </set> where studymybatis_id=#{id} </select> <!-- where样例 --> <select id="whereSelect" parameterType="java.util.Map" resultType="StudyMybatisMap"> select * from studymybatis where studymybatis_classid in <foreach collection="array" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select> <!-- 级联查询 --> <resultMap type="com.study.mybatis.model.StudyMybatis" id="studySelect"> <id property="id" column="studymybatis_id"/> <result property="name" column="studymybatis_name"/> <association property="cid" column="classid" javaType="com.study.mybatis.StudyClass" select="classSelect"/> </resultMap> <select id="selectStudy" resultMap="studySelect" parameterType="java.math.BigDecimal"> select * from studymybatis where studymybatis_id=#{id} </select> <select id="selectClass" resultMap="classSelect" parameterType="java.math.BigDecimal"> select * from studyclass where classid=#{cid} </select> <!-- 或 --> <resultMap type="com.study.mybatis.model.StudyMybatis" id="studySelect"> <id property="id" column="studymybatis_id"/> <result property="name" column="studymybatis_name"/> <association property="cid" column="classid" javaType="com.study.mybatis.StudyClass" select="classSelect"/> </resultMap> <select id="selectStudy" resultMap="studySelect" parameterType="java.math.BigDecimal"> select * from studymybatis sm left outer join studyclass sc on sm.studymybatis_id=sc.classid where studymybatis_id=#{id} </select> <!-- 聚合样例在studyclass.xml --> </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.study.mybatis.StudyClass"> <resultMap type="com.study.mybatis.model.StudyClass" id="studyClassMap"> <id property="id" column="classid"/> <result property="classage" column="classage"/> <result property="banji" column="banji"/> </resultMap> <!-- sql能够被用来定义可重用的SQL代码段,能够包括在其它语句中 --> <sql id="studyclass_list">id,classage,banji</sql> <!-- 聚集 --> <resultMap type="com.study.mybatis.model.StudyClass" id="classResultMap"> <id property="id" column="classid"/> <result property="classage" column="classage"/> <result property="banji" column="banji"/> <association property="studymybatisid" column="studymybatis_id" resultMap="com.study.mybatis.model.StudyMybatis" /> <collection property="studymybatisList" column="classid" javaType="ArrayList" ofType="StudyMybatis" resultMap="com.study.mybatis.model.StudyMybatis"></collection> </resultMap> <select id="classandmybatis" parameterType="int" resultMap="classResultMap"> select * from studyclass sc left outer join studymybatis sm on sc.classid=sm.studymybatis_classid where sc.banji=${banji} </select> </mapper>



 

原文地址:https://www.cnblogs.com/cynchanpin/p/6906889.html