详解MyBatis的getMapper()接口、resultMap标签、Alias别名、 尽量提取sql列、动态操作
一、getMapper()接口
解析:getMapper()接口IDept.class定义一个接口,
挂载一个没有实现的方法,特殊之处,借楼任何方法,必须和小配置中id属性是一致的
通过代理:生成接口的实现类名称,在MyBatis底层维护名称$$Dept_abc,selectDeptByNo()
相当于是一个强类型
Eg
第一步:在cn.happy.dao中定义一个接口
packagecn.happy.dao; importjava.util.List; importcn.happy.entity.Dept; publicinterfaceIDeptDao{ //查看全部---------getAllDept要和小配置里面的id一样 publicList<Dept>getAllDept(); }
第二步:IDept.xml配置小配置
解析:select里面的Id属性要和接口里面的接口方法名一样;mapper的namespace属性包名是cn.happy.dao.IDeptDao接口
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="cn.happy.dao.IDeptDao"> <selectid="getAllDept"resultType="cn.happy.entity.Dept"> select*fromDept </select> </mapper>
第三步:测试类
解析:查看全部信息有两种方法
1)session.selectList("cn.happy.dao.IDeptDao.getAllDept");-------实体类.小配置里面的Id名称============字符串
2)IDeptDaomapper=session.getMapper(IDeptDao.class);相当于实现类,getMapper是一个强类型
//01查看全部信息getMapper()接口类的方法名要和小配置的id一样 @Test publicvoidtestSelectAll(){ SqlSessionsession=factory.openSession(); //用的是弱类型========实体类.小配置里面的Id名称============字符串 /*List<Dept>list=session.selectList("cn.happy.dao.IDeptDao.getAllDept"); for(Deptdept:list){ System.out.println(dept.getDeptName()); }*/ //用getMapper方法HIbernate帮我们在内存中代理出一个接口的实现类======相当于强类型 //mapper是一个实现类对象 IDeptDaomapper=session.getMapper(IDeptDao.class); List<Dept>list=mapper.getAllDept(); for(Deptdept:list){ System.out.println(dept.getDeptName()); }
第四步:全文统一用一个大配置
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEconfiguration PUBLIC"-//mybatis.org//DTDConfig3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--Alias别名小配置里面的type的属性值改成别名--> <typeAliases> <typeAliastype="cn.resultMap.enetity.Emp"alias="emp"/> </typeAliases> <environmentsdefault="development"> <environmentid="development"> <transactionManagertype="JDBC"/> <dataSourcetype="POOLED"> <propertyname="driver"value="oracle.jdbc.OracleDriver"/> <propertyname="url"value="jdbc:oracle:thin:@localhost:1521:orcl"/> <propertyname="username"value="sa"/> <propertyname="password"value="1"/> </dataSource> </environment> </environments> <!--映射文件:描述某个实体和数据库表的对应关系--> <mappers> <mapperresource="cn/resultMap/enetity/Emp.xml"/> </mappers> </configuration>
二、resultMap标签
解析:使用的场景是当实体类的属性与数据库不匹配的时候需要用到resultMap实体类和数据库的属性必须一致。(之前用的是实体类)
Eg检索所有员工,以及隶属部门
第一步:创建一个接口
packagecn.resultMap.dao; importjava.util.List; importcn.resultMap.enetity.Emp; publicinterfaceIEmpDao{ //检索所有员工,以及隶属部门 publicList<Emp>getAllEmps(); }
第二步:配置小配置里面的属性
解析:员工角度多的一方,嵌入一的一方的各个属性请使用association是关联(如果去掉association的话就是基础的resultMap)
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="cn.resultMap.dao.IEmpDao"> <resultMaptype="cn.resultMap.enetity.Emp"id="empMap"> <idproperty="empId"column="EMPID"/> <resultproperty="empName"column="EMPNAME"/> <resultproperty="empCity"column="EMPCITY"/> <!--员工角度多的一方,嵌入一的一方的各个属性请使用association--> <associationproperty="dept"javaType="cn.resultMap.enetity.Dept"> <resultproperty="deptName"column="DEPTNAME"/> <resultproperty="deptNo"column="DEPTNO"/> </association> </resultMap> <selectid="getAllEmps"resultMap="empMap"> selecte.*,d.*fromEmpe,Deptd wheree.deptNo=d.deptNo </select> </mapper>
第三步:测试类
//resultMap:实体的属性名和表的字段名保证一致用resultMap //如果报NullException查看小配置的映射关联resultMap是否配置 @Test publicvoidtestAllEmp(){ SqlSessionsession=factory.openSession(); IEmpDaomapper=session.getMapper(IEmpDao.class); List<Emp>allEmps=mapper.getAllEmps(); for(Empemp:allEmps){ System.out.println(emp.getEmpName()+"\t隶属部门"+emp.getDept().getDeptName()); } session.close(); }
第四步:在大配置引入小配置
三、提取sql列
解析:Sql标签简化代码量在小配置里面写
<!--SQl标签的使用--> <sqlid="columns"> d.deptNo,d.deptName </sql> <!--SQl标签的使用--> <selectid="getAllEmps"resultMap="empMap"> selecte.*,<includerefid="columns"/>fromEmpe,Deptd wheree.deptNo=d.deptNo </select>
四、Alias别名
解析:在大配置上写,这样的话在小配置就可以引用别名了
<!--Alias别名小配置里面的type的属性值改成别名--> <typeAliases> <typeAliastype="cn.resultMap.enetity.Emp"alias="emp"/> </typeAliases>
五、动态操作
解析:用于实现动态SQL的元素主要有:
if choose(when,otherwise) where set
Eg查看在北京城市的人员
第一步:接口
packagecn.resultMap.dao; importjava.util.List; importcn.resultMap.enetity.Emp; publicinterfaceIEmpDao{ //检索所有员工,以及隶属部门 publicList<Emp>getAllEmps(); }
第二步:小配<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper PUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="cn.resultMap.dao.IEmpDao"> <resultMaptype="cn.resultMap.enetity.Emp"id="empMap"> <idproperty="empId"column="EMPID"/> <resultproperty="empName"column="EMPNAME"/> <resultproperty="empCity"column="EMPCITY"/> <!--员工角度多的一方,嵌入一的一方的各个属性请使用association--> <associationproperty="dept"javaType="cn.resultMap.enetity.Dept"> <resultproperty="deptName"column="DEPTNAME"/> <resultproperty="deptNo"column="DEPTNO"/> </association> </resultMap> <selectid="getAllEmps"resultMap="empMap"> selecte.*,d.*fromEmpe,Deptd wheree.deptNo=d.deptNo </select> <!--查询动态查询--> <selectid="testAllEmpBuSelect"parameterType="cn.resultMap.enetity.Emp"resultType="cn.resultMap.enetity.Emp"> select*fromEmp <where> <iftest="empId!=null"> andempId=#{empId} </if> <iftest="empName!=null"> andempName=#{empName} </if> <iftest="empCity!=null"> andempCity=#{empCity} </if> </where> </select> </mapper>
第三步:测试
//动态查询 @Test publicvoidtestSelect(){ SqlSessionsession=factory.openSession(); Empemp=newEmp(); //emp.setEmpName("331"); emp.setEmpCity("sh"); List<Emp>list=session.selectList("cn.resultMap.dao.IEmpDao.testAllEmpBuSelect",emp); for(Empemps:list){ System.out.println(emps.getEmpName()); } session.close(); }
第四步:在大配置引入小配置
Eg修改部门信息
第一步:接口
第二步:小配置
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEmapper PUBLIC"-//mybatis.org//DTDMapper3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mappernamespace="cn.resultMap.dao.IDeptDao"> <resultMaptype="cn.happy.entity.Dept"id="deptResultMap"> <idproperty="deptNo"column="deptNo"/> <resultproperty="deptName"column="deptName"/> </resultMap> <selectid="getAllDept"resultMap="deptResultMap"> selectd.*,e.*fromDeptd,Empe whered.deptNo=e.deptNoandd.deptNo=#{deptNo} </select> <!--修改动态查询--> <selectid="testUpdate"parameterType="int"resultType="cn.resultMap.enetity.Dept"> updatedept <set> <iftest="deptNo!=null"> deptNo=#{deptNo}, </if> <iftest="deptName!=null"> deptName=#{deptName}, </if> </set> wheredeptNo=#{deptNo} </select> </mapper>
第三步:测试
/** *动态修改 **/ @Test publicvoidtestUpdate(){ SqlSessionsession=factory.openSession(); Deptdept=newDept(); dept.setDeptName("财务部"); dept.setDeptNo(1); intcount=session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept); session.commit(); System.out.println(count); session.close(); }
以上所述是小编给大家介绍的详解MyBatis的getMapper()接口、resultMap标签、Alias别名、尽量提取sql列、动态操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!