MyBatis 框架基础入门

 

resultMap 的属性包括:

  • id :当前命名空间中的一个唯一标识,用于标识一个 resultMap
  • type:类的全限定名, 或者一个类型别名
  • autoMapping:为这个ResultMap开启或者关闭自动映射,该属性会覆盖全局的属性 autoMappingBehavior。默认值为:unset

resultMap 的子元素包括:

  • constructor:用来将结果注入到一个实例化好的类的构造方法中
    • idArg: ID 参数,标记结果作为 ID
    • arg:注入到构造方法的一个普通结果
  • id: 一个 ID 结果,标记结果作为 ID
  • result:注入到字段或 JavaBean 属性的普通结果
  • association:复杂的类型关联,多个结果合成的类型
    • 嵌入结果映射:结果映射自身的关联,也可以引用一个外部结果映射
  • collection:复杂类型的集 也可以引用一个外部结果映射
  • discriminator:使用结果值来决定使用哪个结果集
    • case:基本一些值的结果映射
      • 也可以引用一个外部结果映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.shiyanlou.mybatis.mapper.UserMapper">
    <!-- 自定义返回结果集 -->
    <resultMap id="userMap" type="User">
        <id property="id" column="id" javaType="int"></id>
        <result property="username" column="username" javaType="String"></result>
        <result property="password" column="password" javaType="String"></result>
        <result property="sex" column="sex" javaType="String"></result>
        <result property="address" column="address" javaType="String"></result>
    </resultMap>

    <!-- 定义 SQL 语句,其中 id 需要和接口中的方法名一致 -->
    <!-- useGeneratedKeys:实现自动生成主键 -->
    <!-- keyProperty: 唯一标记一个属性 -->
    <!-- parameterType 指明查询时使用的参数类型,resultType 指明查询返回的结果集类型 -->
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        insert into user (username,password,sex,address) values
        (#{username},#{password},#{sex},#{address})
    </insert>

    <update id="updateUser"  parameterType="User">
        update user set
        address=#{address} where
        id=#{id}
    </update>

    <delete id="deleteUser" parameterType="int">
        delete from user where
        id=#{id}
    </delete>

    <!-- 如未为 Java Bean 起类别名,resultType="com.shiyanlou.mybatis.model.User" -->

    <!-- 使用resultType时,一定要保证,你属性名与字段名相同;如果不相同,就使用resultMap -->
    <select id="selectUserById" parameterType="int" resultType="User">
        select * from user where id=#{id}
    </select>

    <select id="selectAllUser" resultMap="userMap">
        select * from user
    </select>

</mapper>

 

 

 

id 和 result 都是映射一个单独列的值到简单数据类型(数值型、字符串和日期等)的单独属性或字段 。

唯一不同的是 id 为主键映射,而 result 为其他数据库表字段到 JavaBean 属性的映射。

属性描述
property 映射到 JavaBean 的属性
column 数据库的列名或列名的重命名标签
javaType 一个 Java 类的完全限定名,或一个类型别名 。如果你映射到一个 JavaBean , MyBatis 通常可以断定类型。如果你映射到的是 HashMap,应该明确地指定 javaType 来保证所需的行为
jdbcType 所支持的 JDBC 类型,其仅作用在对插入,更新和删除操作允许为空的列。这是 JDBC 的需要,而 MyBatis 不需要。如果直接使用 JDBC 编程,需要指定这个类型且有允许为空的列

支持的 JDBC 类型:

BIT、FLOAT、CHAR、TIMESTAMP、OTHER、UNDEFINED、TINYINT、REAL、VARCHAR、BINARY、BLOB、NVARCHAR、SMALLINT、DOUBLE、LONGVARCHAR、VARBINARY、CLOB、NCHAR、INTEGER、NUMERIC、DATE、LONGVARBINARY、BOOLEAN、NCLOB、BIGINT、DECIMAL、TIME、NULL、CURSOR、ARRAY

    <resultMap id="userMap" type="User">
        <id property="id" column="id" javaType="int"></id>
        <result property="username" column="username" javaType="String"></result>
        <result property="password" column="password" javaType="String"></result>
        <result property="sex" column="sex" javaType="String"></result>
        <result property="address" column="address" javaType="String"></result>
    </resultMap>

 

 

select

在 MyBatis 中,对于每个 insert 、update 或 delete 操作通常对应多个 select 操作。

例如一个简单的查询:

<select id="selectUserById" parameterType="int" resultType="User">
    select * from user where id=#{id}
</select>

这个语句被叫做 selectUserById ,有一个 int(即 Integer) 型参数返回一个 User 类型的对象。

注意参数符号:#{id}

上述语句对于的 JDBC 语句如下:

String selectUserById = "select * from user where id=?";
PreparedStatement ps = conn.prepareStatement(selectUserById);
ps.setInt(1,id);

select 的属性:

属性描述
id 命名空间中唯一的标识符,可被其他语句引用
parameterType 传入该语句的参数的完全限定名或别名
resultType 该语句返回类型的完全限定名或别名,注意如果是集合情形,那应该是集合可以包含的类型,而不能是集合本身
resultMap 外部 resultMap 的命名引用
flushCache 任何语句的调用都会导致本地缓存和二级缓存被清空,默认为 false
useCache 导致本条语句的结果被二级缓存,默认为 true
timeout 设置驱动器在抛出异常前数据库返回请求结果的秒数,默认为 unset(依赖驱动)
fetchSize 尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认为 unset(依赖驱动)
statementType 让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认为 PREPARED
resultSetType FORWARD_ONLY,SCROLL_SENSITIVE 或 SCROLL_INSENSITIVE 中的一个,默认为 unset(依赖驱动)
databaseId 如果配置了 databaseIdProvider,MyBatis 会加载所有的不带 databaseId 或匹配当前 databaseId 的语句;如果带或者不带的语句都有,则不带的会被忽略
resultOrdered 仅针对嵌套结果 select 语句适用,如果为 true,就是假设包含了嵌套结果集或是分组了,默认为 false
resultSets 仅对多结果集适用,它将列出返回的结果集并为每个结果集给一个名称,名称用逗号分隔

注:返回时可以使用 resultType 或 resultMap,但不能同时使用

 

insert

例如一个插入操作:

<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
    insert into user (username,password,sex,address) values (#{username},#{password},#{sex},#{address})
</insert>

这里使用了 useGeneratedKeys 来实现自动生成主键策略,然后把 keyProperty 设成对应的列。

posted on 2017-08-07 16:51  michaelchan  阅读(96)  评论(0)    收藏  举报

导航