配置
详见mybatis文档
https://mybatis.org/mybatis-3/sqlmap-xml.html#Auto-mapping
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/>
</settings>
<typeAliases>
<package name="org.sang.bean"/>
<typeAlias alias="Author" type="domain.blog.Author"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="java.math.RoundingMode"/>
<package name="org.mybatis.example"/>
</typeHandlers>
<objectFactory type="org.mybatis.example.ExampleObjectFactory">
<property name="someProperty" value="100"/>
</objectFactory>
<plugins>
<plugin interceptor="org.mybatis.example.ExamplePlugin">
<property name="someProperty" value="100"/>
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="..." value="..."/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<databaseIdProvider type="DB_VENDOR">
<property name="SQL Server" value="sqlserver"/>
<property name="DB2" value="db2"/>
<property name="Oracle" value="oracle" />
</databaseIdProvider>
<mappers>
<mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
<mapper url="file:///var/mappers/AuthorMapper.xml"/>
<mapper class="org.mybatis.builder.BlogMapper"/>
<package name="org.mybatis.builder"/>
</mappers>
</configuration>
映射器
映射器主要元素
元素 |
描述 |
备注 |
select |
查询语句 |
可以自定义参数,返回结果集 |
insert |
插入语句 |
执行后返回一个整数,代表插入条数 |
update |
更新语句 |
执行后返回一个整数,代表更新条数 |
delete |
删除语句 |
执行后返回一个整数,代表删除条数 |
parameterMap |
定义参数映射关系 |
不建议使用 |
sql |
定义sql |
可以被到处引用 |
resultMap |
数据库结果集 |
提供映射规则 |
cache |
命名空间缓存配置 |
|
cache-ref |
其他命名空间缓存配置 |
|
select元素
元素 |
描述 |
备注 |
id |
id+mapper的namespace的组合是唯一的 |
如果不唯一抛出异常 |
parameterType |
可以给出类的全限定名,也可以是别名 |
可以选择javabean,map等复杂参数传递 |
resultType |
可以给出类的全限定名,在允许自动匹配的情况下通过javabean映射规范映射或者定义为int,double,float也可以使用别名 |
常用参数之一 |
resultMap |
自定义映射规范 |
非常复杂 |
flushCache |
调用sql后。是否要求mybatis清除之前查询的本地和二级缓存 |
默认false |
useCache |
启动二级缓存开关 |
默认True |
timeout |
超时参数,默认秒,超时抛异常 |
默认是jdbc设置的秒 |
fetchSize |
获取记录总条数 |
默认是jdbc设置的条数 |
databaseId |
- |
提供多种数据库支持 |
<select
id="selectPerson"
parameterType="int"
parameterMap="deprecated"
resultType="hashmap"
resultMap="personResultMap"
flushCache="false"
useCache="true"
timeout="10"
fetchSize="256"
statementType="PREPARED"
resultSetType="FORWARD_ONLY">
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">
resultMap
<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>
缓存
<cache
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
<?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" >
<mapper namespace="dao.MUserMapper" >
<resultMap id="BaseResultMap" type="rml.model.MUser" >
<id column="ID" property="id" jdbcType="VARCHAR" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
<result column="AGE" property="age" jdbcType="DECIMAL" />
<result column="ADDRESS" property="address" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
ID, NAME, AGE, ADDRESS
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from MUSER
where ID = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from MUSER
where ID = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="rml.model.MUser" >
insert into MUSER (ID, NAME, AGE,
ADDRESS)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=DECIMAL},
#{address,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="rml.model.MUser" >
insert into MUSER
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="name != null" >
NAME,
</if>
<if test="age != null" >
AGE,
</if>
<if test="address != null" >
ADDRESS,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=DECIMAL},
</if>
<if test="address != null" >
#{address,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="rml.model.MUser" >
update MUSER
<set >
<if test="name != null" >
NAME = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
AGE = #{age,jdbcType=DECIMAL},
</if>
<if test="address != null" >
ADDRESS = #{address,jdbcType=VARCHAR},
</if>
</set>
where ID = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="rml.model.MUser" >
update MUSER
set NAME = #{name,jdbcType=VARCHAR},
AGE = #{age,jdbcType=DECIMAL},
ADDRESS = #{address,jdbcType=VARCHAR}
where ID = #{id,jdbcType=VARCHAR}
</update>
<select id="getAll" resultMap="BaseResultMap">
select ID, NAME, AGE, ADDRESS from muser
</select>
</mapper>