mybatis配置文件
来源:互联网 发布:js文件中定义全局变量 编辑:程序博客网 时间:2024/06/18 11:38
SqlMapConfig.xml配置文件中,可以配置
1、加载外部的资源文件
<properties resource="jdbc.properties">
<property name="jdbc.password" value="12345"/>
</properties>
2、<!-- 全局参数配置:二级缓存,延迟加载
<settings></settings>
-->
<!-- 定义别名 -->
<typeAliases>
<!-- 给单个的类起别名
<typeAlias type="com.qf.domain.User" alias="user"/>
-->
<!-- 给指定包下的类起别名
别名的定义规则:类名首字母小写
-->
<package name="com.qf.domain"/>
</typeAliases>
3、
<!-- 配置mybatis的环境信息 -->
<environments default="development">
<environment id="development">
<!-- 配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源,采用mybatis连接池 -->
<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>
4、<!-- 加载映射文件 -->
<mappers>
<!-- 使用资源的路径 -->
<mapper resource="User.xml"/>
<!-- <mapper resource="com/qf/mapper/UserMapper.xml"/> -->
<!--
使用资源的绝对路径
<mapper url=""/> -->
<!--
Mapper接口的全类名
要求:Mapper接口的名称与映射文件名称一致
必须在同一个目录下
<mapper class="com.qf.mapper.UserMapper"/>
-->
<!-- 加载某个包下的映射文件 (推荐)
要求:Mapper接口的名称与映射文件名称一致
必须在同一个目录下
-->
<package name="com.qf.mapper"/>
</mappers>
在*Mapper.xml的配置中,直接填写sql语句。
namespace值为对应的全类名
<mapper namespace="com.qf.mapper.UserMapper">
<select id="findByQueryVO" parameterType="userQueryVO"
resultType="user">
<!-- select * from user where sex=#{userExt.sex}
and username like '%${userExt.username}%'
-->
<!--
用if进行判断是否为空时,不仅要判断null,也要判断空字符串‘’;
Where标签:会去掉条件中的第一个and符号。
and
or
-->
select * from user
<where>
<if test="userExt!=null">
<if test="userExt.sex != null and userExt.sex!=''">
and sex=#{userExt.sex}
</if>
<if test="userExt.username!=null and userExt.username !=''">
and username like '%${userExt.username}%'
</if>
</if>
</where>
</select>
<select id="findCountByQueryVO" parameterType="userQueryVO" resultType="int">
select count(*) from user
<where>
<include refid="where_findByQueryVO"></include>
</where>
</select>
<!--
sql片段:将相同的sql语句抽取出来形成sql片段,之后再statement中引用片段
id:sql判断的名称,使用该名称引用片段
-->
<sql id="where_findByQueryVO">
<if test="userExt!=null">
<if test="userExt.sex != null and userExt.sex!=''">
and sex=#{userExt.sex}
</if>
<if test="userExt.username!=null and userExt.username !=''">
and username like '%${userExt.username}%'
</if>
</if>
</sql>
<!--
select * from user where id in (1,2,3,4,5)
foreach:
collection:遍历的集合
item:集合中的一个元素
open:起始部分 左括号
close:结束部分 右括号
separator:分隔符,逗号
在foreach标签之间使用#{id}表示遍历的元素
-->
<select id="findByIdList" parameterType="userQueryVO" resultType="user">
select * from user
<where>
<if test="idList!= null and idList.size()>0">
id in
<foreach collection="idList" item="id"
open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
<!--
使用list集合作为输入参数
-->
<select id="findByList" parameterType="java.util.List" resultType="user">
select * from user
<where>
<if test="list!= null and list.size()>0">
<foreach collection="list" item="id"
open="id in (" close=")" separator=",">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>
- MyBatis配置文件
- mybatis 配置文件
- mybatis配置文件
- mybatis 配置文件
- MyBatis配置文件
- Mybatis配置文件
- mybatis配置文件
- mybatis配置文件
- mybatis配置文件
- mybatis配置文件
- mybatis配置文件
- mybatis配置文件
- 【mybatis】配置文件
- mybatis 配置文件
- mybatis配置文件
- MyBatis 配置文件
- mybatis配置文件
- mybatis配置文件
- bootstrap-入门
- HDU 1896 优先队列
- WMB8.0中调用WebService超时问题
- UVALive
- springMVC自动封装对象
- mybatis配置文件
- 打印div内容
- 实用小技巧-1
- TensorFlow实战:Chapter-3(CNN-1-卷积神经网络简介)
- 字符串连接算法挑战
- 【2017/7】实验记录
- JAVA学习笔记(1):类和对象是什么
- PAT (Advanced Level) Practise 1036 Boys vs Girls (25)
- 移动开发如何选择正确的框架?