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 参数
一、一个 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 - 自动映射全部。
七、缓存