基于MyBaits开发的过程
1.sqlSession.selectList("EmpMapper.findAll"):SqlSession中封装的操作数据库的方法,MyBatis中操作数据库最主要的对象和所有的方法。
2.EmpMapper.findAll:String字符串,方法中要执行的sql语句的位置,即映射的sql语句。
3.:所有的sql语句存在的小区
4.类型
select * from emp
:具体sql所在的门牌号和sql本身+结果集
问题:
封装在Sqlsession对象中的方法在寻找sql的映射时,传入的参数是一个需要拼接的(namespace+id)的字符串,这种方式容易发生字符串拼写错误且编译时期不会提示的问题,存在一定的风险。
解决方式:Mapper接口开发
利用接口作为桥梁,在寻找sql映射时,带着相应的参数先走接口,接口对传来的参数进行了检查判断,同时接口会进行编译也就避免了字符串拼写错误和编译不提示的问题。
基于MyBatis接口开发的过程
1.sqlSession.getMapper(EmpMapper.class):SqlSession中封装的操作数据库的getMapper()` T getMapper(Class type)`方法
2.EmpMapper.class:接口,规定了方法名(id),参数和返回值接收类型
3.:所有的sql语句被规定的小区
4.
select * from emp
:具体sql所在的门牌号和sql本身+结果集类型(和接口中的相对应)
接口Mapper
package Ring1270.pra.MyBatis.Mapper;
import Ring1270.pra.MyBatis.pojo.Emp;
import java.util.List;
public interface EmpMapper {
/**
* 根据id查询员工信息
* @param id
* @return Emp
*/ public Emp findById(Integer id);
public List findAll();
}
1、创建一个接口,接口的全限定类名和mapper文件的namespace值要相同
2、mapper文件中每条要执行的SQL语句,在接口中要添加一个对应的方法,并且接口中的方法名和SQL标签上的id值相同
3、Mapper接口中方法接收的参数类型,和mapper.xml中定义的sql的接收的参数类型要相同
4、接口中方法的返回值类型和SQL标签上的resultType即返回值类型相同(如果方法返回值是集合,resultType只需要指定集合中的泛型)
关于XxxMapper.xml
实际开发中的xml文件
id,create_by,create_time,update_by,update_time,delete_flag,unit_update,import_flag,company_id,unit_code,unit_type,unit_fname_cn,unit_sname_cn,unit_fname_en
,unit_sname_en,unit_pid,unit_scope,unit_leveltype,unit_empid
,unit_chargeid,unit_hcityid,unit_wcityid,unit_cotcid
,status,unit_project,unit_sdate,unit_edate,unit_invreason,comment,unit_oaid,unit_qywxid,unit_ddid
,unit_dlocation,unit_dlocationX,unit_dlocationY,unit_ischart,unit_posttype,unit_company,unit_dept
select
from org_units
where 1=1
and id = #{orgUnits.id}
and create_by = #{orgUnits.createBy}
and create_time = #{orgUnits.createTime}
and update_by = #{orgUnits.updateBy}
and update_time = #{orgUnits.updateTime}
and delete_flag = #{orgUnits.deleteFlag}
and unit_update = #{orgUnits.unitUpdate}
and import_flag = #{orgUnits.importFlag}
and company_id = #{orgUnits.companyId}
and unit_code = #{orgUnits.unitCode}
and unit_type = #{orgUnits.unitType}
and unit_fname_cn like concat('%',#{orgUnits.unitFnameCn},'%')
and unit_sname_cn = #{orgUnits.unitSnameCn}
and unit_fname_en = #{orgUnits.unitFnameEn}
and unit_sname_en = #{orgUnits.unitSnameEn}
and unit_pid = #{orgUnits.unitPid}
and unit_scope = #{orgUnits.unitScope}
and unit_leveltype = #{orgUnits.unitLeveltype}
and unit_empid = #{orgUnits.unitEmpid}
and unit_chargeid = #{orgUnits.unitChargeid}
and unit_hcityid = #{orgUnits.unitHcityid}
and unit_wcityid = #{orgUnits.unitWcityid}
and unit_cotcid = #{orgUnits.unitCotcid}
and status = #{orgUnits.status}
and (now() between unit_sdate and unit_edate)
and unit_project = #{orgUnits.unitProject}
and unit_sdate = #{orgUnits.unitSdate}
and unit_edate = #{orgUnits.unitEdate}
and unit_invreason = #{orgUnits.unitInvreason}
and comment = #{orgUnits.comment}
and unit_ischart = #{orgUnits.unitIschart}
select
from (
SELECT
,@le:= IF (unit_pid = 0 ,0, IF( LOCATE(CONCAT('|',unit_pid,':'), @pathlevel) > 0 ,SUBSTRING_INDEX(SUBSTRING_INDEX(@pathlevel,CONCAT('|',unit_pid,':'),-1),'|',1) +1 ,@le+1)) levels,
@pathlevel:= CONCAT(@pathlevel,'|',id,':', @le ,'|') pathlevel,
@pathnodes:= IF( unit_pid =0,',0', CONCAT_WS(',', IF( LOCATE(CONCAT('|',unit_pid,':'),@pathall) > 0 ,SUBSTRING_INDEX(SUBSTRING_INDEX(@pathall,CONCAT('|',unit_pid,':'),-1),'|',1) ,@pathnodes ) ,unit_pid ) ) paths,
@pathall:=CONCAT(@pathall,'|',id,':', @pathnodes ,'|') pathall
FROM org_units, (SELECT @le:=0,@pathlevel:='', @pathall:='',@pathnodes:='') vv
where delete_flag=0
ORDER BY unit_pid
) src
(FIND_IN_SET(#{nodeId}, paths) OR id = #{nodeId}) and delete_flag = 0
and status in
#{item}
and (now() between unit_sdate and unit_edate)
and unit_type in
#{item}
#{LangWhere}
select
from org_units
delete_flag = 0
and company_id = #{companyId}
and id in
#{item}
and status in
#{item}
and (now() between unit_sdate and unit_edate)
and (unit_pid in
#{item}
or id in
#{item}
)
and unit_type in
#{item}
com.hrocloud.znjs.orgemp.model.OrgUnitsChart
and unit_code like concat('%',#{unitCode},'%')
select
from org_units
delete_flag = 0
and status = '02valid' and (now() between unit_sdate and unit_edate)
and unit_pid in
#{item}
and unit_type in
#{item}
select
from org_units
delete_flag = 0 and unit_update = 0
-- and status = '02valid' and (now() between unit_sdate and unit_edate)
and ( unit_pid in
#{item}
or id in
#{item}
)
select
from org_units
where delete_flag = 0 and unit_update = 0
and id = #{orgUnits.id}
and id in
#{item}
and company_id = #{orgUnits.companyId}
and unit_code like concat('%',#{orgUnits.unitCode},'%')
and unit_type = #{orgUnits.unitType}
and unit_fname_cn like concat('%',#{orgUnits.unitFnameCn},'%')
and unit_sname_cn like concat('%',#{orgUnits.unitSnameCn},'%')
and unit_fname_en like concat('%',#{orgUnits.unitFnameEn},'%')
and unit_sname_en like concat('%',#{orgUnits.unitSnameEn},'%')
and (FIND_IN_SET(id,fun_getAllSubOrgDept(#{orgUnits.unitPid}))
and (FIND_IN_SET(id,fun_getAllSubOrgUnits(#{orgUnits.unitPid}))
or id=#{orgUnits.unitPid} )
)
and unit_leveltype = #{orgUnits.unitLeveltype}
and unit_empid = #{orgUnits.unitEmpid}
and unit_chargeid = #{orgUnits.unitChargeid}
and unit_hcityid = #{orgUnits.unitHcityid}
and unit_wcityid = #{orgUnits.unitWcityid}
and unit_cotcid = #{orgUnits.unitCotcid}
and unit_ischart = #{orgUnits.unitIschart}
and status = #{orgUnits.status}
and (Date(#{orgUnits.qcDate}) between unit_sdate and unit_edate)
and (now() between unit_sdate and unit_edate)
and unit_project = #{orgUnits.unitProject}
select
from org_units
where delete_flag = 0 and unit_update = 0
and unit_type = #{orgUnits.unitType}
and (FIND_IN_SET(id,fun_getAllSubOrgDept(#{orgUnits.unitPid}))
and (FIND_IN_SET(id,fun_getAllSubOrgUnits(#{orgUnits.unitPid}))
or id=#{orgUnits.unitPid} )
)
and status = #{orgUnits.status}
and (Date(#{orgUnits.qcDate}) between unit_sdate and unit_edate)
and (now() between unit_sdate and unit_edate)
select
id,unit_fname_cn,unit_empid
from org_units
where delete_flag = 0
and unit_update = 0
and status = '02valid'
and unit_ischart = 1
and (now() between unit_sdate and unit_edate)
and unit_pid = #{unitPid}
select
id,unit_fname_cn,unit_empid
from org_units
where delete_flag = 0
and unit_update = 0
and status = '02valid'
and unit_ischart = 1
and (now() between unit_sdate and unit_edate)
and id = #{id}
select
from org_units
delete_flag = 0 and unit_update = 0
and status = '02valid' and (now() between unit_sdate and unit_edate)
and unit_pid =#{pid}
动态SQL标签
标签
标签:是根据test属性中的布尔表达式的值,从而决定是否执行包含在其中的SQL片段。如果判断结果为true,则执行其中的SQL片段;如果结果为false,则不执行其中的SQL片段
标签
标签:用于对包含在其中的SQL片段进行检索,在需要时可以生成where关键字,并且在需要时会剔除多余的连接词(比如and或者or)
标签
遇到choose标签判断when里面的条件,条件满足执行里面的sql,不满足继续判断下一个when标签的里面的条件,满足执行sql,不满足继续向下判断,都不满足执行otherwise标签里面的sql,类似switch,case,default。
标签
foreach标签:可以对传过来的参数数组或集合进行遍历,以下是foreach标签上的各个属性介绍:
1.item:必需,若collection为数组或List集合时,item表示其中的元素,若collection为map中的key,item表示map中value(集合或数组)中的元素
2.open:可选,表示遍历生成的SQL片段以什么开始,最常用的是左括号'('
3.collection:必需,值为遍历的集合类型,例如:如果参数只是一个数组或List集合,则collection的值为array或list;如果传的是多个参数,用map封装,collection则指定为map中的key。
4.close:可选,表示遍历生成的SQL片段以什么结束,最常用的是右括号')'
5.separator:可选,每次遍历后给生成的SQL片段后面指定间隔符
6.index:当前迭代的序号
标签
抽取公共sql片段,避免重复