mybatis学习指南--xml文件方式篇

mybatis学习指南---xml文件篇

以下内容是由我总结mybatis官方文档和实践中的一些经验,总共分为四篇:xml文件配置篇,java方式配置篇,缓存篇,其他配置篇,第一次这样正式的写一篇文章,致敬我的学长--兵哥,最感谢的人之一,向你学习。

因为是第一篇,所以先来介绍下mybatis,他能提供什么功能,先看一下官方文档的描述:

MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以对配置和原生Map使用简单的 XML 或注解,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

通过上面的叙述可以看出她是一款对jdbc操作封装的框架,众所周知她是优秀的ORM实现,与hibernate一起撑起java持久层操作的框架,从个人使用的经验来说,mybatis使用起来没有hibernate舒服、方便,但hibernate入门简单,深入却比较难,她是一款沉重的框架,从各方面简化程序的开发,但mybatis也有一些区别与hibernate的优点,比如我参与这次开发中,因为后台的数据库压力比较大,使用了读写分离(总结出来了也会发表一篇博客的),经过技术领导的决定,最终决定使用mybatis(原来使用的jpa,然后全部改成mybatis。。心情有一些沉重,真的体会到了什么是产品,一遍一遍的提纯,才是真正的产品)。废话不多说了,先上mybatis和hibernate的优缺点比较,然后带大家一起开始进入mybatis的大门吧。

优缺点比较转自这位大神的文章:http://blog.csdn.net/a63297066/article/details/51454726,比我想的更全一些,从各个方面来比较她们的优缺点。

1、开发上手难度

hibernate的真正掌握(封装的功能和特性非常多)要比Mybatis来得难。(这个本人深有体会)

在真正产品级应用上要用Hibernate,不仅对开发人员的要求高,hibernate往往还不适合(多表关联查询等,这个也深有体会)。


2、系统调优调优方案对比

Hibernate:

* 制定合理的缓存策略;

* 尽量使用延迟加载特性;
* 采用合理的Session管理机制;
* 使用批量抓取,设定合理的批处理参数(batch_size);

* 进行合理的O/R映射设计


Mybatis:

* MyBatis在Session方面和Hibernate的Session生命周期是一致的,同样需要合理的Session管理机制。MyBatis同样具有二级缓存机制。 

* MyBatis可以进行详细的SQL优化设计。


3、SQL优化方面

Hibernate的查询会将表中的所有字段查询出来,这一点会有性能消耗。

Hibernate也可以自己写SQL来指定需要查询的字段,但这样就破坏了Hibernate开发的简洁性。


Mybatis的SQL是手动编写的,所以可以按需求指定查询的字段。


总的来说,Hibernate使用的是封装好,通用的SQL来应付所有场景,而Mybatis是针对响应的场景设计的SQL。Mybatis的SQL会更灵活、可控性更好、更优化。


4、移植性

Hibernate与具体数据库的关联只需在XML文件中配置即可,所有的HQL语句与具体使用的数据库无关,移植性很好。

MyBatis项目中所有的SQL语句都是依赖所用的数据库的,所以不同数据库类型的支持不好。


5、JDBC

Hibernate是在JDBC上进行了一次封装。

Mybatis是基于原生的JDBC的。Mybatis有运行速度上的优势。


6、功能、特性丰富程度

Hibernate提供了诸多功能和特性。要全掌握很难。

Mybatis 自身功能很有限,但Mybatis支持plugin,可以使用开源的plugin来扩展功能。


7、动态SQL

Mybatis mapper xml 支持动态SQL

Hibernate不支持


实际项目关于Hibernate和Mybatis的选型:

1、数据量:有以下情况最好选用Mybatis

如果有超过千万级别的表

如果有单次业务大批量数据提交的需求(百万条及以上的),这个尤其不建议用Hibernate

如果有单次业务大批量读取需求(百万条及以上的)(注,hibernate多表查询比较费劲,用不好很容易造成性能问题)

2、表关联复杂度

如果主要业务表的关联表超过20个(大概值),不建议使用hibernate

3、人员

如果开发成员多数不是多年使用hibernate的情况,建议使用mybatis

4、数据库对于项目的重要程度

如果项目要求对于数据库可控性好,可深度调优,用mybatis



下面进入mybatis的大门吧:

1.引入jar,我使用的spring-boot,开发很方便:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath />
</parent>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- mybatis setting -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mybatis setting -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mysql -->
<!-- ali druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<!-- ali druid -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


2.配置:

application.yml配置文件中写上:

mybatis:
  mapper-locations: classpath:/mybatis/*Mapper.xml
  type-aliases-package: com.towatt.entity
指定entity的位置,上面 mapper-locations:指定的是mapper.xml文件存放的位置,

3.开始写sql吧。

在入口类中加一个注解 @EnableTransactionManagement 开启spring的事务,会自动提交事务

下面介绍怎么写**Mapper.xml文件:


注解方式和xml方式,下面先介绍xml方式。
命名空间(Namespaces):在之前版本的 MyBatis 中是可选的,容易引起混淆因此是没有益处的。现在的命名空间则是必须的,目的是希望能比只是简单的使用更长的完全限定名来区分语句更进一步,
 下面使用时,命名空间就是dao层接口的完全限定名。


mybatis已经为许多常见的 Java 类型内建了相应的类型别名。它们都是大小写不敏感的,需要注意的是由基本类型名称重复导致的特殊处理


Mapper XML 文件:{


SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):
cache – 给定命名空间的缓存配置。 
cache-ref – 其他命名空间缓存配置的引用。 
resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。 
parameterMap – 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。 
sql – 可被其他语句引用的可重用语句块。 
insert – 映射插入语句 
update – 映射更新语句 
delete – 映射删除语句 
select – 映射查询语句 

1.
select
<select id="selectPerson" parameterType="int" resultType="hashmap">
 SELECT * FROM PERSON WHERE ID = #{id}
</select>
这个语句被称作 selectPerson,接受一个 int(或 Integer)类型的参数,并返回一个 HashMap 类型的对象,其中的键是列名,值便是结果行中的对应值。 
注意参数符号:#{id}
这就告诉 MyBatis 创建一个预处理语句参数,通过 JDBC,这样的一个参数在 SQL 中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样: 
// Similar JDBC code, NOT MyBatis…
String selectPerson = "SELECT * FROM PERSON WHERE ID=?";
PreparedStatement ps = conn.prepareStatement(selectPerson);
ps.setInt(1,id);

select 元素有很多属性允许你配置,来决定每条语句的作用细节。 
<select
 id="selectPerson"
 parameterType="int"
 parameterMap="deprecated"
 resultType="hashmap"
 resultMap="personResultMap"
 flushCache="false"
 useCache="true"
 timeout="10000"
 fetchSize="256"
 statementType="PREPARED"
 resultSetType="FORWARD_ONLY">

属性 描述 
id 在命名空间中唯一的标识符,可以被用来引用这条语句。  
parameterType 将会传入这条语句的参数类的完全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数,默认值为 unset。  
parameterMap  这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。  
resultType 从这条语句中返回的期望类型的类的完全限定名或别名。注意如果是集合情形,那应该是集合可以包含的类型,而不能是集合本身。使用 resultType 或 resultMap,但不能同时使用。  
resultMap 外部 resultMap 的命名引用。结果集的映射是 MyBatis 最强大的特性,对其有一个很好的理解的话,许多复杂映射的情形都能迎刃而解。使用 resultMap 或 resultType,但不能同时使用。  
flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:false。  
useCache 将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true。  
timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。  
fetchSize 这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)。  
statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。  
resultSetType FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认值为 unset (依赖驱动)。  
databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。  
resultOrdered 这个设置仅针对嵌套结果 select 语句适用:如果为 true,就是假设包含了嵌套结果集或是分组了,这样的话当返回一个主结果行的时候,就不会发生有对前面结果集的引用的情况。这就使得在获取嵌套的结果集的时候不至于导致内存不够用。默认值:false。  
resultSets 这个设置仅对多结果集的情况适用,它将列出语句执行后返回的结果集并每个结果集给一个名称,名称是逗号分隔的。  

2.
insert, update 和 delete
数据变更语句 insert,update 和 delete 的实现非常接近: 
<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">
 

属性 描述 
id 命名空间中的唯一标识符,可被用来代表这条语句。 
parameterType 将要传入语句的参数的完全限定类名或别名。这个属性是可选的,因为 MyBatis 可以通过 TypeHandler 推断出具体传入语句的参数,默认值为 unset。  
parameterMap  这是引用外部 parameterMap 的已经被废弃的方法。使用内联参数映射和 parameterType 属性。  
flushCache 将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:true(对应插入、更新和删除语句)。  
timeout 这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。  
statementType STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。  
useGeneratedKeys (仅对 insert 和 update 有用)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段),默认值:false。  
keyProperty (仅对 insert 和 update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值,默认:unset。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。  
keyColumn (仅对 insert 和 update 有用)通过生成的键值设置表中的列名,这个设置仅在某些数据库(像 PostgreSQL)是必须的,当主键列不是表中的第一列的时候需要设置。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。  
databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略。  

下面就是 insert,update 和 delete 语句的示例:
<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>

自动生成主键:如果你的数据库支持自动生成主键的字段(比如 MySQL 和 SQL Server),那么你可以设置 useGeneratedKeys=”true”,
然后再把 keyProperty 设置到目标属性上就OK了。例如,如果上面的 Author 表已经对 id 使用了自动生成的列类型,那么语句可以修改为:
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
 insert into Author (username,password,email,bio)
 values (#{username},#{password},#{email},#{bio})
</insert>


selectKey 
对于不支持自动生成类型的数据库或可能不支持自动生成主键 JDBC 驱动来说,MyBatis 有另外一种方法来生成主键。 
这里有一个简单(甚至很傻)的示例,它可以生成一个随机 ID(你最好不要这么做,但这里展示了 MyBatis 处理问题的灵活性及其所关心的广度): 
<insert id="insertAuthor">
 <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 会被设置,然后插入语句会被调用。这给你了一个和数据库中来处理自动生成的主键类似
的行为,避免了使 Java 代码变得复杂。 


3.sql
这个元素可以被用来定义可重用的 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>


这个没看懂上面的一些元素,先不开展。

4.Parameters
<select id="selectUsers" resultType="User">
 select id, username, password
 from users
 where id = #{id}
</select>
上面的这个示例说明了一个非常简单的命名参数映射。参数类型被设置为 int,这样这个参数就可以被设置成任何内容。原生的类型或简单数据类型
(比如整型和字符串)因为没有相关属性,它会完全用参数值来替代。然而,如果传入一个复杂的对象,行为就会有一点不同了。比如: 
<insert id="insertUser" parameterType="User">
 insert into users (id, username, password)
 values (#{id}, #{username}, #{password})
</insert>
如果 User 类型的参数对象传递到了语句中,id、username 和 password 属性将会被查找,然后将它们的值传入预处理语句的参数中。 
这点对于向语句中传参是比较好的而且又简单,不过参数映射的功能远不止于此。 
首先,像 MyBatis 的其他部分一样,参数也可以指定一个特殊的数据类型 #{property,javaType=int,jdbcType=NUMERIC}
像 MyBatis 的剩余部分一样,javaType 通常可以从参数对象中来去确定,前提是只要对象不是一个 HashMap。那么 javaType 应该被确定来保证使用正确类型处理器。
尽管看起来配置变得越来越繁琐,但实际上是很少去设置它们。 
对于数值类型,还有一个小数保留位数的设置,来确定小数点后保留的位数。 
#{height,javaType=double,jdbcType=NUMERIC,numericScale=2}
尽管所有这些强大的选项很多时候你只简单指定属性名,其他的事情 MyBatis 会自己去推断,最多你需要为可能为空的列名指定 jdbcType。
一般只需要#{}参数即可,paramType什么的参数mybatis都会自动配置的。

5.Result Maps
结果集映射:
使用resultType,返回结果集将根据javaBean的属性名称去匹配列名
<select id="selectUsers" resultType="com.someapp.model.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>

高级结果映射:
<!-- Very Complex Statement -->
<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
 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,
  A.favourite_section as author_favourite_section,
  P.id as post_id,
  P.blog_id as post_blog_id,
  P.author_id as post_author_id,
  P.created_on as post_created_on,
  P.section as post_section,
  P.subject as post_subject,
  P.draft as draft,
  P.body as post_body,
  C.id as comment_id,
  C.post_id as comment_post_id,
  C.name as comment_name,
  C.comment as comment_text,
  T.id as tag_id,
  T.name as tag_name
 from Blog B
  left outer join Author A on B.author_id = A.id
  left outer join Post P on B.id = P.blog_id
  left outer join Comment C on P.id = C.post_id
  left outer join Post_Tag PT on PT.post_id = P.id
  left outer join Tag T on PT.tag_id = T.id
 where B.id = #{id}
</select>

这是一个复杂映射:
<!-- Very Complex Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
 <constructor>
<idArg column="blog_id" javaType="int"/>
 </constructor>
 <result property="title" column="blog_title"/>
 <association property="author" javaType="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"/>
<result property="favouriteSection" column="author_favourite_section"/>
 </association>
 <collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<association property="author" javaType="Author"/>
<collection property="comments" ofType="Comment">
 <id property="id" column="comment_id"/>
</collection>
<collection property="tags" ofType="Tag" >
 <id property="id" column="tag_id"/>
</collection>
<discriminator javaType="int" column="draft">
 <case value="1" resultType="DraftPost"/>
</discriminator>
 </collection>
</resultMap>

resultMap:

constructor - 类在实例化时,用来注入结果到构造方法中 
idArg - ID 参数;标记结果作为 ID 可以帮助提高整体效能 
arg - 注入到构造方法的一个普通结果 
id – 一个 ID 结果;标记结果作为 ID 可以帮助提高整体效能 
result – 注入到字段或 JavaBean 属性的普通结果 
association – 一个复杂的类型关联;许多结果将包成这种类型 
嵌入结果映射 – 结果映射自身的关联,或者参考一个 
collection – 复杂类型的集 
嵌入结果映射 – 结果映射自身的集,或者参考一个 
discriminator – 使用结果值来决定使用哪个结果映射 
case – 基于某些值的结果映射 
嵌入结果映射 – 这种情形结果也映射它本身,因此可以包含很多相同的元素,或者它可以参照一个外部的结果映射。

result:

属性 描述 
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 我们在前面讨论过默认的类型处理器。使用这个属性,你可以覆盖默认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现,或者是类型别名。  


构造方法:
<constructor>
  <idArg column="id" javaType="int"/>
  <arg column="username" javaType="String"/>
</constructor>

对于大多数数据传输对象(Data Transfer Object,DTO)类型,属性可以起作用,而且像你绝大多数的领域模型, 指令也许是你想使用一成不变的类的地方。
通常包含引用或查询数据的表很少或基本不变的话对一成不变的类来说是合适的。构造方法注入允许你在初始化时为类设置属性的值,而不用暴露出公有方法。
MyBatis 也支持私有属性和私有 JavaBeans 属性来达到这个目的,但是一些人更青睐构造方法注入。构造方法元素支持这个。 

为了向这个构造方法中注入结果,MyBatis 需要通过它的参数的类型来标识构造方法。 Java 没有自查(反射)参数名的方法。所以当创建一个构造方法元素时,
保证参数是按顺序排列的,而且数据类型也是确定的。 
<constructor>
  <idArg column="id" javaType="int"/>
  <arg column="username" javaType="String"/>
</constructor>

关联:
<association property="author" column="blog_author_id" javaType="Author">
 <id property="id" column="author_id"/>
 <result property="username" column="author_username"/>
</association>
关联元素处理“有一个”类型的关系。比如,在我们的示例中,一个博客有一个用户。关联映射就工作于这种结果之上。你指定了目标属性,来获取值的列,属性的
java 类型(很多情况下 MyBatis 可以自己算出来) ,如果需要的话还有 jdbc 类型,如果你想覆盖或获取的结果值还需要类型控制器。 


关联中不同的是你需要告诉 MyBatis 如何加载关联。MyBatis 在这方面会有两种不同的方式: 
嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型。 
嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集。首先,然让我们来查看这个元素的属性。所有的你都会看到,它和普通的只由 select 和 
resultMap 属性的结果映射不同。 

关联的嵌套查询:

属性    描述
column 来自数据库的类名,或重命名的列标签。这和通常传递给 resultSet.getString(columnName)方法的字符串是相同的。 column 注 意 : 要 处 理 复 合 主 键 , 你 可 以 指 定 多 个 列 名 通 过 column= ” {prop1=col1,prop2=col2} ” 这种语法来传递给嵌套查询语 句。这会引起 prop1 和 prop2 以参数对象形式来设置给目标嵌套查询语句。  
select 另外一个映射语句的 ID,可以加载这个属性映射需要的复杂类型。获取的 在列属性中指定的列的值将被传递给目标 select 语句作为参数。表格后面 有一个详细的示例。 select 注 意 : 要 处 理 复 合 主 键 , 你 可 以 指 定 多 个 列 名 通 过 column= ” {prop1=col1,prop2=col2} ” 这种语法来传递给嵌套查询语 句。这会引起 prop1 和 prop2 以参数对象形式来设置给目标嵌套查询语句。 
示例: 


<resultMap id="blogResult" type="Blog">
 <association property="author" column="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>


我们有两个查询语句:一个来加载博客,另外一个来加载作者,而且博客的结果映射描 述了“selectAuthor”语句应该被用来加载它的 author 属性。 


其他所有的属性将会被自动加载,假设它们的列和属性名相匹配。 


这种方式很简单, 但是对于大型数据集合和列表将不会表现很好。 问题就是我们熟知的 “N+1 查询问题”。概括地讲,N+1 查询问题可以是这样引起的: 
?你执行了一个单独的 SQL 语句来获取结果列表(就是“+1”)。
?对返回的每条记录,你执行了一个查询语句来为每个加载细节(就是“N”)。 


这个问题会导致成百上千的 SQL 语句被执行。这通常不是期望的。 


MyBatis 能延迟加载这样的查询就是一个好处,因此你可以分散这些语句同时运行的消 耗。然而,如果你加载一个列表,之后迅速迭代来访问嵌套的数据,你会调用所有的延迟加 载,这样的行为可能是很糟糕的。 


所以还有另外一种方法。 




关联的嵌套结果: 
在上面你已经看到了一个非常复杂的嵌套关联的示例。 下面这个是一个非常简单的示例 来说明它如何工作。代替了执行一个分离的语句,
我们联合博客表和作者表在一起,就像: 
<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>

现在,上面的示例用了外部的结果映射元素来映射关联。这使得 Author 结果映射可以 重用。然而,如果你不需要重用它的话,或者你仅仅
引用你所有的结果映射合到一个单独描 述的结果映射中。你可以嵌套结果映射。这里给出使用这种方式的相同示例: 
<resultMap id="blogResult" type="Blog">
 <id property="id" column="blog_id" />
 <result property="title" column="blog_title"/>
 <association property="author" javaType="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"/>
 </association>
</resultMap>

如果blog有一个co-author怎么办? select语句将看起来这个样子: 
<select id="selectBlog" 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>
再次调用Author的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>
因为结果中的列名与resultMap中的列名不同。 你需要指定columnPrefix去重用映射co-author结果的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>

上面你已经看到了如何处理“有一个”类型关联。但是“有很多个”是怎样的?下面这个部分就是来讨论这个主题的。 


集合:
<collection property="posts" ofType="domain.blog.Post">
 <id property="id" column="post_id"/>
 <result property="subject" column="post_subject"/>
 <result property="body" column="post_body"/>
</collection>
集合元素的作用几乎和关联是相同的。实际上,它们也很相似,文档的异同是多余的。所以我们更多关注于它们的不同。 


我们来继续上面的示例,一个博客只有一个作者。但是博客有很多文章。在博客类中, 这可以由下面这样的写法来表示:
private List<Post> posts;
要映射嵌套结果集合到 List 中,我们使用集合元素。就像关联元素一样,我们可以从连接中使用嵌套查询,或者嵌套结果。 

集合的嵌套查询:
首先,让我们看看使用嵌套查询来为博客加载文章。 
<resultMap id="blogResult" type="Blog">
 <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>


<select id="selectBlog" resultMap="blogResult">
 SELECT * FROM BLOG WHERE ID = #{id}
</select>


<select id="selectPostsForBlog" resultType="Blog">
 SELECT * FROM POST WHERE BLOG_ID = #{id}
</select>
这里你应该注意很多东西,但大部分代码和上面的关联元素是非常相似的。首先,你应该注意我们使用的是集合元素。
然后要注意那个新的“ofType”属性。这个属性用来区分 JavaBean(或字段)属性类型和集合包含的类型来说是很重要的。所以你可以读出下面
这个映射: <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
读作: “在 Post 类型的 ArrayList 中的 posts 的集合。” javaType 属性是不需要的,因为 MyBatis 在很多情况下会为你算出来。
所以你可以缩短写法: <collection property="posts" column="id" ofType="Post" select="selectPostsForBlog"/>

集合的嵌套结果:
至此,你可以猜测集合的嵌套结果是如何来工作的,因为它和关联完全相同,除了它应用了一个“ofType”属性
<select id="selectBlog" resultMap="blogResult">
 select
 B.id as blog_id,
 B.title as blog_title,
 B.author_id as blog_author_id,
 P.id as post_id,
 P.subject as post_subject,
 P.body as post_body,
 from Blog B
 left outer join Post P on B.id = P.blog_id
 where B.id = #{id}
</select>
我们又一次联合了博客表和文章表,而且关注于保证特性,结果列标签的简单映射。现在用文章映射集合映射博客,可以简单写为:
<resultMap id="blogResult" type="Blog">
 <id property="id" column="blog_id" />
 <result property="title" column="blog_title"/>
 <collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
 </collection>
</resultMap>

同样,要记得 id 元素的重要性,如果你不记得了,请阅读上面的关联部分。 


同样, 如果你引用更长的形式允许你的结果映射的更多重用, 你可以使用下面这个替代的映射: 


<resultMap id="blogResult" type="Blog">
 <id property="id" column="blog_id" />
 <result property="title" column="blog_title"/>
 <collection property="posts" ofType="Post" resultMap="blogPostResult" columnPrefix="post_"/>
</resultMap>


<resultMap id="blogPostResult" type="Post">
 <id property="id" column="id"/>
 <result property="subject" column="subject"/>
 <result property="body" column="body"/>
</resultMap>

一对一关联映射:学生和学生证的对应

card映射:
<mapper namespace="cardNamespace">
<resultMap type="cn.itcast.javaee.mybatis.one2one.Card" id="cardMap">
<id property="id" column="id" />
<result property="num" column="num" />
</resultMap>
</mapper>
Student映射:
<mapper namespace="studentNamespace">
<resultMap type="cn.itcast.javaee.mybatis.one2one.Student" id="studentMap">
<id property="id" column="id" />
<result property="name" column="name"/>
<association property="card" resultMap="cardNamespace.cardMap"/>
<!-- 这里使用了resultMap引用card的resultMap。 -->
</resultMap>

<select id="findById" parameterType="int" resultMap="studentMap">
select s.id,s.name,c.id,c.num
from students s inner join cards c
on s.cid = c.id 
and s.id = #{id}
</select>
</mapper>






一对多关联映射:学生和班级的对应

班级(一):
<resultMap type="com.towatt.entity.Grade" id="gradeMap">
<id property="id" column="gid" />
<result property="name" column="gname" />
<collection property="studentList" resultMap="com.towatt.dao.StudentDao.studentMap"/> 


<!-- <collection property="studentList" ofType="com.towatt.entity.Student">
<id property="id" column="sid" />
<result property="name" column="sname" />
<association property="grade" resultMap="basicGradeMap" />
</collection>   

ofType  指定集合的类型   -->


</resultMap>





学生(多):
<mapper namespace="com.towatt.dao.StudentDao">
<resultMap type="com.towatt.entity.Student"
id="studentMap">
<id property="id" column="sid" />
<result property="name" column="sname" />
<association property="grade" resultMap="com.towatt.dao.GradeDao.basicGradeMap"/>
</resultMap>

<select id="findAllByName" parameterType="string" resultMap="studentMap">
select s.sid,s.sname,g.gid,g.gname
from grades g,students s
where g.gid
= s.sgid
and g.gname = #{name}
</select>
</mapper>  

建议双向都定义最基础的resultMap(不包含关联关系的映射)和复杂的resultMap(包含关联关系的实体),如果双方都使用复杂的resultMap来处理映射结果,
会造成重复引用,导致栈溢出。
其中在指定关联时可以用下列元素:{
association
collection









多对多关联映射:
学生和所选课程的关系就是典型的多对多
学生映射:
<mapper namespace="com.towatt.dao.StudentDao">
<resultMap type="com.towatt.entity.Student" id="studentMap">
<id property="id" column="sid" />
<result property="name" column="sname" />
</resultMap>

<resultMap type="com.towatt.entity.Student" id="studentBasicMap">
<id property="id" column="sid" />
<result property="name" column="sname"/>
<collection property="courseList" resultMap="com.towatt.dao.CourseDao.courseMap"></collection>
</resultMap>


<select id="findStudentByName" parameterType="string" resultMap="studentBasicMap">
select s.sid,s.sname,c.cid,c.cname
from students s,middles m,courses c
where s.sid =
m.sid
and m.cid = c.cid
and c.cname = #{name}
</select>
</mapper>

课程映射:
<mapper namespace="com.towatt.dao.CourseDao">
<resultMap type="com.towatt.entity.Course"
id="courseMap">
<id property="id" column="cid" />
<result property="name" column="cname" />
</resultMap>

<resultMap type="com.towatt.entity.Course"
id="courseBasicMap">
<id property="id" column="cid" />
<result property="name" column="cname" />
<collection property="studentList" resultMap="com.towatt.dao.StudentDao.studentMap"></collection>
</resultMap>



<select id="findCourseByName" resultMap="courseBasicMap">
select s.sid,s.sname,c.cid,c.cname
from students s,middles m,courses c
where s.sid =
m.sid
and m.cid = c.cid
and s.sname = #{name}
</select>
</mapper>

多对多实际就是双向一对多,在描述映射关系时使用collection元素来指定多的一方的属性。






鉴别器:自己理解:鉴别器就是通过我自己存的值来决定哪些属性加载,哪些属性不加载,或者动态决定使用哪个结果映射加载结果。
<discriminator javaType="int" column="draft">
 <case value="1" resultType="DraftPost"/>
</discriminator>

有时一个单独的数据库查询也许返回很多不同 (但是希望有些关联) 数据类型的结果集。鉴别器元素就是被设计来处理这个情况的, 还有包括类
的继承层次结构。鉴别器非常容易理解,因为它的表现很像 Java 语言中的 switch 语句。 


定义鉴别器指定了 column 和 javaType 属性。列是 MyBatis 查找比较值的地方。 JavaType 是需要被用来保证等价测试的合适类型
(尽管字符串在很多情形下都会有用)。比如: 


<resultMap id="vehicleResult" type="Vehicle">
 <id property="id" column="id" />
 <result property="vin" column="vin"/>
 <result property="year" column="year"/>
 <result property="make" column="make"/>
 <result property="model" column="model"/>
 <result property="color" column="color"/>
 <discriminator javaType="int" column="vehicle_type">
<case value="1" resultMap="carResult"/>
<case value="2" resultMap="truckResult"/>
<case value="3" resultMap="vanResult"/>
<case value="4" resultMap="suvResult"/>
 </discriminator>
</resultMap>

在这个示例中, MyBatis 会从结果集中得到每条记录, 然后比较它的 vehicle 类型的值。如果它匹配任何一个鉴别器的实例,那么就使用这个实例
指定的结果映射。换句话说,这样做完全是剩余的结果映射被忽略(除非它被扩展,这在第二个示例中讨论) 。如果没有任何一个实例相匹配,
那么 MyBatis 仅仅使用鉴别器块外定义的结果映射。所以,如果 carResult 按如下声明: 
<resultMap id="carResult" type="Car">
 <result property="doorCount" column="door_count" />
</resultMap>

那么只有 doorCount 属性会被加载。这步完成后完整地允许鉴别器实例的独立组,尽管和父结果映射可能没有什么关系。这种情况下,我们当然
知道 cars 和 vehicles 之间有关系, 如 Car 是一个 Vehicle 实例。因此,我们想要剩余的属性也被加载。我们设置的结果映射的简单改变如下。 
<resultMap id="carResult" type="Car" extends="vehicleResult">
 <result property="doorCount" column="door_count" />
</resultMap>
这个还有点意思,使用继承结果集来加载指定属性,原来collection或association是向下指定加载结果,而extends是向上指定加载结果。

现在 vehicleResult 和 carResult 的属性都会被加载了。尽管曾经有些人会发现这个外部映射定义会多少有一些令人厌烦之处。
因此还有另外一种语法来做简洁的映射风格。比如: 
<resultMap id="vehicleResult" type="Vehicle">
 <id property="id" column="id" />
 <result property="vin" column="vin"/>
 <result property="year" column="year"/>
 <result property="make" column="make"/>
 <result property="model" column="model"/>
 <result property="color" column="color"/>
 <discriminator javaType="int" column="vehicle_type">
<case value="1" resultType="carResult">
 <result property="doorCount" column="door_count" />
</case>
<case value="2" resultType="truckResult">
 <result property="boxSize" column="box_size" />
 <result property="extendedCab" column="extended_cab" />
</case>
<case value="3" resultType="vanResult">
 <result property="powerSlidingDoor" column="power_sliding_door" />
</case>
<case value="4" resultType="suvResult">
 <result property="allWheelDrive" column="all_wheel_drive" />
</case>
 </discriminator>
</resultMap>
要记得 这些都是结果映射, 如果你不指定任何结果, 那么 MyBatis 将会为你自动匹配列和属性。所以这些例子中的大部分是很冗长的,
而其实是不需要的。也就是说,很多数据库是很复杂的,我们不太可能对所有示例都能依靠它。 

自动映射:
正如你在前面一节看到的,在简单的场景下,MyBatis可以替你自动映射查询结果。 如果遇到复杂的场景,你需要构建一个result map。 但是在本节你
将看到,你也可以混合使用这两种策略。 让我们到深一点的层面上看看自动映射是怎样工作的。 
当自动映射查询结果时,MyBatis会获取sql返回的列名并在java类中查找相同名字的属性(忽略大小写)。 这意味着如果Mybatis发现了ID列和id属性,
Mybatis会将ID的值赋给id。 


通常数据库列使用大写单词命名,单词间用下划线分隔;而java属性一般遵循驼峰命名法。 为了在这两种命名方式之间启用自动映射,
需要将 mapUnderscoreToCamelCase设置为true。 


自动映射甚至在特定的result map下也能工作。在这种情况下,对于每一个result map,所有的ResultSet提供的列, 如果没有被手工映射,
则将被自动映射。自动映射处理完毕后手工映射才会被处理。 在接下来的例子中, 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>有三种自动映射等级: 


NONE - 禁用自动映射。仅设置手动映射属性。 
PARTIAL - 将自动映射结果除了那些有内部定义内嵌结果映射的(joins). 
FULL - 自动映射所有。 
默认值是PARTIAL,这是有原因的。当使用FULL时,自动映射会在处理join结果时执行,并且join取得若干相同行的不同实体数据,因此
这可能导致非预期的映射。下面的例子将展示这种风险: 


<select id="selectBlog" resultMap="blogResult">
 select
B.id,
B.title,
A.username,
 from Blog B left outer join Author A on B.author_id = A.id
 where B.id = #{id}
</select><resultMap id="blogResult" type="Blog">
 <association property="author" resultMap="authorResult"/>
</resultMap>


<resultMap id="authorResult" type="Author">
 <result property="username" column="author_username"/>
</resultMap>在结果中Blog和Author均将自动映射。但是注意Author有一个id属性,在ResultSet中有一个列名为id, 所以Author的id
将被填充为Blog的id,这不是你所期待的。所以需要谨慎使用FULL。 


通过添加autoMapping属性可以忽略自动映射等级配置,你可以启用或者禁用自动映射指定的ResultMap。 


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


真实的例子:

需要级联查询时你的配置文件可以这样写,

  <resultMap id="BaseResultMap" type="com.cfyj.mybatis.entity.RechargeRecord" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="member_id" property="memberId" jdbcType="BIGINT" />
    <result column="notify_id" property="notify_id" jdbcType="BIGINT" />
    <result column="order_id" property="orderId" jdbcType="VARCHAR" />
    <result column="transaction_id" property="transactionId" jdbcType="VARCHAR" />
    <result column="money" property="money" jdbcType="DOUBLE" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />     
    <association javaType="com.cfyj.mybatis.entity.DictionaryItem" jdbcType="VARCHAR" property="status"  column="status" select="findByDictionary">  
    </association>
    <association javaType="com.cfyj.mybatis.entity.DictionaryItem" jdbcType="VARCHAR"  property="rechargePosition"  column="recharge_position" select="findByDictionary">
    </association>
    <association javaType="com.cfyj.mybatis.entity.DictionaryItem" jdbcType="VARCHAR" property="channel"  column="channel" select="findByDictionary">
    </association>
    
    <association javaType="com.cfyj.mybatis.entity.Individual" jdbcType="BIGINT"  property="individual"  column="individual_id" select="findIndividual">
    </association>
  </resultMap>
  
     <!-- 根据会员id 分页查询充值记录 -->
  <select id="findByMemberId" resultMap="BaseResultMap">
  select * from mem_individual_recharge_record 
  <where>
  <if test="memberId!=null and memberId>0">
  member_id = #{memberId}
  </if>
  </where>
  </select>
  
  <!-- 查询字典项 -->
  <select id="findByDictionary" resultType="com.cfyj.mybatis.entity.DictionaryItem">
  select d.* from sys_dictionary_item d where d.item_code = #{id};
  </select>
  
  <select id="findIndividual" resultType="com.cfyj.mybatis.entity.Individual">
  select * from mem_individual i where i.id = #{id};
  </select>
  
  <select id="findById" resultMap="BaseResultMap">
  select * from mem_individual_recharge_record where id = #{id}
  </select>
  
   <insert id="saveRechargeRecord" parameterType="com.cfyj.mybatis.entity.RechargeRecord" >
    insert into mem_individual_recharge_record (member_id,notify_id, 
      order_id, recharge_position, channel, 
      money, status, create_time, 
      transaction_id, individual_id)
    values (#{memberId}, #{notify_id}, 
      #{orderId}, #{rechargePosition.itemCode}, #{channel.itemCode}, 
      #{money}, #{status.itemCode}, #{createTime}, 
      #{transactionId}, #{memberId})
  </insert>


注:

1.使用association元素中的select属性时,你最好指定上javaType的类型,但不能指定resultMap,会异常。

2.使用association元素中的select属性时,select语句中的#{id}代表的值是这个association中的column指定的列的值。

<association javaType="com.cfyj.mybatis.entity.Individual" jdbcType="BIGINT"  property="individual"  column="individual_id" select="findIndividual"> 

<select id="findIndividual" resultType="com.cfyj.mybatis.entity.Individual">
  select * from mem_individual i where i.id = #{id};
  </select>

如这个关联查询,#{id}代表的是级联查出的列individual_id的值。但在使用过程中也遇到了一些问题,比如association元素下还有association元素,这时内部的association元素会不起作用。


4.动态sql

MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦。拼接的时候要确保不能忘了必要的空格,还要注意省掉列名列表最后的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。

通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中。

动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多的元素需要来了解。MyBatis 3 大大提升了它们,现在用不到原先一半的元素就可以了。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。

  • if
  • choose (when, otherwise)
  • trim (where, set)
  • foreach

if

动态 SQL 通常要做的事情是有条件地包含 where 子句的一部分。比如:

<select id="findActiveBlogWithTitleLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  WHERE state = ‘ACTIVE’ 
  <if test="title != null">
    AND title like #{title}
  </if>
</select>

这条语句提供了一个可选的文本查找类型的功能。如果没有传入“title”,那么所有处于“ACTIVE”状态的BLOG都会返回;反之若传入了“title”,那么就会把模糊查找“title”内容的BLOG结果返回(就这个例子而言,细心的读者会发现其中的参数值是可以包含一些掩码或通配符的)。


如果想可选地通过“title”和“author”两个条件搜索该怎么办呢?首先,改变语句的名称让它更具实际意义;然后只要加入另一个条件即可。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’ 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</select>

choose, when, otherwise

有些时候,我们不想用到所有的条件语句,而只想从中择其一二。针对这种情况,MyBatis 提供了 choose 元素,它有点像 Java 中的 switch 语句。

还是上面的例子,但是这次变为提供了“title”就按“title”查找,提供了“author”就按“author”查找,若两者都没有提供,就返回所有符合条件的BLOG(实际情况可能是由管理员按一定策略选出BLOG列表,而不是返回大量无意义的随机结果)。

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

注: where 元素知道如果由被包含的标记返回任意内容,就仅仅插入“WHERE” 。而且,如 果以“AND”或“OR”开头的内容,那么就会跳过 WHERE 不插入。
<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
  <where> 
    <if test="state != null">
         AND state = #{state}
    </if> 
    <if test="title != null">
        AND title like #{title}
    </if>
    <if test="author != null and author.name != null">
        AND author_name like #{author.name}
    </if>
  </where>
</select>

foreach

动态 SQL 的另外一个常用的必要操作是需要对一个集合进行遍历,通常是在构建 IN 条件语句的时候。比如:

<select id="selectPostIn" resultType="domain.blog.Post">
  SELECT *
  FROM POST P
  WHERE ID in
  <foreach item="item" index="index" collection="list"
      open="(" separator="," close=")">
        #{item}
  </foreach>
</select>

foreach 元素的功能是非常强大的,它允许你指定一个集合,声明可以用在元素体内的集合项和索引变量。它也允许你指定开闭匹配的字符串以及在迭代中间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。



5.最后是接口:

使用@mapper注解:

@Mapper
public interface RechargeRecordDao {


public int saveRechargeRecord(RechargeRecord rechargeRecord);


// 根据ID查询对应的充值记录
public RechargeRecord findById(Long id);


// 根据会员id 分页查询充值记录
public List<RechargeRecord> findByMemberId(@Param("memberId")Long memberId);


}

注:@mapper注解加在接口上,接口内部的方法名对应的是*mapper.xml中的id

如: <select id="findById" resultMap="BaseResultMap">
  select * from mem_individual_bank_card where id = #{id}
  </select>


大致就这些,有一些乱,因为是在我阅读官方文档和使用中复制到文本中的,有时间我会整理一些。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值