//获取单个实体,@Param("eid")也可以不用
Emp getEmp(@Param("eid")Integer eid);
<select id="getEmp" resultType="Emp">
select * from emp where eid=#{eid}
select>
public List<Emp> getEmps();
<select id="getEmps" resultType="Emp">
select * from emp;
select>
//查询行数
int getCount();
<select id="getCount" resultType="Integer">
select count(*) from emp
select>
//返回一条数据的多个字段
Map<String,String> getEmpToMap(Integer eid);
<select id="getEmpToMap" resultType="map">
select * from emp where eid=#{eid}
select>
//返回多条数据的多个字段
List<Map<String,String>>getEmpToListMap();
<select id="getEmpToMap" resultType="map">
select * from emp
select>
//模糊查询
List<Emp>dim(String email);
<select id="dim" resultType="Emp">
select * from emp where email "%"#{email}"%"
select>
<select id="getEmp2" resultType="Emp">
select * from emp where 1=1
<if test="empName !='' and empName != null">
and empName=#{empName}
if>
<if test="sex !='' and sex != null">
and sex=#{sex}
if>
<if test="age !='' and age != null">
and age=#{age}
if>
select>
如果需要用where条件的话,一般使用where和if标签
<select id="getEmp3" resultType="Emp">
select * from emp
<where>
<if test="empName !='' and empName != null">
empName=#{empName}
if>
<if test="sex !='' and sex != null">
and sex=#{sex}
if>
<if test="age !='' and age != null">
and age=#{age}
if>
where>
select>
trim用于去掉或添加标签中的内容
常用属性:
<select id="getEmp4" resultType="Emp">
select * from emp
<trim prefix="where" suffixOverrides="and">
<if test="empName !='' and empName != null">
empName=#{empName} and
if>
<if test="sex !='' and sex != null">
sex=#{sex} and
if>
<if test="age !='' and age != null">
age=#{age}
if>
trim>
select>
foreach一般用来循环数组和集合
常用属性
<insert id="addEmps" parameterType="java.util.ArrayList">
insert into emp (empName,age,sex,email,did) values
<foreach collection="list" index="index" separator="," item="item">
(#{empName},#{age},#{sex},#{email},#{did})
foreach>
insert>
<select id="getEmp5" resultType="Emp">
select * from emp where eid in
<foreach collection="eids" item="item" separator="," open="(" close=")">
#{item}
foreach>
select>