一:sql映射 1、select 属性 描述 id 此命名空间内的标识符 parameterType 参数的类的全名或者alias,可选。默认为空。 parameterMap Deprecated resultType 返回结果的类型全名或alias,如果结果是集合,此类型表示的是集合的成员类型。 resultMap 使用指定的resultMap来映射结果集。resultMap 和 resultType只能二选一。 flushCache 如果为true,每次调用,一级缓存和二级缓存都会回写。select语句中默认为false。 useCache 如果为true,结果将在二级缓存中缓存。select语句中默认为true timeout 设置超时,若超时则抛出异常。 fetchSize 尝试在获取数据时分批获取。 statementType STATEMENT,PREPARED或者CALLABLE. 分别对应JDBC中的Statement,PreparedStatement和CallableStatement respectively。默认PREPARED. resultSetType FORWARD_ONLY,SCROLL_SENSITIVE或者SCROLL_INSENSITIVE。默认为空。 databaseId 使用特定的databaseIdProvider resultOrdered 嵌套查询时使用。 resultSets 多返回集合时使用。 2. 修改语句(insert,update,DELETE) 这3种语句的属性基本和上边select的一样,INSERT和UPDATE的语句有几个特殊的属性如下: 属性 描述 useGeneratedKeys 将使用JDBC的getGeneratedKeys方法来获取主键的值。默认为false。 keyProperty 主键。MyBatis会将生成的主键赋给这个列。联合主键使用逗号隔开。 keyColumn 特定数据库需要使用。 3. SQl语句段(sql标签) 标签可以定义一段sql语句段,可以在其他语句中引入使用。sql标签可以包含参数。示例如下: <sql id="userColumns">${alias}.id,${alias}.username,${alias}.password </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> 参数(property)也可以在refid或者include进来的sql中使用。示例如下: <sql id="sometable"> ${prefix}Table </sql> <sql id="someinclude"> from <include refid="${include_target}"/> </sql> <select id="select" resultType="map"> select field1, field2, field3 <include refid="someinclude"> <property name="prefix" value="Some"/> <property name="include_target" value="sometable"/> </include> </select> 4. #{}和${}的区别 #{}在底层实现上使用?做占位符来生成PreparedStatement,然后将参数传入,大多数情况都应使用这个,它更快、更安全。 ${}将传入的数据直接显示生成在sql中。如:order by ${user_id},如果传入的值是111,那么解析成sql时的值为order by 111, 如果传入的值是id,则解析成的sql为order by id. 5. 类型别名 什么地方都少不了这种小技巧,可以让你少打很多字。 <typeAlias type="com.someapp.model.User" alias="User"/> 6. ResultMap 官方文档上说这个特性是整个MyBatis中最强大的特性(没有之一)。其实它做的就是映射结果集。 MyBatis底层使用JDBC,所以查询出来的结果是ResultSet,如果resultType是一个对象,MyBatis底层就会创建一个resultMap,并根据字段名一一对应上这个对象。如果你有一个查询,它的结果集非常复杂,可以使用resultMap来做映射。 cache和cach-ref 使用cache标签在映射文件内(某命名空间内)实现二级缓存,其所有属性都有缺省值,所以单单一个标签就可以生效。cach-ref可以在两个映射文件之间共享缓存 <cache eviction="LRU"//缓存移除策略 flushInterval="60000"//默认不设置,不定时刷新 size="1024" readOnly="false"/> 二:demo1: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- 既是实体的映射文件。类似orm。同时又是dao的实现代码 --> <mapper namespace="com.etc.dao.NewsDao"> <cache readOnly="true" size="100000" eviction="FIFO" flushInterval="60000"> </cache> <!-- 配置实体和数据表的映射关系 --> <resultMap type="News" id="NewsResult"> <id column="id" property="id"/> <!-- 主键 --> <result column="title" property="title"/> <result column="content" property="content"/> </resultMap> <!-- 查询全部,实现findAll方法 --> <select id="findAll" resultMap="NewsResult"> select * from news </select> <!-- 精确查询 ,实现findbyid的方法--> <select id="findById" parameterType="java.lang.Integer" resultType="News"> select * from news where id=#{id} </select> <!-- 添加 --> <insert id="add" parameterType="News" > insert into news (title,content) values(#{title},#{content}) </insert> <!-- 修改--> <update id="update" parameterType="News" > update news set title=#{title},content=#{content} where id=#{id} </update> <!-- 删除 --> <delete id="delete" parameterType="java.lang.Integer"> delete from news where id=#{id} </delete> </mapper> ===============================================================================================