MyBatis学习总结二——SQL映射配置
1.SQL映射配置文件的结构概览
- cache – 配置给定命名空间的缓存。
- cache-ref – 从其他命名空间引用缓存配置。
- resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载你的对象。
- parameterMap – 已经被废弃了!老式风格的参数映射。内联参数是首选,这个元 素可能在将来被移除。这里不会记录。
- sql – 可以重用的 SQL 块,也可以被其他语句引用。
- insert – 映射插入语句
- update – 映射更新语句
- delete – 映射删除语句
- select – 映射查询语句
2.select元素
简单示例:
<select id=”selectPerson” parameterType=”int” resultType=”hashmap”> SELECT * FROM PERSON WHERE ID = #{id} </select>
这段sql语句的意思是传入一个int类型的参数给select语句,返回的结果集保存在hashmap中,hashmap的key为字段名。
这里的#{id}就是使用PrepareStatement的方式传入参数,即像这样:
String selectPerson = “SELECT * FROM PERSON WHERE ID=?”; PreparedStatement ps = conn.prepareStatement(selectPerson); ps.setInt(1,id);
select元素支持的属性:
id | 在命名空间中唯一的标识符,可以被用来引用这条语句。 |
parameterType | 将会传入这条语句的参数类的完全限定名或别名。 |
parameterMap | 这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数 映射和 parameterType 属性。 |
resultType | 从这条语句中返回的期望类型的类的完全限定名或别名。注意集 合情形,那应该是集合可以包含的类型,而不能是集合本身。使 用 resultType 或 resultMap,但不能同时使用。 |
resultMap | 命名引用外部的 resultMap。 返回 map 是 MyBatis 最具力量的特性, 对其有一个很好的理解的话, 许多复杂映射的情形就能被解决了。 使用 resultMap 或 resultType,但不能同时使用。 |
flushCache | 将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被 清空。默认值:false。 |
useCache | 将其设置为 true, 将会导致本条语句的结果被缓存。 默认值: true。 |
timeout | 这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的 最大等待值。默认不设置(驱动自行处理) |
fetchSize | 这是暗示驱动程序每次批量返回的结果行数。默认不设置(驱动 自行处理)。 |
statementType | STA TEMENT,PREPARED 或 CALLABLE 的一种。 这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。 默认值:PREPARED。 |
resultSetType | FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE 中的一种。默认是不设置(驱动自行处理)。 |
databaseId | In case there is a configured databaseIdProvider, MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one. If case the same statement if found with and without the databaseId the latter will be discarded. |
3.Insert、update、delete 元素
数据修改语句insert、update 和delete 的配置使用都非常相似,只是insert元素多了两个属性配置(useGeneratedKey,keyProperty,keyColumn)。
下面是insert元素的示例(update,delete元素也差不多):
<!--自动生成主键--> <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>
id | 在命名空间中唯一的标识符,可以被用来引用这条语句。 |
parameterType | 将会传入这条语句的参数类的完全限定名或别名。 |
parameterMap | 这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数 映射和 parameterType 属性。 |
flushCache | 将其设置为 true,不论语句什么时候被带哦用,都会导致缓存被清 空。默认值:false。 |
timeout | 这个设置驱动程序等待数据库返回请求结果, 并抛出异常时间的最 大等待值。默认不设置(驱动自行处理)。 |
statementType | STA TEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBatis 使用选择使用 Statement,PreparedStatement 或 CallableStatement。 默认值:PREPARED。 |
useGeneratedKeys | ( 仅 对 insert 有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的 getGeneratedKeys 方法来取出由数据(比如:像 MySQL 和 SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。 默认值:false。 |
keyProperty | (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。 默认: 不设置。 |
keyColumn | (仅对 insert 有用) 标记一个属性, MyBatis 会通过 getGeneratedKeys 或者通过 insert 语句的 selectKey 子元素设置它的值。 默认: 不设置。 |
3.parameter(参数)
我们可以向#{id},#{name}这样形式的语句转入参数,但我们还可以更加具体地声明参数的java类型,jdbc类型,以及使用哪个TypeHandler,对于jdbc类型是NUMERIC的参数,还可以指定他的小数位数,如:
#{age,javaType=int,jdbcType=NUMERIC,numbericScale=2,typeHandler=MyTypeHandler}
除了这些外,还可以声明参数是IN,OUT或INOUT模式
#{age,javaType=int,jdbcType=NUMERIC,numbericScale=2,typeHandler=MyTypeHandler,mode=INOUT}
最好,还可以使用${str}形式,把str参数的直接字符串值替换到${str}符号中,这种方式尽量少用,因为这会造成SQL注入的问题。
4.sql元素
这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。比如:
<sql id="userColumns"> id,username,password </sql>
<select id="selectUsers" parameterType="int" resultType="hashmap"> select <include refid="userColumns"/> from some_table where id = #{id} </select>
5.resultMap元素
resultMap的概览视图:
-
constructor - 类在实例化时,用来注入结果到构造方法中
- idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能
- arg - 注入到构造方法的一个普通结果
- id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能
- result – 注入到字段或 JavaBean 属性的普通结果
-
association – 一个复杂的类型关联;许多结果将包成这种类型
- 嵌入结果映射 – 结果映射自身的关联,或者参考一个
-
collection – 复杂类型的集
- 嵌入结果映射 – 结果映射自身的集,或者参考一个
-
discriminator – 使用结果值来决定使用哪个结果映射
-
case – 基于某些值的结果映射
- 嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相 同的元素,或者它可以参照一个外部的结果映射。
-
case – 基于某些值的结果映射
5.1 id、result及constructor
这三个元素使用起来都非常相似的,它们只能用于简单类型数据。
<id property="id" column="post_id"/> <result property="subject" column="post_subject"/>
这些元素支持的属性:
property | 映射到列结果的字段或属性。如果匹配的是存在的,和给定名称相同 的 JavaBeans 的属性,那么就会使用。否则 MyBatis 将会寻找给定名称 property 的字段。这两种情形你可以使用通常点式的复杂属性导航。比如,你 可以这样映射一些东西: “username” ,或者映射到一些复杂的东西: “address.street.number” 。 |
column | 从数据库中得到的列名,或者是列名的重命名标签。这也是通常和会 传递给 resultSet.getString(columnName)方法参数中相同的字符串。 |
javaType | 一个 Java 类的完全限定名,或一个类型别名(参加上面内建类型别名 的列表) 。如果你映射到一个 JavaBean,MyBatis 通常可以断定类型。 然而,如果你映射到的是 HashMap,那么你应该明确地指定 javaType 来保证所需的行为。 |
jdbcType | 在这个表格之后的所支持的 JDBC 类型列表中的类型。JDBC 类型是仅 仅需要对插入,更新和删除操作可能为空的列进行处理。这是 JDBC jdbcType 的需要,而不是 MyBatis 的。如果你直接使用 JDBC 编程,你需要指定 这个类型-但仅仅对可能为空的值。 |
typeHandler | 我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默 认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理 器的实现,或者是类型别名。 |
这些元素的映射支持的JDBC类型有:
BIT | FLOAT | CHAR | TIMESTAMP | OTHER | UNDEFINED |
TINYINT | REAL | VARCHAR | BINARY | BLOG | NVARCHAR |
SMALLINT | DOUBLE | LONGVARCHAR | VARBINARY | CLOB | >NCHAR |
INTEGER | NUMERIC | DATE | LONGVARBINARY | BOOLEAN | NCLOB |
BIGINT | DECIMAL | TIME | NULL | CURSOR | ARRAY |
5.2 association关联查询
association关联查询用于处理一对一关系,它可以使用两种方式查询这个关联对象:嵌套查询方式和嵌套结果方式。
5.2.1 嵌套查询方式
示例:
<resultMap id="blogResult" type="Blog"> <association property="author" column="blog_author_id" javaType="Author" select="selectAuthor"/> </resultMap> <select id="selectBlog" parameterType="int" resultMap="blogResult"> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id="selectAuthor" parameterType="int" resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id} </select>
Notice:嵌套查询的属性column,表示该关联对象是通过哪个字段查询得到的,该字段的值会被当作参数传递到嵌套查询selectAuthor中。
Notice:嵌套查询方式可以很容易地实现递归查询,而嵌套结果方式是不能实现递归查询的。
5.2.2嵌套结果方式
示例:
<select id="selectBlog" parameterType="int" resultMap="blogResult"> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio from Blog B left outer join Author A on B.author_id = A.id where B.id = #{id} </select>
<resultMap id="blogResult" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <association property="author" column="blog_author_id" javaType="Author" resultMap="authorResult"/> </resultMap> <resultMap id="authorResult" type="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> </resultMap>
Notice:嵌套结果方式一般对应的是一个多表连接查询,把查询出来的结果集映射到关联对象上。
在多表连接查询中,一般都会重用resultMap,但连接查询的结果集的列名要求是不一样的,association元素提供了一个columnPrefix属性来解决这个问题。如一篇文章有两个作者共同完成,一个主编,一个副编。
<select id="selectBlog" parameterType="int" resultMap="blogResult"> select B.id as blog_id, B.title as blog_title, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio, CA.id as co_author_id, CA.username as co_author_username, CA.password as co_author_password, CA.email as co_author_email, CA.bio as co_author_bio from Blog B left outer join Author A on B.author_id = A.id left outer join Author CA on B.co_author_id = CA.id where B.id = #{id} </select>
<resultMap id="authorResult" type="Author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> </resultMap>
<resultMap id="blogResult" type="Blog"> <id property="id" column="blog_id" /> <result property="title" column="blog_title"/> <association property="author" resultMap="authorResult" /> <association property="coAuthor" resultMap="authorResult" columnPrefix="co_" /> </resultMap>
5.3 collection集合映射
集合映射是用于解决一对多的关系模型映射问题。它与关联映射非常相似,这里主要说明集合映射的一些不同点。
Notice:collection的column属性表示查询该集合所需要的列或字段,它会被作为参数传递到嵌套查询中。
这里展示一个例子,如何使用嵌套查询来实现递归查询:
5.3.1典型的树型表结构
上图是Category表的结构图,其中parent_id是外键,它引用同一个表的id字段。这其实就是经典的在同一个表中实现树型结构。
5.3.2 Category表的数据
5.3.3 配置mybatis的映射文件,实现递归查询
<resultMap id="categoryResultMap" type="Category"> <id column="id" property="id"></id> <result column="name" property="name"></result> <association property="parent" column="parent_id" select="selectCategoryById"></association><!-- 递归查询,只能用select方式 --> <collection property="children" column="id" ofType="Category" javaType="java.util.ArrayList" select="selectCategoryChildren"></collection><!-- 递归查询,只能用select方式 --> </resultMap> <select id="selectAllCategory" resultMap="categoryResultMap"> select * from category; </select> <select id="selectCategoryById" resultMap="categoryResultMap" parameterType="int"> select * from category where id=#{parent_id} </select> <select id="selectCategoryChildren" resultMap="categoryResultMap" parameterType="int"> select * from category where parent_id=#{id} </select>
相关推荐
基于mybatis的慢SQL小插件,原理是mybatis拦截器。只需要在springboot的配置文件做简单的配置,mybatis拦截器将SQL中所有参数自动做了填充。拦截器监控慢SQL并将完整的可执行的SQL语句打印在日志文件中,复制该SQL...
MyBatis is a first class persistence framework with support for custom SQL, stored procedures and advanced mappings. MyBatis eliminates almost all of the JDBC code and manual setting of parameters and...
自己用
mybatis知识点总结,清晰明了,一看就会,有需要的可以下载。
线上笔记存档
mybatis111111111
一般使用mybatis的环境,大多都是别人已经配置好的。直接用就好了,如何自己搭建呢?其实很简单。看官方的文档就可以解决了。主要为了学习mybatis最基础的配置。我文章中的方法不基于spring,一般很少会在真实项目中...
mybatis的demo程序, 基于dubbo的mybatis的演示程序。提供基本的pox配置
mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip mybatis-plus最新代码生成器项目源码 :mybatis-plus-generator.zip ...
MyBatis简单入门的一些介绍内容
Mybatis 处理 CLOB、BLOB 类型数据
针对父子级数据目录查询, 以前都是逐级的去根据父级id查询子集目录, 查出后最后再在代码中拼成树形结构, 相当复杂,我们可以利用 mybatis 提供的 collection 标签自动组织树形结构
MybatisX 是一款全免费且强大的 IDEA 插件,支持跳转,自动补全生成 SQL,代码生成。
MyBatis3.2.3帮助文档(中文版).chm
springboot+mybatis