Skip to content

Mybatis

约 192 个字 108 行代码 预计阅读时间 2 分钟

指北

Mybatis官网

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.15</version>
</dependency>

Maven静态资源过滤问题

将这段配置放入你的 pom.xml 文件中,Maven 就会根据你定义的路径和规则处理资源文件。

<build>
    <resources>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
       <resource>
           <directory>src/main/resources</directory>
           <includes>
               <include>**/*.properties</include>
               <include>**/*.xml</include>
           </includes>
           <filtering>false</filtering>
       </resource>
    </resources>
</build>

结果集映射:

<resultMap id="UserMap" type="User">
   <!-- id为主键 -->
    <!-- <id column="id" property="id"/> -->
    <!-- <result column="name" property="name"/> -->
    <!-- column是数据库表的列名 , property是对应实体类的属性名 -->
    <!-- 若一致,则可以自动映射 -->
   <result column="pwd" property="password"/>
</resultMap>

<select id="selectUserById" resultMap="UserMap">
  select id , name , pwd from user where id = #{id}
</select>

多对一

image-20240803143249317

association 是对象,collection 是集合

<!-- 多对一 -->
<association property="teacher" javaType="com.selfknow.domain.Teacher"></association>

<!-- 一对多 -->
<collection property="students" javaType="ArrayList" ofType="com,selfknow.domain.Student"></collection>

更推荐多表嵌套查询。

多表嵌套查询

<select id="getStudentList" resultMap="StudentTeacher">
    select
        student.id as sid,
        student.name as sname,
        teacher.id as tid,
        teacher.name as tname
    from student,teacher where student.tid = teacher.id
</select>

<resultMap id="StudentTeacher" type="com.selfknow.domain.Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname" />
    <association property="teacher" javaType="com.selfknow.domain.Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
    </association>
</resultMap>

子查询

<select id="getStudentList2" resultMap="StudentTeacher2">
    select * from student
</select>

<resultMap id="StudentTeacher2" type="com.selfknow.domain.Student">
    <association property="teacher" column="tid" javaType="com.selfknow.domain.Teacher" select="getTeacherById"/>
</resultMap>

<select id="getTeacherById" resultType="com.selfknow.domain.Teacher">
    select * from teacher where id = #{id}
</select>

动态 sql

https://mybatis.org/mybatis-3/dynamic-sql.html

if

<select id="queryBlogIf" parameterType="map" resultType="blog">
  select * from blog where 1=1
   <if test="title != null">
      and title = #{title}
   </if>
   <if test="author != null">
      and author = #{author}
   </if>
</select>

where

实际情况下不能加入 1=1,采用 where 标签,其他的正常写就好了(他会自己处理是否加入 where 或 and)

  • 这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
<select id="queryBlogIf" parameterType="map" resultType="blog">
  select * from blog
   <where>
       <if test="title != null">
          title = #{title}
       </if>
       <if test="author != null">
          and author = #{author}
       </if>
   </where>
</select>

set

更新操作

choose 标签类似于 switch

<select id="queryBlogChoose" parameterType="map" resultType="blog">
  select * from blog
   <where>
       <choose>
           <when test="title != null">
                title = #{title}
           </when>
           <when test="author != null">
              and author = #{author}
           </when>
           <otherwise>
              and views = #{views}
           </otherwise>
       </choose>
   </where>
</select>

Last update: August 6, 2024
Created: August 6, 2024