<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.xxx.mapper.EmpMapper"><!--
1 接口的名字和Mapper映射为文件名字必须保持一致(不包含拓展名)
2 Mapper映射文件的namespace必须是接口的全路径名
3 sql语句的id必须是对应方法的名
4 DeptMapper映射文件应该和接口编译之后放在同一个目录下
--><!--List<Emp> findAll();--><selectid="findAll"resultType="emp">
select * from emp
</select></mapper><!--
单个基本数据类型作为方法参数
#{}中可以随便写,遵循见名知意
Emp findByEmpno(int empno);
--><selectid="findByEmpno"resultType="emp">
select * from emp where empno =#{empno}
</select><!--
多个基本数据类型作为方法参数
List<Emp> findByDeptnoAndSal(@Param("detpno") int deptno,@Param("sal") double sal);
方式1 arg* arg0 arg1 arg2 数字是索引,从0开始
方式2 param* param1 param2 param3 数字是编号,从1开始
使用别名
List<Emp> findByDeptnoAndSal(@Param("detpno") int deptno,@Param("sal") double sal);
通过@Param注解使用别名之后,就不能再使用arg* 但是可以继续使用param*
--><selectid="findByDeptnoAndSal"resultType="emp"><!--select * from emp where deptno =#{arg0} and sal >= #{arg1}--><!-- select * from emp where deptno =#{param1} and sal >= #{param2}--><!-- select * from emp where deptno =#{deptno} and sal >= #{sal}--></select><!--
参数是map,{}写键的名字
--><selectid="findByDeptnoAndSal2"resultType="emp"parameterType="map"><!--select * from emp where deptno =#{arg0} and sal >= #{arg1}--><!-- select * from emp where deptno =#{param1} and sal >= #{param2}-->
select * from emp where deptno =#{deptno} and sal >= #{sal}
</select><!--单个引用类型,{}中写的使用对象的属性名--><selectid="findByDeptnoAndSal3"resultType="emp"parameterType="emp">
select * from emp where deptno =#{deptno} and sal >= #{sal}
</select><!--
多个引用类型作为方法参数
List<Emp> findByDeptnoAndSal4(@Param("empa") Emp empa,@Param("empb") Emp empb);
如果用@Param定义了别名,那么就不能使用arg*.属性名,但是可以使用param*.属性名和别名.属性名
--><selectid="findByDeptnoAndSal4"resultType="emp"><!-- select * from emp where deptno =#{arg0.deptno} and sal >= #{arg1.sal} -->
select * from emp where deptno =#{param1.deptno} and sal >= #{param2.sal}
<!-- select * from emp where deptno =#{empa.deptno} and sal >= #{empb.sal}--></select>
<!--List<Emp> findByCondition(Emp emp);--><selectid="findByCondition"resultType="emp">
select * from EMP where 1=1
<iftest="empno != null">
and empno =#{empno}
</if><iftest="ename != null and ename != ''">
and ename like concat('%',#{ename},'%')
</if><iftest="job != null and job != ''">
and job =#{job}
</if><iftest="mgr != null">
and mgr =#{mgr}
</if><iftest="hiredate != null">
and hiredate =#{hiredate}
</if><iftest="sal != null">
and sal =#{sal}
</if><iftest="comm != null">
and comm =#{comm}
</if><iftest="deptno != null">
and deptno =#{deptno}
</if></select>
4.2 where
通常where配合if一起使用,忽略if条件内第一个and
<!--List<Emp> findByCondition(Emp emp);--><selectid="findByCondition"resultType="emp">
select * from EMP
<where><iftest="empno != null">
and empno =#{empno}
</if><iftest="ename != null and ename != ''">
and ename like concat('%',#{ename},'%')
</if><!--......--></where></select>
<iftest="ename != null and ename != ''"><bindname="likePattern"value="'%'+param1+'%'"/>
and ename like concat #{likePattern}
</if>
4.7 sql
定义一段公共的sql,可以引用
<sqlid="empColumn">empno,ename,job,mgr,hiredate,sal,comm,deptno</sql><sqlid="baseSelect">select <includerefid="empColumn"></include> from emp</sql><!--List<Emp> findByCondition(Emp emp);--><selectid="findByCondition"resultType="emp"><includerefid="baseSelect"></include><trimprefix="where"prefixOverrides="and"><iftest="empno != null">
and empno =#{empno}
</if><iftest="ename != null and ename != ''"><bindname="likePattern"value="'%'+ename+'%'"/>
and ename like #{likePattern}
</if><iftest="job != null and job != ''">
and job =#{job}
</if><iftest="mgr != null">
and mgr =#{mgr}
</if><iftest="hiredate != null">
and hiredate =#{hiredate}
</if><iftest="sal != null">
and sal =#{sal}
</if><iftest="comm != null">
and comm =#{comm}
</if><iftest="deptno != null">
and deptno =#{deptno}
</if></trim></select>
4.8 for each
<!--List<Emp> findByEmpnos1(int[] empnos);
collection="" 遍历的集合或者是数组
参数是数组,collection中名字指定为array
参数是List集合,collection中名字指定为list
separator="" 多个元素取出的时候 用什么文字分隔
open="" 以什么开头
close="" 以什么结尾
item="" 中间变量名
for(Person per:PersonList)
--><selectid="findByEmpnos1"resultType="emp">
select * from emp where empno in
<foreachcollection="array"separator=","open="("close=")"item="deptno">
#{deptno}
</foreach></select><!-- List<Emp> findByEmpnos2(List<Integer> empnos);--><selectid="findByEmpnos2"resultType="emp">
select * from emp where empno in
<foreachcollection="list"separator=","open="("close=")"item="deptno">
#{deptno}
</foreach></select>
<mappernamespace="com.xxx.mapper.EmpMapper"><resultMapid="empMap"type="emp"><idproperty="empno"column="empno"></id><resultproperty="name"column="ename"></result><resultproperty="job"column="job"></result><resultproperty="sal"column="sal"></result><resultproperty="hiredate"column="hiredate"></result><resultproperty="mgr"column="mgr"></result><resultproperty="comm"column="comm"></result><resultproperty="deptno"column="deptno"></result></resultMap><selectid="findByEmpno"resultMap="empMap">
select * from emp where empno =#{empno}
</select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.xxx.mapper.EmpMapper"><!--Emp findEmpJoinDeptByEmpno(int empno);--><resultMapid="empJoinDept"type="emp"><!--设置emp本身的八个属性的映射关系--><idproperty="empno"column="empno"></id><resultproperty="ename"column="ename"></result><resultproperty="job"column="job"></result><resultproperty="sal"column="sal"></result><resultproperty="hiredate"column="hiredate"></result><resultproperty="mgr"column="mgr"></result><resultproperty="comm"column="comm"></result><resultproperty="deptno"column="deptno"></result><!--
association 处理一对一
封装一对一信息关系的标签
property emp类的属性名
javaType 用哪个类的对象给属性赋值
--><associationproperty="dept"javaType="dept"><idcolumn="deptno"property="deptno"></id><resultcolumn="dname"property="dname"></result><resultcolumn="loc"property="loc"></result></association></resultMap><selectid="findEmpJoinDeptByEmpno"resultMap="empJoinDept">
select * from
emp e
left join dept d
on e.deptno =d.deptno
where empno = #{empno}
</select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.xxx.mapper.DeptMapper"><!--Dept findDeptJoinEmpsByDeptno(int deptno);--><resultMapid="deptJoinEmps"type="dept"><idcolumn="deptno"property="deptno"></id><resultcolumn="dname"property="dname"></result><resultcolumn="loc"property="loc"></result><!--处理一对多关系的标签--><collectionproperty="empList"ofType="emp"><!--设置emp本身的八个属性的映射关系--><idproperty="empno"column="empno"></id><resultproperty="ename"column="ename"></result><resultproperty="job"column="job"></result><resultproperty="sal"column="sal"></result><resultproperty="hiredate"column="hiredate"></result><resultproperty="mgr"column="mgr"></result><resultproperty="comm"column="comm"></result><resultproperty="deptno"column="deptno"></result></collection></resultMap><selectid="findDeptJoinEmpsByDeptno"resultMap="deptJoinEmps">
select * from dept d left join emp e on d.deptno =e.deptno where d.deptno =#{deptno}
</select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.xxx.mapper.ProjectMapper"><!--Project findProjectJoinEmpsByPid(int pid);--><resultMapid="projectJoinEmps"type="project"><idcolumn="pid"property="pid"></id><resultcolumn="pname"property="pname"></result><resultcolumn="money"property="money"></result><!--一对多 集合属性 collection--><collectionproperty="projectRecords"ofType="projectRecord"><idcolumn="empno"property="empno"></id><idcolumn="pid"property="pid"></id><!--一对一 --><associationproperty="emp"javaType="emp"><idproperty="empno"column="empno"></id><resultproperty="ename"column="ename"></result><resultproperty="job"column="job"></result><resultproperty="sal"column="sal"></result><resultproperty="hiredate"column="hiredate"></result><resultproperty="mgr"column="mgr"></result><resultproperty="comm"column="comm"></result><resultproperty="deptno"column="deptno"></result></association></collection></resultMap><selectid="findProjectJoinEmpsByPid"resultMap="projectJoinEmps">
select * from
project p
left join projectrecord pr
on p.pid = pr.pid
left join emp e
on e.empno = pr.empno
where p.pid= #{pid}
</select></mapper>