现在流行的orm框架几乎全都是一些xml配置啦,mybatis相对hibernate优雅之处就在于Mapper XML吧(不过用过jooq之后,配置这种东西真是好伤...)。
Mapper XML 文件
MyBatis的映射语句异常强大,省去了JDBC将近95%的代码,SQL映射文件有很少的几个顶级元素。
cache:给定命名空间的缓存配置。
cache-ref:其他命名空间缓存配置的引用;
resultMap:最复杂也是最强大的元素,用来描述如何从数据库结果集中加载对象;
sql:可被其他语句引用的可重用语句快;
insert:映射插入语句;
update:映射更新语句;
delete:映射删除语句;
select:映射查询语句。
select
查询语句是MyBatis中最常用的元素之一,简单的select元素非常简单。如:
<select id="selectPerson" parameterType="int" resultType="hashmap">
SELECT * FROM PERSON WHERE ID = #{id}
</select>
这个语句被称作selectPerson,接收一个int型的参数,返回一个HashMap对象,其中列名是key,值是value。
#{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:将会传入这条语句的参数类的完全限定名或者别名。
resultType:返回的期望类型的完全限定名或者别名;
resultMap:外部resultMap的命名引用。resultMap和resultType不能同时使用;
flushCache:将其设置为true,任何时候只要语句被调用,都会导致本地缓存和二级缓存被清空,默认:false;
useCache:使本条语句的结果被二级缓存,默认:对select元素为true;
timeout:抛出异常之前等待数据库返回请求结果的秒数。
fetchSize:每次返回的结果行数,默认依赖驱动;
statementType:STATEMENT,PREPARED或CALLABLE的一个。默认:PREPARED;
resultSetType:FORWARD_ONLY,SCROLL_SENSITIVE或SCROLL_INSENSITIVE中的一个,默认依赖驱动;
insert,uypdate,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>
这些看起来都很简单的样子。
sql:sql语句片段可以被包含在其他语句中
<sql id="userColumns"> id,username,password</sql>
<select id="selectUsers" resultType="map">
select <include refid="userColumns"/>
from some_table
where id = #{id}
</select>
ResultMaps:
ResultMaps除了将结果集映射到hashmap上,还可以使用javabeans或者pojos。
package com.someapp.model;
public class User {
private int id;
private String username;
private String hashedPassword;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getHashedPassword() {
return hashedPassword;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
User类有三个属性:id,username和hashedPassword。这些在select语句中会精确匹配到列名:
<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>
以上是简单的mapper映射。下次再来写更加复杂的高级映射。(头会有点晕啊!!!)