MyBatisx详解之SQL映射XML文件

来源:互联网 发布:广州文豆php培训学校 编辑:程序博客网 时间:2024/05/17 04:29
           SQL映射XML文件
           
MyBatis 真正强大之处就在这些映射语句:
SQL 映射XML 文件只有一些基本的元素需要配置,并且要按照下面的顺序来定义:


1.cache : 在特定的命名空间配置缓存
2.cache-ref :引用另外一个命名空间配置的缓存
3.resultMap :最复杂也是最强大的元素,用来描述如何从数据库结果集里加载对象。  --  完成 ORM 映射的关键
4.sql:能被其他语句重用的SQL块
5.insert –INSERT 映射语句
6.update –UPDATE 映射语句
7.delete –DELEETE 映射语句
8.select –SELECT 映射语句






Select 元素;


<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
这条语句叫做selectPerson,以int 型(或者Integer 型)作为参数,并返回一个以数据库
列名作为键值的HashMap。


参数的表示方法: #{参数}


select元素中的常用数属性:
<select
id=”selectPerson”
parameterType=”int”
resultType=”hashmap”
resultMap=”personResultMap”
flushCache=”false”
useCache=”true”
timeout=”10000”
fetchSize=”256”
statementType=”PREPARED”
>


Select 映射语句中的属性说明:


1.id --- 在这个命名空间下唯一的标识符,可被其它语句引用
2.parameterType --- 传给此语句的参数的完整类名或别名  --- 对象或简单数据类型
3.flushCache --- 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false
4.timeout --- 设置超时时间
5.fetchSize --- 设置从数据库获得记录的条数,默认没有设置,由驱动器自己决定
6.statementType --- 可设置为STATEMENT,PREPARED 或CALLABLE 中的任意一个,告诉MyBatis 分
别使用Statement,PreparedStatement或者CallableStatement。默认:PREPARED


* resultType --- 语句返回值类型的完整类名或别名。注意,如果返回的是集合
(collections),那么应该是集合所包含的具体子类型,而不是集合本身。resultType 与resultMap 不能同时使用


* resultMap --- 引用的外部定义的resultMap。结果集映射是MyBatis 中最强大的特性,同时
又非常好理解。许多复杂的映射都可以轻松解决。resultType 与resultMap不能同时使用




Insert、update、delete 元素:


<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
useGeneratedKeys=""
timeout="20000">


<update
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">


<delete
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20000">




Insert、update、delete 映射语句中的属性说明:


1.id --- 在这个命名空间下唯一的标识符,可被其它语句引用
2.parameterType --- 传给此语句的参数的完整类名或别名  --- 对象或简单数据类型
3.flushCache --- 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false
4.timeout --- 设置超时时间
5.statementType --- 可设置为STATEMENT,PREPARED 或CALLABLE 中的任意一个,告诉MyBatis 分
别使用Statement,PreparedStatement或者CallableStatement。默认:PREPARED


Insert 元素独有的:


* useGeneratedKeys --- (仅限insert 语句时使用)告诉MyBatis 使用JDBC 的getGeneratedKeys 方
法来获取数据库自动生成主键(如:MySQL、SQLSERVER 等关系型数据库会有自增的字段)。默认:false


* keyProperty --- (仅限insert 语句时使用)设置自动生成主键的字段,这个字段的值由
getGeneratedKeys 方法返回,或者由insert 元素的selectKey 子元素返回。默认不设置。




例子: 自动生成主键:  keyProperty设置id为自动生成的id


<insert id="insertAuthor" parameterType="domain.blog.Author"
useGeneratedKeys=”true” keyProperty=”id”>
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>


随机生成id并插入:


<insert id="insertAuthor" parameterType="domain.blog.Author">
<selectKey keyProperty="id" resultType="int" order="BEFORE">
select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
</selectKey>
insert into Author
(id, username, password, email,bio, favourite_section)
values
(#{id}, #{username}, #{password}, #{email}, #{bio},
#{favouriteSection,jdbcType=VARCHAR}
)
</insert>


selectKey 语句首先会执行,Author 表的ID 首先会被设值,然后才会调用
insert 语句。这相当于在您的数据库中自动生成键值,不需要编写复杂的java 代码。


selectKey 元素描述如下:
<selectKey
keyProperty="id"
resultType="int"
order="BEFORE"
statementType="PREPARED">


属性说明: 
1. keyProperty --- 设置需要自动生成键值的列
2. resultType --- 返回类型,主键的类型可以是int也可以是String类型
3. order --- 可以设成BEFORE 或者AFTER,如果设为BEFORE,那它会先选择主键,然后设置keyProperty,
再执行insert 语句;如果设为AFTER,它就先执行insert语句再执行selectKey 语句,像数据库如Oracle 
那样在insert 语句中调用内嵌的序列机制一样。






         Sql 元素


这个元素用来定义能够被其它语句引用的可重用SQL 语句块。例如:
<sql id=”userColumns”> id,username,password </sql>
这个SQL 语句块能够被其它语句引用,如:
<select id=”selectUsers” parameterType=”int” resultType=”hashmap”>
select <include refid=”userColumns”/>
from some_table
where id = #{id}
</select>




         resultMap --- JavaBean和数据库结果集的映射
         
使用简单的配置语句而不需要详细地处理结果集映射


JavaBean 可以像HashMap 一样简单地映射到ResultSet 结果集。


<select id=”selectUsers” parameterType=”int”
resultType=”com.someapp.model.User”>
select id, username, hashedPassword
from some_table
where id = #{id}
</sql>


使用别名:


<!-- In Config XML file -->
<typeAlias type=”com.someapp.model.User” alias=”User”/>
<!-- In SQL Mapping XML file -->
<select id=”selectUsers” parameterType=”int”
resultType=”User”>
select id, username, hashedPassword
from some_table
where id = #{id}
</sql>




开发中常用的方式:


解决列名不匹配的方法。


<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>


这个语句将会被resultMap 属性引用(注意,我们没有使用resultType)。如:


<select id=”selectUsers” parameterType=”int” resultMap=”userResultMap”>
select user_id, user_name, hashed_password
from some_table
where id = #{id}
</sql>
0 0
原创粉丝点击