mybatis个人笔记

properties引入核心配置文件:
1.可以直接引入外部文件
2.可以在其中增加一些属性配置
3.如果两个文件有同一个字段,优先使用外部配置文件

---------------
ResultMap结果集映射  (当数据库的字段名与实体类的属性不一致时)
     这里的column对应数据库的字段,property为实体类的属性
<resultMap id="map" type="User"
    <result column="pwd" property="password"/>
</resultMap>

<select id,name,pwd  from mybatis.user where id=#{id}
</select>
----------------------
STDOUT_LOGGING标准日志输出
在mybatis核心配置文件中,配置我们的日志
<settings>
         <setting name="logImpl" values="STDOUT_LOGGING"/>
</settings>
-----------------------
关于@Param()注解
1.基本类型的参数或者String类型,需要加上
2.引用类型不需要
3.如果只有一个基本类型的话,可以忽略,但是建议加上
4.我们在SQL中引用的就是我们这里的@Param()中设定的属性名
----------------------
使用lombok插件只需注解,省略大量的代码
1.下载插件(已下载)
2.导入依赖lombok(maven仓库搜索下载)
3.实体类加上@Data  不用写set get
------------------------

多对一处理(查询所有学生和一个老师)
1.学生表 id name tid    老师表  id  name
2.方法一 子查询,方法二使用按照结果嵌套处理(所示)推荐使用
3.注意: 复杂的属性我们需要单独处理,  对象:association   集合:collection

按照结果嵌套处理

<select id="getStudent"  resultMap="StudentTeacher">
    select s.id sid,s.name sname,t.name tname
    from student s,teacher t
    where s.tid=t.id;
</select>
<resultMap id ="studentTeacher" type="Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
        <rsult property="name" column="tname"/>
    </association>
</resultMap>


----------------------------
一对多处理(查询一个老师对应的所有学生)
1使用结果嵌套查询
2.注意:  javaType=""指定属性的类型   集合中的泛型信息,我们使用ofType获取

<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid,s.name sname,t.name tname,t.id tid
    from student s,teacher t
    where s.tid=t.id  and t.id=#{tid}
</select>
<resultMap id ="TeacherStudent" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

 


==================
动态SQL 看官方文档

posted @ 2020-03-14 20:33  lemmon_water  阅读(132)  评论(0)    收藏  举报