xml映射文件

1、mapper xml文件

SQL 映射文件有不多的幾個頂級元素(按照它們應該被定義的順序):java

cache – 給定命名空間的緩存配置。
cache-ref – 其餘命名空間緩存配置的引用。
resultMap – 是最複雜也是最強大的元素,用來描述如何從數據庫結果集中來加載對象。
parameterMap – 已廢棄!老式風格的參數映射。內聯參數是首選,這個元素可能在未來被移除,這裏不會記錄。
sql – 可被其餘語句引用的可重用語句塊。
insert – 映射插入語句
update – 映射更新語句
delete – 映射刪除語句
select – 映射查詢語句

一、sql

select
<select id="selectPerson" parameterType="int" resultType="hashmap">
  SELECT * FROM PERSON WHERE ID = #{id}
</select>

(1)接受一個 int(或 Integer)類型的參數數據庫

(2)返回一個 HashMap 類型的對象緩存

(3)select 元素有不少屬性容許你配置,來決定每條語句的做用細節mybatis

<select
  id="selectPerson"
  parameterType="int"
  parameterMap="deprecated"
  resultType="hashmap"
  resultMap="personResultMap"
  flushCache="false"
  useCache="true"
  timeout="10000"
  fetchSize="256"
  statementType="PREPARED"
  resultSetType="FORWARD_ONLY">

二、insert, update 和 deleteapp

(1)配置dom

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

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

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

(2)sql示例學習

<insert id="insertAuthor">
  insert into Author (id,username,password,email,bio)
  values (#{id},#{username},#{password},#{email},#{bio})
</insert>

<update id="updateAuthor">
  update Author set
    username = #{username},
    password = #{password},
    email = #{email},
    bio = #{bio}
  where id = #{id}
</update>

<delete id="deleteAuthor">
  delete from Author where id = #{id}
</delete>

(3)生成主鍵fetch

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

(4)多行插入code

<insert id="insertAuthor" useGeneratedKeys="true"
    keyProperty="id">
  insert into Author (username, password, email, bio) values
  <foreach item="item" collection="list" separator=",">
    (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
  </foreach>
</insert>

三、sql

(1)定義可重用的 SQL 代碼段,能夠包含在其餘語句中:

<sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>

這個 SQL 片斷能夠被包含在其餘語句中:

<select id="selectUsers" resultType="map">
  select
    <include refid="userColumns"><property name="alias" value="t1"/></include>,
    <include refid="userColumns"><property name="alias" value="t2"/></include>
  from some_table t1
    cross join some_table t2
</select>

屬性值也能夠被用在 include 元素的 refid 屬性裏:

<include refid="${include_target}"/>

或 include 內部語句中:

${prefix}Table

四、參數(Parameters)

(1)參數也能夠指定一個特殊的數據類型:

#{property,javaType=int,jdbcType=NUMERIC}

(2)指定一個特殊的類型處理器類(或別名)

#{age,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler}

(3)肯定小數點後保留的位數

#{height,javaType=double,jdbcType=NUMERIC,numericScale=2}

(4)mode 屬性容許你指定 IN,OUT 或 INOUT 參數

2、Result Maps

一、一個 JavaBean 能夠被映射到 ResultSet

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

二、類型別名

<!-- In mybatis-config.xml file -->
<typeAlias type="com.someapp.model.User" alias="User"/>

<!-- In SQL Mapping XML file -->
<select id="selectUsers" resultType="User">
  select id, username, hashedPassword
  from some_table
  where id = #{id}
</select>

三、能夠在 SELECT 語句中對列使用別名(這是一個 基本的 SQL 特性)來匹配標籤

<select id="selectUsers" resultType="User">
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password     as "hashedPassword"
  from some_table
  where id = #{id}
</select>

四、使用外部的 resultMap

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

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

五、高級結果映射

有待學習。。。。。

六、自動映射

(1)須要將 mapUnderscoreToCamelCase設置爲true。

(2)id 和 userName列將被自動映射, hashed_password 列將根據配置映射

<select id="selectUsers" resultMap="userResultMap">
  select
    user_id             as "id",
    user_name           as "userName",
    hashed_password
  from some_table
  where id = #{id}
</select>

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

(3)三種自動映射等級:

NONE - 禁用自動映射。僅設置手動映射屬性。
PARTIAL - 將自動映射結果除了那些有內部定義內嵌結果映射的(joins).
FULL - 自動映射全部。

七、緩存

相關文章
相關標籤/搜索