SQL 映射的 XML 文件

MyBatis 真正的力量是在映射语句中。这里是奇迹发生的地方。 对于所有的力量, SQL
映射的 XML 文件是相当的简单。当然如果你将它们和对等功能的 JDBC 代码来比较,你会
发现映射文件节省了大约 95%的代码量。 MyBatis 的构建就是聚焦于 SQL 的,使其远离于
普通的方式。

SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):
 cache - 配置给定命名空间的缓存。
 cache-ref – 从其他命名空间引用缓存配置。
 resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载你的对象。
 sql – 可以重用的 SQL 块,也可以被其他语句引用。
 insert – 映射插入语句
 update – 映射更新语句
 delete – 映射删除语句
 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”           <!--将会传入这条语句的参数类的完全限定名或别名。-->
parameterMap=”deprecated”   
resultType=”hashmap”         <!--返回的期望类型的类的完全限定名或别名。注意集合情形,那应该是集合可以包含的类型,而不能是集   
                                 合本身。使用 resultType 或 resultMap,但不能同时使用>
resultMap=”personResultMap”    <!--命名引用外部的 resultMap。返回 map 是 MyBatis 最具力量的特性,对其有一个很好的理解的话,许    
                                 多复杂映射的情形就能被解决了。使用 resultMap 或 resultType,但不能同时使用。-->
flushCache=”false”             <!--将其设置为 true, 无论语句什么时候被调用,都会导致缓存被清空。默认值: false。-->
useCache=”true”                <!--将其设置为 true,将会导致本条语句的结果被缓存。默认值: true。>
timeout=”10000”                <!--这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。
                                    默认不设置(驱动自行处理)。-->
fetchSize=”256”               <!--这是暗示驱动程序每次批量返回的结果行数。默认不设置(驱动自行处理)。-->
statementType=”PREPARED”      <!--STATEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis使用选择使用 Statement,  
                                                                    PreparedStatement 或 CallableStatement。默认值: PREPARED。-->
resultSetType=”FORWARD_ONLY”   <!--FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE中的一种。
                                     默认是不设置(驱动自行处理)。-->
>

 

insert, update, delete
数据修改语句 insert, update 和 delete 在它们的实现中非常相似:
<insert   id="insertAuthor"  parameterType="domain.blog.Author"   flushCache="true"
          statementType="PREPARED"  keyProperty=""  useGeneratedKeys=""     timeout="20000">
支持自动生成主键的数据库可以使用这个
<insert id="insertAuthor" parameterType="domain.blog.Author"     useGeneratedKeys=”true” keyProperty=”id”>
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
不支持的可以使用这个
<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>

 

属性描述:
keyProperty:selectKey 语句结果应该被设置的目标属性
resultType    :结果的类型。 MyBatis 通常可以算出来,但是写上也没有问题。MyBatis 允许任何简单类型用作主键的类型,包括字符串。
order    :这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键,设置 keyProperty 然后执行插入语句。如果

          设置为 AFTER,那么先执行插入语句,然后是 selectKey 元素-这和如 Oracle 数据库相似,可以在插入语句中嵌入序列调用。

statementType  :  MyBatis 支持 STATEMENT, PREPARED 和CALLABLE 语句的映射类型,
                分别代表 PreparedStatement 和CallableStatement 类型。
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>

 

字符串替换
默认情况下,使用#{}格式的语法会导致 MyBatis 创建预处理语句属性并以它为背景设
置安全的值(比如?)。这样做很安全,很迅速, 也是首选的做法,有时你只是想直接在 SQL
语句中插入一个不改变的字符串。比如,像 ORDER BY,你可以这样来使用:
ORDER BY ${columnName}
这里 MyBatis 不会修改或转义字符串。
重要: 接受从用户输出的内容并提供给语句中不变的字符串,这样做是不安全的。这会
         导致潜在的 SQL 注入攻击,因此你不应该允许用户输入这些字段,或者通常自行转义并检查。
resultMap
----------------------------------将返回的结果映射到Map
<select id=”selectUsers” parameterType=”int” resultType=”hashmap”>
    select id, username, hashedPassword from some_table   where id = #{id}
</select>
----------------------------------将返回的结果映射到Bean
<select id=”selectUsers” parameterType=”int”   resultType=”com.someapp.model.User”>
         select id, username, hashedPasswordfrom some_tablewhere id = #{id}
</select>
这个可以输入类的别名,而不用输入类的全路径
<!-- 在XML配置文件中-->
<typeAlias type=”com.someapp.model.User” alias=”User”/>
<!-- 在SQL映射的XML文件中-->
<select id=”selectUsers” parameterType=”int”   resultType=”User”>
    select id, username, hashedPassword   from some_table    where id = #{id}
</select>
 
如果列名没有精确匹配,你可以在列名上使用 select 字句的别名(一个
标准的 SQL 特性)来匹配标签
<select id=”selectUsers” parameterType=”int” resultType=” User”>
       select   user_id as “id”,  user_name as “userName”,   hashed_password as “hashedPassword”
       from some_table   where id = #{id}
</select>

 

》》》解决列名不匹配的另外一种方式
<resultMap id="userResultMap" type="User">
   <id property="id" column="user_id" />
   <result property="username" column="user_name"/>
   <result property="password" column="hashed_password"/>
</resultMap>

 

引用它的语句使用 resultMap 属性就行了(注意我们去掉了 resultType 属性)。比如:

<select id=”selectUsers” parameterType=”int”    resultMap=”userResultMap”>
    select user_id, user_name, hashed_password    from some_table    where id = #{id}
</select>

 

posted @ 2016-12-26 14:16  卖臭豆腐喽  阅读(2137)  评论(0编辑  收藏  举报