核心配置文件
配置文件顺序
<properties>
配置属性元素,通过外部的配置动态地更换内部定义的属性。
一般用于数据库的连接(db.properties)。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
<properties resource="db.properties"/>
<settings>
用于改变MyBatis运行时的行为,例如开启二级缓存、开启延迟加载等。
设置MyBatis延迟加载的配置
<settings>
<!-- 打开延迟加载的开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 将积极加载改为延迟加载,即按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
用于设置别名,
<typeAliases>
<typeAliases alias="user" name="com.qcby.mybatis.po.User"/>
</typeAliases>
通过扫描包的形式
<typeAliases>
<package name="com.qcby.mybatis.po"/>
</typeAliases>
<objectFactory>
<plugins>
<environments>
配置多种数据库
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<databaseIDProvider>
<mappers>
用于指定MyBatis映射文件的位置。
单映射方式
<!-- 类路径引入 -->
<mappers>
<mapper resource="com.qcby.mybatis.mapper.UserMapper.xml" />
</mappers>
<!-- 本地文件路径引入 -->
<mappers>
<mapper url="file://D:/demo/com/qcby/mybatis/mapper/UserMapper.xml"/>
</mappers>
<!-- 接口类引入 -->
<mappers>
<mapper class="com.qcby.mybatis.mapper.UserMapper"/>
</mappers>
使用扫描包的形式,对包下所有文件进行映射
<mappers>
<package name="com.qcby.mybatis.mapper"/>
</mappers>
主要元素
<select>:查询
<insert>:添加
<update>:修改
<delete>:删除
mapper中的动态SQL
1.<if>
判断语句,用于单条件分支判断
<select id="" resultType="">
select * from user where 1=1
<if test="userName!=null and userName!=''">
and userName like concat('%',#{userName},'%')
</if>
<if test="userAge!=null and userAge!=''">
and userAge=#{userAge}
</if>
</select>
2.<choose><when><otherwise>
相当于Java中的(switch...case...default)语句,用于多条件分支判断
<select id="" resultType="">
select * from user where 1=1
<choose>
<when test="userName!=null and userName!=''">
and userName like concat('%',#{userName},'%')
</when>
<when test="userAge!=null and userAge!=''">
and userAge=#{userAge}
</when>
<otherwise>
and userPhone is not null
</otherwise>
</choose>
</select>
3.<where><trem>
在查询条件中使用,一般<where>更方便使用。
<where>会自动拼装SQL语句,其中多余内容"and"或"or",<where>元素也会自动将他们去除。
<select id="" resultType="">
select * from user
<where>
<if test="userName!=null and userName!=''">
and userName like concat('%',#{userName},'%')
</if>
<if test="userAge!=null and userAge!=''">
and userAge=#{userAge}
</if>
</where>
</select>
<trim>元素的作用是去除一些特殊的字符串,prefix属性代表的是语句的前缀(这里使用where来连接后面的SQL片段),而prefixOvereides属性代表的是需要去除的那些特殊字符串(这里定义去除SQL中的and),下面的写法与<where>元素基本等效。
<select id="" resultType="">
select * from user
<trim prefix="where" prefixOverrides="and">
<if test="userName!=null and userName!=''">
and userName like concat('%',#{userName},'%')
</if>
<if test="userAge!=null and userAge!=''">
and userAge=#{userAge}
</if>
</trim>
</select>
4.<set>
<set>元素会去除SQL语句中最后一个多余的逗号。
注意:在映射文件中使用<set><if>元素组合进行update语句动态SQL组装时,如果<set>元素内包含的内容都为空,则会出现SQL语法错误。在更新时,要确保传入的字段不能都为空。
<update id="" resultType="">
update user
<set>
<if test="userName!=null and userName!=''">
userName=#{userName},
</if>
<if test="userAge!=null and userAge!=''">
userAge=#{userAge},
</if>
</set>
where userId=#{userId}
</update>
5.<foreach>
循环语句,常用于in语句等列举条件中。
属性:
item:循环中当前的元素
index:当前元素在集合的位置下标
collection:传递过来的参数类型(array、list/collection、map、包装类名)
separator:各个元素的间隔符
注意:当迭代器对象是数组时,index是当前迭代的次数,item的值是本次迭代获取的元素。使用字典时,index是键,item是值
<delete id="deleteByIds">
delete from user where userID in
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
6.<bind>
通过OGNL表达式创建一个变量,绑定到上下文,常用于模糊查询的sql中。
<select id="" resultType="">
<bind name="pattern_username" value="'%'+username+'%'" />
select * from user
where username like #{pattern_username}
</select>