Mybatis——注解的使用、联表查询、动态SQL和缓存

使用注解开发

mybatis中使用注解只能完成一些简单的sql,更多的还是使用xml

@Select("select * from user")
List<User> getUsers();

注解进行增删改查

@Select("select * from user where id=#{id}")
User getUserById(@Param(id) int id);
@Insert("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{password} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id = #{id}")
int deleteUser(@param("id") int id);

关于@Param()注解

基本类型的参数或者String类型,需要加上

引用类型不需要加

只有一个基本类型,可以不用

SQl中引用的就是我们这里的@Param()中设定的属性名


Lombok

1、在IDEA中安装Lombok插件

2、在项目中导入Lombok的jar包

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>

3、在实体类上加注解即可使用

@Data:无参构造,get,set,tostring,hashcode,equals

@AllArgsConstructor:有参构造

@NoArgsConstructor:无参构造

 

联表查询

MySQL多对一查询方式

比如多个学生对应一个老师

子查询:按照查询嵌套处理

<select id="getStudent" resultMap="StudentTeacher">
        select * from Student;
</select>
    
<resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"></result>
        <result property="name" column="name"></result>
        <!-- 复杂的属性,我们需要单独处理  对象:association 集合:collection  -->
        <!-- javaType是teacher的类型 -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association>
</resultMap>
    
<select id="getTeacher" resultType="Teacher">
        select * from teacher where id = #{id}
</select>

联表查询

 

<!-- 按照结果嵌套处理 -->
    <select id="getStudent2" resultMap="StudentTeacher2">
        select s.id sid,s.name sname,t.name tname
        from student s,teacher t
        where s.tid = t.id
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <result property="id" column="sid"></result>
        <result property="name" column="sname"></result>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"></result>
        </association>
    </resultMap>

一对多处理

比如一个老师对应多个学生

按照查询嵌套处理

<select id="getTeacher" resultMap="StudentTeacher">
    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="StudentTeacher" type="Teacher">
    <result property="id" column="tid"></result>
    <result property="name" column="tname"></result>
    <!-- 复杂的属性需要单独处理  对象:association  集合:collection
         javaType=""  指定属性的类型
         集合中的泛型信息,用ofType获取
    -->
    <collection property="student" ofType="Student">
        <result property="id" column="sid"></result>
        <result property="name" column="sname"></result>
        <result property="tid" column="tid"></result>
    </collection>
</resultMap>

按照结果嵌套处理

<select id="getTeacher2" resultMap="StudentTeacher2">
    select * from teacher where id = #{tid}
</select>

<resultMap id="StudentTeacher2" type="Teacher">
    <result property="id" column="tid"></result>
    <result property="name" column="tname"></result>
    <collection property="student" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"></collection>
</resultMap>

<select id="getStudentByTeacherId" resultType="Student">
    select * from student where id = #{tid}
</select>

总结:

关联 - association 【多对一】

集合 - collection   【一对多】

 

javaType  &  ofType

javaType用来指定实体类中属性的类型

ofType用来指定映射到List或者集合中的pojo类型

动态SQL

动态SQL就是在拼接SQL语句

动态SQL其实还是SQL语句,只是可以在SQL层面执行逻辑代码

if、where、set、choose、when、foreach

where&if

<select id="getBlog" parameterType="map" resultType="com.lyr.pojo.Blog">
    select * from blog
    <where>
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="auther != null">
            and auther = #{auther}
        </if>
    </where>
</select>

 

choose(when,otherwise)

<select id="chooseBlog" parameterType="map" resultType="com.lyr.pojo.Blog">
    select * from blog
    <where>
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="auther">
                and auther = #{auther}
            </when>
            <otherwise>
                views = #{views}
            </otherwise>
        </choose>
    </where>
</select>

 

trim(set,where)

<update id="updateBlog" parameterType="map">
    update blog
    <set>
        <if test="title != null">
            title = #{title},
        </if>
        <if test="auther != null">
            auther = #{auther}
        </if>
    </set>
    where id = #{id}
</update>

 

foreach

<!-- select * from blog where 1=1 and (id=1 or id=2 or id=3)   -->
    <select id="foreachBlog" parameterType="map" resultType="com.lyr.pojo.Blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id = #{id}
            </foreach>
        </where>
</select>


Sql片段

实现代码的复用

使用:

1、使用SQL标签提取公共的部分

<sql id="select-title-auther">
    <if test="title != null">
        title = #{title}
    </if>
    <if test="auther != null">
        auther = #{auther}
    </if>
</sql>

2、在需要使用的地方使用include标签引用即可

<where>
    <include refid="select-title-auther"></include>
</where>

注意事项:
最好基于单表来定义SQL片段
不要存在where


缓存

一级缓存:

一级缓存也叫本地缓存:SqlSession

与数据库同一次会话期间查到的数据会放在本地缓存中。

以后如果需要获取相同的数据,直接从缓存中拿,就不用去查询数据库了。

一级缓存是默认开启的,只在一次Sqlsession中有效

一级缓存就相当于一个map

 

缓存失效的情况:

1、查询不同的东西

2、增删改操作,可能会改变原来的数据,所以会刷新缓存

3、查询不同的Mapper.xml

4、手动清理缓存

//清理缓存
sqlSession.clearCache();

 

 

二级缓存:

二级缓存也叫全局缓存,比一级缓存的作用域广

一个命名空间(namespace)对应一个二级缓存

使用:

1、开启全局缓存

<setting name="cacheEnable" value="true"/>

2、在Mapper.xml中使用二级缓存

<cache
    eviction="FIFO"
    flushInterval="60000"
    size="512"
    readOnly="true"/>

3、测试

 

总结:

只要开启了二级缓存,在同一个Mapper下就有效

所以的数据都会先放在一级缓存中,只有当会话提交或者关闭的时候,才会提交到二级缓存中。

缓存顺序:

先看二级缓存中有没有

再看以及缓存中有没有

查询数据库

Mybatis就先到这了,接下来就要去学Spring啦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值