2023年6月27日发(作者:)
MyBatis基于注解开发(联合查询) @Insert @Delete @Update @Param @ResultMap @Results(id="规则名",value={ @Result(property="",column="",id=true), @Result(property="",column=""), @Result(property="",column=""), @Result(property="",javaType=,column="",one=@One(select="⽅法名",fetchType=)), }) @Select("select * from xxx where xxx = #{key}") public Person selectOne(String key); @Select("") public IDCard selectIDCard();package dao;import ;import ;import tions.*;import ype;import ;public interface DeptDao { //设计⼀个⽅法
根据部门编号deptno
查询部门的信息+所属于当前部门的所有员⼯信息 @Results( id="selectDept", value={ @Result(property="deptno",column="deptno",id=true), @Result(property="dname",column="dname"), @Result(property="loc",column="loc"), @Result(property="empList",javaType=,column="deptno",many=@Many(select="selectEmp",fetchType=)), } ) @Select("select * from dept where deptno = #{deptno}") public Dept selectOne(Integer deptno); //辅助⽅法 @Select("select * from emp where deptno = #{deptno}") public Emp selectEmp(Integer deptno); //========================================================== @Select("select * from dept") @ResultMap("selectDept") public List
根据员⼯empno编号
查询员⼯信息+所属部门信息 @Results( id="selectEmp", value={ @Result(property="empno",column="empno",id=true), @Result(property="ename",column="ename"), @Result(property="job",column="job"), @Result(property="mgr",column="mgr"), @Result(property="hiredate",column="hiredate"), @Result(property="sal",column="sal"), @Result(property="comm",column="comm"), @Result(property="dept",javaType=,column="deptno",one=@One(select="selectDept",fetchType=)) }) @Select("select * from emp where empno = #{empno}") public Emp selectOne(Integer empno); //辅助⽅法 @Select("select * from dept where deptno = #{deptno}") public Dept selectDept(Integer deptno); //==================================================== //查询所有的emp信息+每个emp对应的dept对象 @Select("select * from emp") @ResultMap("selectEmp") public List
根据⼈的id
查询⼈的信息+连同对应的⾝份证信息 @Select("select * from person where pid = #{pid}") @Results( id="selectOne", value={ @Result(property="pid",column="pid",id=true), @Result(property="pname",column="pname"), @Result(property="idCard",javaType=,column="cardid",one=@One(select="selectOneIDCard",fetchType=)) } ) public Person selectOne(Integer pid);//⽅法返回值对应的是xml resultMap标签中的type属性 //辅助⽅法 @Select("select * from idcard where cardid = #{cardid}") public IDCard selectOneIDCard(String cardid); //利⽤原来的xml形式 //
⽂件中有好多 // ace----类名 // -----------⽅法名 // 3.
对象 //
辅助查询
辅助查询--(⼀个⽅法
⽅法上的注解 SQL) // 6.两个对象
第⼆个对象延迟机制 settings设置-----注解⾥设置 @Select("select * from person") @ResultMap("selectOne") public List
帮我们执⾏这个⽅法该做的事情 //分析⽅法
⽅法名字delete
⽅法参数sid //分析
类名字tDao---namespace //
⽅法名字delete----------id // mapper的
语句上⾯有参数#{key}-----利⽤⽅法传递的sid参数进⾏匹配 //
代理对象会根据标签
调⽤原来sqlSession对象中的delete⽅法 //类⽅法名和sql之间的对应关系
找到sql
知道sql上⾯的信息
执⾏哪个⽅法 //如果将原有的xml⽂件删掉
改成注解的形式 //1.类
⽅法名和sql对应关系就简单了 //2.注解中的信息肯定需要写sql //3.执⾏底层的哪个对应⽅法-----注解名字类似以前的标签名
为了找寻底层⽅法⽤的 // Insert Delete Update Select @Insert("insert into student value (#{sid},#{sname},#{ssex},#{sage})") //public void insert(Student student);//⼀个domain对象
⼀个基本值(int Integer String) //public void insert(Map map); public void insert(@Param("sid")Integer sid,@Param("sname")String sname,@Param("ssex")String ssex,@Param("sage")Integer sage); //如果dao⽅法的参数不是包装成⼀个对象 // 中将原来的#{key}---#{param1} #{param2} // 2.⽅法的每⼀个参数前⾯
添加@Param("key")
注解的key与SQL#{key}
对应 @Update("update student set sname=#{sname},ssex=#{ssex},sage=#{sage} where sid=#{sid}") public void update(Student student); //================================================================================== //dao的⽅法
负责读取数据库中⼀⾏记录 @Select("select * from student where sid = #{sid}") public Student selectOne(Integer sid); //接⼝底层有⼀个代理类 ProxyStudentDao(对象) //ProxyStudentDao代理对象帮我的Dao接⼝执⾏⼀个⽅法 selectOne // 1.解析⽅法
类名字
⽅法名字
⽅法参数
返回值 // 2.解析⽂件 namespace
标签中的id SQL上的#{key}
标签中的resultType //
类名字----找寻namespace //
⽅法名字--找寻某⼀个标签中的id属性 //
进⽽找到标签中的⼀条SQL语句 //
⽅法的参数---与找到的SQL进⾏key匹配
组合成⼀条完整的SQL语句(可以执⾏的) //
加载驱动 //
获取连接 //
获取状态参数啦 //
拼接SQL //
执⾏查询操作ResultSet = executeQuery(); //
将结果集的信息取出来
存⼊⼀个新的容器内(容器的类型通过解析xml标签中的resultType属性来的) //
资源全部关闭 //
将新的容器连同⾥⾯的数据返回 //
需要的信息 1.类名字 2.⽅法名字 3.定位注解在哪⾥ 4.注解⾥获取⼀条SQL 上⾯的信息(⽅法参数) // 7.执⾏查询后的返回类型(当前⽅法的返回值类型) 8.底层执⾏的⽅法One();(注解名字) //查询多条记录 @Select("select * from student") public List
⼀对⼀ //
⼀对多 //
多对多 //联合查询 //
先查⼀遍
再查⼀遍(⽴即加载
延迟加载) //
联合的⽅式(等值连接
内连接
外连接)}
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687818062a47787.html
评论列表(0条)