标签:mybatis
Mybatis 的应用程序都以一个sqlSessionFactory 对象的实例为核心。SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象来获得。SqlSessionFactoryBuilder对象可以从XML配置文件或从Configuration类的习惯准备的实例中构建SqlSessionFactory对象。
SQL映射文件的几个顶级元素(按照它们应该被定义的顺序):
cache配置给定命名空间的引用缓存的配置
cache-ref 从其他命名空间引用缓存配置
resultMap用来描述数据库结果集和对象的对应关系
sql可以重用的SQL块,也可以被其他语句引用
insert映射插入语句
update映射更新语句
delete映射删除语句
select映射查询语句
Select属性描述
Id在命名空间中唯一的标识符,可以被用来引用的语句。
parameterType将会传入这条语句的参数类的完全限定名或别名
resultType从这条语句中返回的期望类型的类的完全限定名或别名。注意集合情形,那 应 该是集合可以包含的类型,而不能是集合本身,使用resultType或resultMap,但不 能同时使用
resultMap命名引用外部的resultMap
flushCache将其设置为true,不论语句什么时候被调用,都会导致缓存被清空,默认值 为false
useCache将其设置为true,将会导致语句的结果被缓存。默认值为true
timeOut这个设置驱动程序等待数据库返回请求结果,并抛出异常事件的最大等待值
默认不设置(驱动自行处理)
fetchSize这个暗示驱动程序每次批量返回的结果数
statementType:STATEMENT.PREPARED或CALLABLE的一种。让mybatis 选择使用
Stetament,PreparedStatement或CallableStatement。默认值PREPARED.
resultSetType:FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE中的一 种,默认是不设置(驱动自行处理)
Insert特有属性
useGeneratedKeys这会告诉Mybatis使用JDBC的getGeneratedKeys方法来取出由数据 内部生成的主键。默认值false
keyProperty标记一个属性,MyBatis会通过getGeneratedKeys或者通过insert语句的selectKey子元素设置它的值。默认不设置
MyBatis的动态SQL是基于OGNL表达式的。实现动态SQL的主要元素有:if,choose(when,otherwise),trim,where,set,foreach
if就是简单的条件判断,利用if语句我们可以实现某些简单的条件选择
choose元素的作用就相当于JAVA中的switch语句,基本上跟JSTL中的choose的作用和用法是一样的,通常都是与when和otherwise搭配的
trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是prefix和suffix;可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,对应的属性是prefixOverrides和suffixOverrides;正因为trim有这样的功能,所以我们也可以非常简单的利用trim来代替where元素的功能
set元素主要是用在更新操作的时候,它的主要功能和where元素其实是差不多的,主要是在包含的语句前输出一个set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果set包含的内容为空的话则会出错。有了set元素我们就可以动态的更新那些修改了的字段
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key
联合元素用来处理“一对一”的关系。需要指定映射的Java实体类的属性,属性的javaType(通常MyBatis 自己会识别)。对应的数据库表的列名称。如果想覆写的话返回结果的值,需要指定typeHandler。
不同情况需要告诉MyBatis 如何加载一个联合。MyBatis 可以用两种方式加载:
1. select: 执行一个其它映射的SQL 语句返回一个Java实体类型。较灵活;
2. resultsMap: 使用一个嵌套的结果映射来处理通过join查询结果集,映射成Java实体类型。
聚集元素用来处理“一对多”的关系。需要指定映射的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
Id、result属性如下:
Attribute |
Description |
property |
映射数据库列的字段或属性。如果JavaBean 的属性与给定的名称匹配,就会使用匹配的名字。否则,MyBatis 将搜索给定名称的字段。两种情况下您都可以使用逗点的属性形式。比如,您可以映射到“username”,也可以映射到“address.street.number”。 |
column |
数据库的列名或者列标签别名。与传递给resultSet.getString(columnName)的参数名称相同。 |
javaType |
完整java类名或别名(参考上面的内置别名列表)。如果映射到一个JavaBean,那MyBatis 通常会自行检测到。然而,如果映射到一个HashMap,那您应该明确指定javaType 来确保所需行为。 |
jdbcType |
这张表下面支持的JDBC类型列表列出的JDBC类型。这个属性只在insert,update或delete 的时候针对允许空的列有用。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>
标签:mybatis
原文地址:http://blog.csdn.net/strutce/article/details/46340007